Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

962
Visualizações
Drawing an SVG line diagonally across its containing DIV (and maintaining as DIV resized)

I am trying to draw an SVG line diagonally across a div like this:

enter image description here

I can create a div with an SVG line inside:

<div id="my_div" class="rectangle">
    <svg id="my_line" width='100%' height='100%' viewBox='0 0 2000 2000' preserveAspectRatio='none'><line style='red; stroke-width:2' /></svg>
</div>

Since I can resize the containing rectangle div, I need the SVG line to move with the rectangle div as it's resized, always touching the top left and bottom right corners.

I try to use getBoundingClientRect to set the line positions:

var rect_box = document.getElementById("my_div").getBoundingClientRect();

$("#my_line").attr("x1", rect_box.left)
$("#my_line").attr("y1", rect_box.top)
$("#my_line").attr("x2", rect_box.right)
$("#my_line").attr("y2", rect_box.bottom)

But this doesn't work. Is there a way to set the viewBox, or apply CSS, such that the SVG line always remains fixed across the diagonal of its containing div?

Note: There's also this approach that uses CSS clip path, and it remains fixed to its corners during resizing, but the thickness of the line changes as it's resized. I need the line thickness to remain constant.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No javascript required.

#my_div {
  outline: 1px solid;
  height: 200px;
}
<div id="my_div">
  <svg width='100%' height='100%' viewBox='0 0 100 100' preserveAspectRatio='none'>
       <line x1="0" y1="0" x2="100" y2="100" vector-effect="non-scaling-stroke" stroke="red" />
    </svg>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda