How do I Display a .svg Icon Without it Getting Clipped? - html

I am trying to display these social media icons on my website in such a way that I can change the color using css (so i can add the hover effect shown in the code below).
However, since these are vector image files (.svg) I couldn't seem to find a way to insert the image into my web page using the conventional <img src=""> method. So I proceeded to create CSS classes to get the image to appear as a CSS mask (thus allowing me to change the color via CSS).
However, I quickly noticed that the sides of the otherwise circular vector graphic icons were slightly cutoff. Being the perfectionist that I am, I wanted to make the icons actually appear round as they were supposed to. So, after checking out the webkit-mask MSDN entry I increased the width of the .media-buttons class and used -webkit-mask-position to shift the image a few pixels down and to the left as is evident in my code below. This obviously did not work.
It has also come to my attention that -webkit-mask is not fully supported by all browsers.
CSS:
#cross{-webkit-mask: url("http://path/to/image.svg") no-repeat;}
.media-buttons {
width: 202px;
height: 202px;
background: #000;
-webkit-mask-position: 10px 10px;
}
.media-buttons:hover {background: rgb(94, 176, 201);}
HTML:
<img class="media-buttons" id="cross">
TL;DR, I am looking for a (possibly new) way to display my .svg icons that has somewhat decent browser support and the ability to manipulate the icon's color via CSS.

