SVG responsive on div - html

As the title said, I want to add an SVG border animation to a div. I tryed with a static div but now I want to make it works even if the div width and height change dynamically (as thumbnail class with Bootstrap)
Here is my SVG :
<svg>
<line class="top" x1="0" y1="0" x2="3000" y2="0"/>
<line class="left" x1="0" y1="3000" x2="0" y2="0"/>
<line class="bottom" x1="3000" y1="300" x2="0" y2="300"/>
<line class="right" x1="300" y1="0" x2="300" y2="300"/>
</svg>
Thanks for the help, here is a jsbin : http://jsbin.com/suvinakaqa/1/edit

If you want the SVG to responsively fill the <div>, you need to give it a viewBox and set preserveAspectRatio="none" so that it stretches to fill the <div> both horizontally and vertically. Otherwise it will just keep its aspect ratio and just scale up or down to fit inside the <div>.
Note that the stretching that occurs, now that you've disabled aspect ratio, will cause the horizontal lines to appear to have a different width to the vertical ones. To fix that you could add vector-effect="non-scaling-stroke" to the lines.
.myDiv
{
width: 100%;
height: 300px;
}
<div class="myDiv">
<svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none"
stroke="black" stroke-width="4">
<line class="top" x1="0" y1="0" x2="100" y2="0" vector-effect="non-scaling-stroke"/>
<line class="left" x1="0" y1="100" x2="0" y2="0" vector-effect="non-scaling-stroke"/>
<line class="bottom" x1="100" y1="100" x2="0" y2="100" vector-effect="non-scaling-stroke"/>
<line class="right" x1="100" y1="0" x2="100" y2="100" vector-effect="non-scaling-stroke"/>
</svg>
</div>

For eg you have following in HTML
<div class="myClass">
<svg>
<line class="top" x1="0" y1="0" x2="3000" y2="0"/>
<line class="left" x1="0" y1="3000" x2="0" y2="0"/>
<line class="bottom" x1="3000" y1="300" x2="0" y2="300"/>
<line class="right" x1="300" y1="0" x2="300" y2="300"/>
</svg>
</div>
Please use following CSS
.myClass svg{
width:100% !important;
height:auto;
}

Related

Inconsistent SVG lines despite shape-rendering [duplicate]

This question already has answers here:
Why is SVG stroke-width : 1 making lines transparent?
(6 answers)
Closed 2 years ago.
Why are some of my lines in svg thicker than others?
All of them are on whole number coords. "stroke-width: 0.5" was my last attempt to create 1px line. I had tryed also elsewhere mentioned shape-rendering: crispedges with stroke of 1 pixel but with same setup those lines which appears thicker on image were straight 2px instead of 1. I would love to fix that somehow but even knowledge about how its determined which lines get thicker by what rule would be very appretiated. I dont understand why its so hard to make consistent 1px thick line using svg.
My code:
#my-svg {
width : 500px;
height: 500px;
margin: .5em;
}
#my-svg line {
stroke:black;
stroke-width:0.5;
}
<svg id="my-svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
<line x1="0" y1="26" x2="380" y2="26" ></line>
<line x1="0" y1="52" x2="380" y2="52" ></line>
<line x1="0" y1="78" x2="380" y2="78" ></line>
<line x1="0" y1="104" x2="380" y2="104"></line>
<line x1="0" y1="130" x2="380" y2="130"></line>
<line x1="0" y1="156" x2="380" y2="156"></line>
<line x1="0" y1="182" x2="380" y2="182"></line>
<line x1="40" y1="0" x2="40" y2="182"></line>
<line x1="240" y1="0" x2="240" y2="182"></line>
<line x1="300" y1="0" x2="300" y2="182"></line>
<line x1="380" y1="0" x2="380" y2="182"></line>
</svg>
Example when using stroke-width: 1 and shape-rendering: crispedges:
i used a html document and a seperated online editor to check your code twice. Your code is corect! It seems to be a browser visualisation error...
Quick tip: your code line can also end like this: stroke:black; stroke-width:0.5" />
If the error persists, please provide us with additional information such as browser, version, full code review, etc.
The Way it work for me:
<!DOCTYPE html>
<html>
<body>
<svg height="500" width="500" viewbox="0 0 500 500">
<line x1="0" y1="26" x2="380" y2="26" style="stroke:black; stroke-width:0.5" />
<line x1="0" y1="52" x2="380" y2="52" style="stroke:black; stroke-width:0.5" />
<line x1="0" y1="78" x2="380" y2="78" style="stroke:black; stroke-width:0.5" />
<line x1="0" y1="104" x2="380" y2="104" style="stroke:black; stroke-width:0.5" />
<line x1="0" y1="130" x2="380" y2="130" style="stroke:black; stroke-width:0.5" />
<line x1="0" y1="156" x2="380" y2="156" style="stroke:black; stroke-width:0.5" />
<line x1="0" y1="182" x2="380" y2="182" style="stroke:black; stroke-width:0.5" />
<line x1="40" y1="0" x2="40" y2="182" style="stroke:black; stroke-width:0.5" />
<line x1="240" y1="0" x2="240" y2="182" style="stroke:black; stroke-width:0.5" />
<line x1="300" y1="0" x2="300" y2="182" style="stroke:black; stroke-width:0.5" />
<line x1="380" y1="0" x2="380" y2="182" style="stroke:black; stroke-width:0.5" />
</svg>
</body>
</html>

Drawing Lines in html