Here's one way:
svg:hover {
color: #009BCD;
}
p {
color: green;
}
<svg width="200px" height="200px" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns" display="none">
<title>Cross</title>
<description>Created with Sketch (http://www.bohemiancoding.com/sketch)</description>
<defs></defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
<path d="M100,0 C44.771524,3.03201907e-14 0,44.771524 0,100 C0,155.228476 44.771524,200 100,200 C155.228476,200 200,155.228476 200,100 C200,44.771524 155.228476,-3.41060513e-14 100,0 Z M84.0763011,40 L115.923699,40 L115.923699,84.1405714 L160,84.1405714 L160,116.034408 L115.923699,116.034408 L115.923699,160.16875 L84.0763011,160.16875 L84.0763011,116.034408 L40,116.034408 L40,84.1405714 L84.0763011,84.1405714 L84.0763011,40 Z" id="Cross" fill="currentColor" sketch:type="MSShapeGroup"></path>
</g>
</svg>
<p>
Heh! What's this
<svg width="1em" height="1em" viewBox="0 0 200 200">
<use xlink:href="#Cross"/>
</svg>
doing in the middle of a sentence?
</p>
<svg width="50px" height="50px" viewBox="0 0 200 200">
<use xlink:href="#Cross"/>
</svg>
What I did here was:
Copy the SVG into the HTML (and removed the XML preamble)
Set display="none" on the <svg> so that it is invisible on the page.
Changed the fill on the icons <path> element to currentColor
Now every time you want a copy of the icon to appear, create a mini-svg that reverences the <path> in the first SVG.
<svg width="50px" height="50px" viewBox="0 0 200 200">
<use xlink:href="#Cross"/>
</svg>
Just set it's size with the width and height values. You just need to include each icon once and you can reference them as many times as you like.
What is currentColor for?
currentColor is a special color value in SVGs that tells the elements to use the value of the current color setting. It means you can set the color outside the <use> element and it gets inherited by whatever the <use> is referencing. Normally you can't style each of the icon references individually. They would all be stuck having whatever fill the original <path> is set to.
Using just the original SVG
svg path {
fill: blue;
}
svg:hover path {
fill: #009BCD;
}
<p>SVG just used as is</p>
<svg width="100px" height="100px" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">
<title>Cross</title>
<description>Created with Sketch (http://www.bohemiancoding.com/sketch)</description>
<defs></defs>
<g id="Page-1" stroke="none" stroke-width="1" fill-rule="evenodd" sketch:type="MSPage">
<path d="M100,0 C44.771524,3.03201907e-14 0,44.771524 0,100 C0,155.228476 44.771524,200 100,200 C155.228476,200 200,155.228476 200,100 C200,44.771524 155.228476,-3.41060513e-14 100,0 Z M84.0763011,40 L115.923699,40 L115.923699,84.1405714 L160,84.1405714 L160,116.034408 L115.923699,116.034408 L115.923699,160.16875 L84.0763011,160.16875 L84.0763011,116.034408 L40,116.034408 L40,84.1405714 L84.0763011,84.1405714 L84.0763011,40 Z" id="Cross" sketch:type="MSShapeGroup"></path>
</g>
</svg>

I can't tell for sure where this bug is coming from but I think it has something to do with borders. Add a 1px border and you'll see some really weird behaviour. Now to answer your question: To make sure your image doesn't get clipped you can use mask-size. E.g. -webkit-mask-size: 50% 50%;
Just don't think masks are ready for primetime just yet.
Here's a pen that demonstrates this: http://codepen.io/anon/pen/ONLRbZ

Related

White gap between SVG and div

This is not a duplicate of any current questions that I can find. I have tried answers such as adding block/flex to the SVG element but I believe this is a different .
I am using Tailwind if that is of any relevance.
This is one of the multiple, different SVGs that this issue is present on:
<svg style="width: 100%" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" y="0px" x="0px" xml:space="preserve" version="1.1" viewBox="0 0 1917.4503 99.737572" id="Untitled-Page%201">
<metadata id="metadata64">
<rdf:rdf><cc:work rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"></dc:type><dc:title></dc:title></cc:work></rdf:rdf>
</metadata>
<defs id="defs62">
<clipPath id="clipPath83" clipPathUnits="userSpaceOnUse">
<rect y="4.6582928" x="1.9868355" height="520.61298" width="1913.6428" id="rect85" style="fill: #0000ff; fill-rule: evenodd"></rect>
</clipPath>
<clipPath id="clipPath101" clipPathUnits="userSpaceOnUse">
<rect y="2.0105031" x="1.6986296" height="99.737572" width="1917.4503" id="rect103" style="fill: #0000ff; fill-rule: evenodd"></rect>
</clipPath>
</defs>
<g transform="translate(-1.6986296,-2.0105031)" clip-path="url(#clipPath101)" id="g79">
<path id="110" d="m -92.8182,485.3333 c 148.4834,-10.021 80.7045,-8.8997 264.4613,-8.8997 211.3321,0 442.2889,49.5664 666.4687,49.5664 255.8733,0 518.9805,-59.2854 737.5684,-59.2854 335.3557,0 441.894,29.1565 441.894,29.1565 L 2035,256 c 0,0 -38.1606,11.5786 -106.04,22.415 L 1919,33.9229 c 0,0 -67.2518,32.8281 -278.9438,32.8281 C 1502.0735,66.751 1335.988,0 1174.4691,0 1032.9564,0 887.1659,55.8081 753.7633,55.8081 619.0213,55.8081 489.1034,1.0942 387.7024,1.0942 230.6074,1.0942 -14,33.9229 -14,33.9229 l -0.0303,192.3195 c 0,0 -30.8519,-4.4524 3.0303,-5.3425 v 77.905 c -115.2449,9.8118 -4.7734,-2.7802 -103.0303,4.7102" fill="#4d5061"></path>
</g>
</svg>
I have multiple SVG elements that I am using to create a wave-like effect. In the picture below, you can see the top section which is the SVG and underneath it you can see the background of the content.
This issue only appears at certain resolutions and the thickness of the line varies between what appears to be half a pixel and 1 pixel in height.
The behaviour occurs both when the SVG is inline or as an IMG. The SVG itself is styled to be 100% width with height set to auto.
I've noticed that tweaking the viewbox allows the SVG to line up properly but this only makes the gap appear at different resolutions instead.
I need a solution that will make this wave SVG sit flush with no pixel gap on all devices, and ideally an explanation to why it is behaving this way because I've been bashing my head against this for too long.
There are multiple SVGs and this problem occurs with all of them.
Fixed this by adding a -1px margin to either the top or the bottom of SVG element to overlap the elements together.
You can put your image and the following div inside a flex with flex-direction colum.
Without flex:
With flex wrapper:
.box {
height:100px;
background-color:#00cba9;
margin:0;
}
svg{
padding-bottom:0;
margin-bottom:0;
border:solid red 3px;
}
.wrapper{
display:flex;
flex-direction: column;
}
<div class="wrapper">
<svg class="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="#00cba9" fill-opacity="1" d="M0,32L46.5,256L92.9,288L139.4,192L185.8,192L232.3,192L278.7,192L325.2,64L371.6,128L418.1,64L464.5,288L511,160L557.4,128L603.9,128L650.3,32L696.8,160L743.2,0L789.7,0L836.1,64L882.6,96L929,64L975.5,64L1021.9,32L1068.4,224L1114.8,160L1161.3,96L1207.7,224L1254.2,64L1300.6,128L1347.1,160L1393.5,0L1440,320L1440,320L1393.5,320L1347.1,320L1300.6,320L1254.2,320L1207.7,320L1161.3,320L1114.8,320L1068.4,320L1021.9,320L975.5,320L929,320L882.6,320L836.1,320L789.7,320L743.2,320L696.8,320L650.3,320L603.9,320L557.4,320L511,320L464.5,320L418.1,320L371.6,320L325.2,320L278.7,320L232.3,320L185.8,320L139.4,320L92.9,320L46.5,320L0,320Z"></path></svg>
<div class="box"></div>
</div>

How to reference different SVGs inside the same SVG file in CSS background?

I am creating a website and a very important requirement of this website is to have a minimal number of files. One place where I can reduce the number of files are the SVG files of the project. Currently this project is using 10+ SVG files and I hope to combine them all into one SVG sprite sheet. Since these SVG files were being used as background images in CSS:
.class-name {
background-image: url(/path/to/file.svg)
}
I now have an svg file, where each one of the previous SVG files is a symbol, so the svg file looks like this:
<svg>
<symbol id="id1">....</svg>
<symbol id="id2">....</svg>
<symbol id="id3">....</svg>
...
</svg>
I wish to use these symbols in my CSS as follows:
.class-name {
background-image: url(/path/to/combined-file.svg#id1)
}
How can I use the SVG symbols in my background image.
I have already tried the following:
.class-name {
background-image: url(/path/to/combined-file.svg#id1)
}
and also
.class-name {
background-image: url("data:image/svg+xml;base64,<svg><use xlink:href="path/to/combined-svg#id1/></svg>"");
}
I have also tried converting all the <symbol> tags to <g> tags but then all the images start to overlap each other and therefore it becomes pointless.
I am hoping for a solution where all my SVG are in one file and i can reference them individually in my CSS background image.
This is an example where I'm using svg sprites as background:
div{width:300px;height:300px; border:1px solid;
background-image:url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/cat.svg#redcat);
background-size:200px;
background-repeat: no-repeat;
background-position: center;
}
<div></div>
You can also use the #blackcat
The combined file looks like this:
(Please observe the style element where this rule svg > svg:not(:target) {display: none;} is hiding all the nested svg elements unless they are the target)
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100px" height="100px" viewBox="0 0 225 225">
<style type="text/css">
<![CDATA[
/* this is hiding all the nested svg elements unless they are the target*/
svg > svg:not(:target) {
display: none;
}
]]>
</style>
<desc>
<g id="cat">
<path id="body" fill-rule="evenodd" clip-rule="evenodd" d="M121.506,108.953.....108.953z"/>
<path id="head" fill-rule="evenodd" clip-rule="evenodd" d="M129.747,18.651......81.453z"/>
</g>
</desc>
<svg version="1.1" id="blackcat" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 225 225">
<use xlink:href ="#cat" fill="black" />
</svg>
<svg version="1.1" id="redcat" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 225 225">
<use xlink:href ="#cat" fill="red" />
</svg>
</svg>
I hope this is what you need.
You will find this explained in detail in this book: Using SVG with CSS3 and HTML5: Vector Graphics for Web Design

SVG icons are very large when inlining the SVG to the HTML document

I have essentially this:
<body>
<svg style="display:none">
<symbol id="mysymb1" viewBox="0 0 200 500">
<g>
<path ...></path>
</g>
</symbol>
<symbol id="mysymb2" viewBox="0 0 200 500">
<g>
<path...></path>
</g>
</symbol>
</svg>
<div>
<svg viewBox="0 0 10 25">
<use xlink:href="#mysymb1"></use>
</svg>
</div>
</body>
My symbols I want to be large during development so I can get in there with some detail, but then when I use them I want them to be like small icons.
The problem is, however, that the icon is showing up extremely large, like over 1000px width and height, causing scrolling and everything. This is pretty much literally the code I have. There's no nested svg elements anywhere, and the paths don't extend beyond the 200/500 bounds.
Not sure what to do to make it work so that the icon is small as defined. TBH I always have this problem with SVG and I'm not sure why it happens.
You need to set the size via CSS. Otherwise the SVG is as large as possible. The viewBox is not the relevant part for resizing an SVG in HTML/CSS.
Example code:
svg {
height: 50px;
width: 20px;
}

How to make angled SVGs interlock

I currently have two SVGs that were designed to interlock with eachother but the higher SVG (The first in the imgur link) acts as if it were a rectangle and "pushes" the lower SVG (The second picture in the imgur link) down away from it and they end up with a large space between them (The third imgur link). I have only changed the width of the second SVG in the code so far. Without manually aligning them, which would ruin my page's responsiveness, is there a way to give the SVG a smaller hitbox or similar?
https://imgur.com/a/YtBuso4
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1250">
<defs>
<style>
.cls-1 {
fill: #190eae;
}
</style>
</defs>
<path id="bali-beautiful-beauty-433539" class="cls-1" d="M0,0H1920V1080L0,1250Z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 960 1080.021" id="sectiona">
<defs>
<style>
.cls-1 {
opacity: 0.7;
}
</style>
</defs>
<g id="Group_78" data-name="Group 78" transform="translate(-488 -3248.979)">
<path id="Path_26" data-name="Path 26" class="cls-1" d="M-1-16.511l960-85.021V978.489l-960-85Z" transform="translate(489 3350.511)"/>
</g>
</svg>
Thank you
I think the most simple solution to your problem is to lessen the viewBox height of the first <svg>, but to show the overflow. That way, the triangular form at the bottom will "slip" under the second <svg>
svg {
overflow:visible;
display:block;
}
.cls-1 {
fill: #190eae;
}
.cls-2 {
opacity: 0.7;
}
<svg id="svg-top" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1080">
<path id="bali-beautiful-beauty-433539" class="cls-1" d="M0,0H1920V1080L0,1250Z"/>
</svg>
<svg id="svg-bottom" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 960 1080.021" id="sectiona">
<g id="Group_78" data-name="Group 78" transform="translate(-488 -3248.979)">
<path id="Path_26" data-name="Path 26" class="cls-2" d="M-1-16.511l960-85.021V978.489l-960-85Z" transform="translate(489 3350.511)"/>
</g>
</svg>
Note how I have moved the styles outside the SVGs. They are part of the same DOM anyway, and if both quote the same class name, both styles will be applied to both paths. I've changed the class name for one of them, so that does not happen.
Another issue is that <svg> elements in HTML are inline-blocks. As such they have a line height, and if they are displayed one below the other (as happens here because their default width is 100%), that may lead to a small visible gap between their layout boxes. Setting display:block solves that.

Filling up remaining space in svg

I tried to create an svg and adding path to it.
Here is my jsfiddle
Now, as you can see, I managed to create a down facing triangle but what I am not able to do is filling up the rest of the space(white colored) inside the svg with some other color(say black). How do I do that?
Here is my HTML
<svg id="bigTriangleColors" width="100%" height="100"
viewBox="0 0 100 102" preserveAspectRatio="none">
<path d="M0 0 L50 100 L100 0 Z"></path>
</svg>
and CSS
#bigTriangleColors {
fill: #E1EAEF;
position: relative;
}
svg:not(:root) {
overflow: hidden;
}
Thank you for your help!
I don't know If I understood correctly, but have you just tried to set the background color on svg
svg {
bacground-color: black;
}
like in this fiddle http://jsfiddle.net/Luoq4Lcz/ ?
Does that solve your problem?
Using background-color on <svg> element works (I believe) in all the latest browser versions, but it didn't work on older versions of every browser.
If backwards compatibility is important to you, then a better solution would be to either:
surround the SVG with a <div> and give that a background colour, or
add a rectangle of the desired colour to the SVG itself:
<svg id="bigTriangleColors" width="100%" height="100"
viewBox="0 0 100 102" preserveAspectRatio="none">
<rect width="100%" height="100%" fill="black"/>
<path d="M0 0 L50 100 L100 0 Z" fill="red"></path>
</svg>