How can i draw something like this in Html ?
<svg height="210" width="500">
<line x1="0" y1="100" x2="200" y2="100" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="200" y1="50" x2="200" y2="150" style="stroke:rgb(255,250,0);stroke-width:2" />
<line x1="200" y1="50" x2="300" y2="50" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="200" y1="150" x2="300" y2="150" style="stroke:rgb(255,0,0);stroke-width:2" />
</svg>
You can use something like the following:
https://jsfiddle.net/qj0672yx/2/
#leftbox {
float:left;
margin-top:65px;
margin-right: 10px;
}
#rightbox{
float:right;
margin-top:0px;
margin-left:0px;
}
#middlebox{
float:left;
margin-right:0px;
}
<div id = "leftbox">
<h2>TESTTESTTEST </h2> </div>
<div>
</div id = "middlebox">
<svg height="210">
<line x1="0" y1="100" x2="200" y2="100" style="stroke:rgb(0,0,0);stroke-width:2" />
<line x1="200" y1="50" x2="200" y2="150" style="stroke:rgb(0,0,0);stroke-width:2" />
<line x1="200" y1="50" x2="300" y2="50" style="stroke:rgb(0,0,0);stroke-width:2" />
<line x1="200" y1="150" x2="300" y2="150" style="stroke:rgb(0,0,0);stroke-width:2" />
</svg>
</div>
<div id = "rightbox">
<h2>TESTTESTTEST </h2> </div>
</div>
However using bootstrap for its grid system and ease to specify and locate the divs would be more logical in your situation. See the link for grid system:
https://getbootstrap.com/docs/4.3/layout/grid/

show lines in each edge of the rectangle

I wanted to create a graphics where there will be a rectangle node in the center and from each of it's corner edges, draw a cross line. That means 4 lines in 4 corner edge of the rectangle.
I could only show some lines in the top left edge of the rectangle. Can anyone guide me on how to show other three lines from that rectangle, please? I did not understand the line concept from the docs
Here is the demo
<svg width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
<line x1="0" y1="0" x2="0" y2="90" stroke="black" stroke-width="3" transform="rotate(-220)"/>
<line x1="0" y1="0" x2="10" y2="90" stroke="black" stroke-width="3" transform="rotate(-220)"/>
<line x1="0" y1="0" x2="10" y2="90" stroke="black" stroke-width="3" transform="rotate(-220)"/>
<line x1="0" y1="0" x2="10" y2="90" stroke="black" stroke-width="3" transform="rotate(-220)"/>
</g>
</svg>
http://jsbin.com/jucosalice/edit?html,output
To draw cross lines at each corner without calculating coordinates, you can use rotate and translate properties:
<svg width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
<line x1="0" y1="0" x2="100" y2="0" transform="rotate(-135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(0 60) rotate(135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 60) rotate(45)" stroke="black" stroke-width="3"/>
</g>
</svg>
Explanation:
The X axis is from left to right, the Y axis is from top to bottom. The direction of rotation is clockwise. So, the code
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
draws a line from (0,0) to (100,0), translate(shift) it by 200 unit in X direction towards right (as it is width of main rectangle), and then rotate it by -45 deg to get its slope.
See here for their documentation.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<svg width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5"></rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
<line x1="0" y1="0" x2="-75" y2="-75" stroke="black" stroke-width="3"/>
<line x1="200" y1="60" x2="275" y2="135" stroke="black" stroke-width="3"/>
<line x1="200" y1="0" x2="275" y2="-75" stroke="black" stroke-width="3"/>
<line x1="0" y1="60" x2="-75" y2="135" stroke="black" stroke-width="3"/>
</g>
</svg>
</body>
</html>

SVG style rules aren't respected when added through CSS, but respected when explicitly set through element <style> tag in HTML

I'm trying to integrate a small SVG image on my web-page. I'm using it as background-image for my body. The image is a small path grid, painted black. Please note the <style> tag.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1000px" height="500px" viewBox="0 0 1000 500" zoomAndPan="disable" preserveAspectRatio="none">
<style type="text/css"><![CDATA[
line
{
stroke: rgba(0,0,0, 0.75); /* Color */
stroke-width: 1px;
}
]]></style>
<line x1="1100" y1="500" x2="-1000" y2="250"/>
<line x1="1100" y1="500" x2="-1000" y2="-100"/>
<line x1="1100" y1="500" x2="-1000" y2="-500"/>
<line x1="1100" y1="500" x2="-250" y2="-500"/>
<line x1="1100" y1="500" x2="300" y2="-500"/>
<line x1="1100" y1="500" x2="750" y2="-500"/>
<line x1="1750" y1="0" x2="750" y2="500"/>
<line x1="1500" y1="0" x2="500" y2="500"/>
<line x1="1150" y1="0" x2="150" y2="500"/>
<line x1="700" y1="0" x2="-300" y2="500"/>
<line x1="150" y1="0" x2="-850" y2="500"/>
</svg>
The thing is, if I set it as a background image in my CSS, like so:
.body{
background-image: url("images/intro.svg");
}
It shows up white.
Alternatively, when I set it explicitly, through HTML:
<body style="background-image: url('images/intro.svg');">foo foo foo</body>
it shows up fine. Black, with 75% opacity (alpha).
What is going on here? Anyway I can fix this, without having to do it explicitly through HTML?

SVG Line not drawing at some co-ordinates

SVG Line at this point isn't drawing line at this coordinates (x1="200" x2="0" y1="200" y2="200") . This problem happens only in Firefox. What mistake did I make?
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<line x1="0" x2="200" y1="0" y2="0" style="stroke:rgb(255,0,0);stroke-width:2"></line>
<line x1="200" x2="200" y1="0" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"></line>
<line x1="0" x2="0" y1="0" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"></line>
<line x1="200" x2="0" y1="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"></line>
</svg>