Firefox and responsive SVG - css

Here is a thing.
I have a 700x700px image that i would need to mask with SVG.
For Chrome and Safari i did that by using -webkit-mask-box-image with external SVG and it works properly.
For Firefox, i used clip-path property, and again, it functions properly.
The responsive part is problem.On Chrome&Safari, that part is working nicely, but on Firefox only the main image is resized, mask stays the same.
I am a complete newbie at this and i tried tons of solutions that i found online and i really couldn’t make it work.
<style>
body {
background: yellow;
}
.img-mask {
-webkit-mask-box-image: url('http://imgh.us/mask_3.svg');
mask-border: url('http://imgh.us/mask_3.svg');
clip-path: url(#mask);
}
</style>
<img src="http://gto-live.com/wp-content/uploads/2015/04/charm-elegance-colorful-sofa-living-room-decor-718x718.jpg" class="img-mask">
enter code here
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="700" height="700" viewBox="0 0 700 700">
<clipPath id="mask">
<path d="M718.004,358.999 C718.004,160.726 557.272,-0.007 358.998,-0.007 C160.725,-0.007 -0.007,160.726 -0.007,358.999 C-0.007,557.272 160.725,718.005 358.998,718.005 C557.272,718.005 718.004,557.272 718.004,358.999 Z"/>
</clipPath>
</svg>
Any help would really really be gratefully appreciated!
Fiddle can be found here https://jsfiddle.net/y7zaw4bz/1/

You need to use objectBoundingBox units (and make the path run from 0 to 1) e.g.
body {
background: yellow;
}
img {
width: 100%;
}
.img-mask {
-webkit-mask-box-image: url('http://imgh.us/mask_3.svg');
mask-border: url('http://imgh.us/mask_3.svg');
clip-path: url(#mask);
}
<img src="http://gto-live.com/wp-content/uploads/2015/04/charm-elegance-colorful-sofa-living-room-decor-718x718.jpg" class="img-mask">
<svg preserveAspectRatio="xMidYMid" width="700" height="700" viewBox="0 0 700 700">
<clipPath id="mask" clipPathUnits="objectBoundingBox">
<path transform="scale(0.0014)" d="M718.004,358.999 C718.004,160.726 557.272,-0.007 358.998,-0.007 C160.725,-0.007 -0.007,160.726 -0.007,358.999 C-0.007,557.272 160.725,718.005 358.998,718.005 C557.272,718.005 718.004,557.272 718.004,358.999 Z"/>
</clipPath>
</svg>
Here I've scaled the path to correct the units 0.0014 is roughly 1 / 700

Related

Clip-path is not working. Did I miss something?

I'm trying to use clip-path in CSS but it's not working. I've watched plenty tutorials by now and it seems simple, but it's just not working and doesn't appear to be trying to work. I should also mention I'm using React--which, whenever I've run into issues recently, it's caused by React.
To my own knowledge, I've done everything I'm supposed to. The SVG is loaded in the DOM as a clipPath tag, with a unique ID and I reference that ID in the CSS.
Please help. Thanks.
SVG Blob (imported in the DOM via import {ReactComponent as LIST_ICON} from ...:
<svg className="list-icon" viewBox="0 0 64 48">
<path d="M12.343,2V10.343c0,1.104-.896,2-2,2H2c-1.105,0-2-.896-2-2V2C0,.896,.895,0,2,0H10.343c1.104,0,2,.896,2,2Zm-2,15.829H2c-1.105,0-2,.896-2,2v8.343c0,1.104,.895,2,2,2H10.343c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H2c-1.105,0-2,.896-2,2v8.343c0,1.105,.895,2,2,2H10.343c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2ZM62,0H19.828c-1.104,0-2,.896-2,2V10.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2V2c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.105,.896,2,2,2H62c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2Z" />
<symbol id="list-icon" viewBox="0 0 64 48">
<path d="M12.343,2V10.343c0,1.104-.896,2-2,2H2c-1.105,0-2-.896-2-2V2C0,.896,.895,0,2,0H10.343c1.104,0,2,.896,2,2Zm-2,15.829H2c-1.105,0-2,.896-2,2v8.343c0,1.104,.895,2,2,2H10.343c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H2c-1.105,0-2,.896-2,2v8.343c0,1.105,.895,2,2,2H10.343c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2ZM62,0H19.828c-1.104,0-2,.896-2,2V10.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2V2c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.105,.896,2,2,2H62c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2Z" />
</symbol>
<defs>
<clipPath id="list-icon-clip" clipPathUnits="objectBoundingBox">
<path d="M12.343,2V10.343c0,1.104-.896,2-2,2H2c-1.105,0-2-.896-2-2V2C0,.896,.895,0,2,0H10.343c1.104,0,2,.896,2,2Zm-2,15.829H2c-1.105,0-2,.896-2,2v8.343c0,1.104,.895,2,2,2H10.343c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H2c-1.105,0-2,.896-2,2v8.343c0,1.105,.895,2,2,2H10.343c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2ZM62,0H19.828c-1.104,0-2,.896-2,2V10.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2V2c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.105,.896,2,2,2H62c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2Z" />
</clipPath>
</defs>
</svg>
More specifically:
<defs>
<clipPath id="list-icon-clip" clipPathUnits="objectBoundingBox">
<path d="M12.343,2V10.343c0,1.104-.896,2-2,2H2c-1.105,0-2-.896-2-2V2C0,.896,.895,0,2,0H10.343c1.104,0,2,.896,2,2Zm-2,15.829H2c-1.105,0-2,.896-2,2v8.343c0,1.104,.895,2,2,2H10.343c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H2c-1.105,0-2,.896-2,2v8.343c0,1.105,.895,2,2,2H10.343c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2ZM62,0H19.828c-1.104,0-2,.896-2,2V10.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2V2c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.104,.896,2,2,2H62c1.104,0,2-.896,2-2v-8.343c0-1.104-.896-2-2-2Zm0,17.829H19.828c-1.104,0-2,.896-2,2v8.343c0,1.105,.896,2,2,2H62c1.104,0,2-.895,2-2v-8.343c0-1.104-.896-2-2-2Z" />
</clipPath>
</defs>
JSX (HTML):
<button className="hint-cache-toggle" onClick={e => e.preventDefault()}>
CSS:
#answer-input .hint-cache-toggle {
position: absolute;
inset: auto 0 0 auto;
border: none;
width: 64px;
height: 48px;
clip-path: url("#list-icon-clip");
/* -webkit-clip-path: url("#list-icon-clip"); */
background-color: black;
}

SVG is not talking FULL 'width' of container

I am trying to make my svg full with width of the screen (container) but its not working. Could someone help me - please?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320" preserveAspectRatio="none" class="first-svg">
<path fill="#27187F" fill-opacity="1" d="M0,192L60,170.7C120,149,240,107,360,106.7C480,107,600,149,720,170.7C840,192,960,192,1080,170.7C1200,149,1320,107,1380,85.3L1440,64L1440,320L1380,320C1320,320,1200,320,1080,320C960,320,840,320,720,320C600,320,480,320,360,320C240,320,120,320,60,320L0,320Z"></path>
</svg>
css
.first-svg {
width: 100%;
height: 200px;
}
I am suing bootstrap for this project I am working (only for practice)
I have done the following and it seemed to work - I added the SVG inside a row.
<div class="row">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320" preserveAspectRatio="none" class="first-svg">
<path fill="#27187F" fill-opacity="1" d="M0,192L60,170.7C120,149,240,107,360,106.7C480,107,600,149,720,170.7C840,192,960,192,1080,170.7C1200,149,1320,107,1380,85.3L1440,64L1440,320L1380,320C1320,320,1200,320,1080,320C960,320,840,320,720,320C600,320,480,320,360,320C240,320,120,320,60,320L0,320Z"></path>
</svg>
</div>

Inline SVG path not working

I've been trying to get a inline svg clipping path to work but can't quite figure out why it isn't.
The first div in the snippet is the one that isn't working (#myClip)
The second is what it should look like.
The third div is all the same code but with a different path that does work.
So what's wrong with my first one? Any help is greatly appreciated.
#myDiv {
background: Red;
min-height: 400px;
-webkit-clip-path: url(#myClip);
clip-path: url(#myClip);
}
/* Div with a different clipping path that I don't want */
#myDiv-two {
background: Red;
min-height: 400px;
-webkit-clip-path: url(#myClipTwo);
clip-path: url(#myClipTwo);
}
<h2>Div with clip path that's not working</h2>
<div id="myDiv">
<svg width="0" height="0">
<clipPath id="myClip" clipPathUnits="objectBoundingBox">
<path d="M0,0V678.48s138.59-46.14,279.3-48.31,256.56,4.64,326.86,13.44S941.94,700.69,1115,688.48s205.35-15.91,325-40.13V0Z"/>
</clipPath>
</svg>
</div>
<h2>Original SVG</h2>
<svg id="Layer_2" data-name="Layer 2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 690.45"><defs></defs><path d="M0,0V678.48s138.59-46.14,279.3-48.31,256.56,4.64,326.86,13.44S941.94,700.69,1115,688.48s205.35-15.91,325-40.13V0Z" fill="#F34862"/></svg>
<h2>Div with different clipping path</h2>
<div id="myDiv-two">
<svg width="0" height="0">
<clipPath id="myClipTwo" clipPathUnits="objectBoundingBox">
<path d="M0,0 1,0 1,0.9 C 1,0.9, 0.77,1, 0.5,1 0.23,1, 0,0.9,0,0.9z"/>
</clipPath>
</svg>
</div>
You have defined your clipping path to be clipPathUnits="objectBoundingBox". When you do that, the coordinates need to be between 0 and 1. (0,0) represents the top left of the element the clip is being applied to. And (1,1) represents the bottom right.
However your coordinates are much bigger than that. For example your initial line (M0,0V678.48) is from (0,0) to (0,678.48).
I would love an explanation as to why the coordinates need to be between 0 and 1 but I have made an SVG by hand instead of exporting from illustrator that is fairly close to my original using coordinates between 0 and 1 so it will work.
#myDiv {
background: Red;
min-height: 400px;
-webkit-clip-path: url(#myClip);
clip-path: url(#myClip);
}
<h2>New Clip path</h2>
<div id="myDiv">
<svg width="0" height="0">
<clipPath id="myClip" clipPathUnits="objectBoundingBox">
<path d="M0,0 1,0 1,0.86 C 1.1,.85, 0.9,1, 0.7,1 0.4,1, 0.3,0.8,0,1z"/>
</clipPath>
</svg>
</div>
<h2>Original SVG</h2>
<svg id="Layer_2" data-name="Layer 2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 690.45"><defs></defs><path d="M0,0V678.48s138.59-46.14,279.3-48.31,256.56,4.64,326.86,13.44S941.94,700.69,1115,688.48s205.35-15.91,325-40.13V0Z" fill="#F34862"/></svg>

CSS Clip-path positioning issues

I have created a fairly simple shape using an SVG element which is then put into my CSS using clip-path. It should make the corners rounded for me but for some reason only 1 of the corners does the effect perfectly.
This is the shape:
<svg height="500" width="500">
<path fill="#555555" d="M50,0 L450,0 Q500,0 500,50 L500,400 Q500,450 450,450 L200,450 L175,500 L150,450 L50,450 Q0,450 0,400 L0,50 Q0,0 50,0z" />
</svg>
This is what happens when i use it as a clip-path
body {
background: #555;
}
img {
clip-path: url(#svgPath);
-webkit-clip-path: url(#svgPath);
}
<svg height="0" width="0">
<defs>
<clipPath id="svgPath">
<path fill="#FFFFFF" d="M50,0 L450,0 Q500,0 500,50 L500,400 Q500,450 450,450 L200,450 L175,500 L150,450 L50,450 Q0,450 0,400 L0,50 Q0,0 50,0z" />
</clipPath>
</defs>
</svg>
<img src="https://dummyimage.com/500" />
It seems to work perfectly within FireFox but shows the corners aren't cut correctly in Chrome apart from the bottom right corner.
The default units for the clip-path is userSpaceOnUse and this seems to calculate the coordinates of the path with reference to the root element. This is the reason why the clip-path seems like it is producing an incorrect output. Nullifying the margin and padding on the root element or absolutely positioning the element (like in the below snippet) should solve the issue.
body {
background: #555;
}
img {
position: absolute;
top: 0px;
left: 0px;
clip-path: url(#svgPath);
-webkit-clip-path: url(#svgPath);
}
<svg height="0" width="0">
<defs>
<clipPath id="svgPath">
<path fill="#FFFFFF" d="M50,0 L450,0 Q500,0 500,50 L500,400 Q500,450 450,450 L200,450 L175,500 L150,450 L50,450 Q0,450 0,400 L0,50 Q0,0 50,0z" />
</clipPath>
</defs>
</svg>
<img src="http://lorempixel.com/500/500/" />
However, in a real life scenario the actual element that has to be clipped could be present anywhere within the body and hence I think it is a much better approach to use the objectBoundingBox as the units like in the below snippet:
body {
background: #555;
}
img {
clip-path: url(#svgPath);
-webkit-clip-path: url(#svgPath);
}
<svg height="0" width="0">
<defs>
<clipPath id="svgPath" clipPathUnits="objectBoundingBox">
<path fill="#FFFFFF" d="M0.1,0 L0.9,0 Q1,0 1,0.1 L1,0.8 Q1,0.9 0.9,0.9 L0.4,0.9 L0.35,1 L0.3,0.9 L0.1,0.9 Q0,0.9 0,0.8 L0,0.1 Q0,0 0.1,0z" />
</clipPath>
</defs>
</svg>
<img src="https://dummyimage.com/500" />
As mentioned in the question itself, this behavior is visible only in Chrome and not Firefox for reasons unknown to me. Firefox produces an output similar to the expected one even when (a) extra padding + margin is added to the body and (b) when the image itself is wrapped inside another container which also has padding + margin.
The only case where Firefox's output matches with Chrome is when a padding is added directly to the img tag itself. I believe this happens because padding is part of the element and thus affects the coordinates.

Inline SVG element with fluid height

I want to include an inline SVG for a project. I can dump the SVG generated from inkscape into a HTML document and it displays. This is a responsive site, so I want the element to resize fluidly. I keep running into an issue with the height of the SVG element being the height of the document at page load.
<div id="svg">
<svg 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" version="1.1" width="100%" preserveAspectRatio="xMinYMin meet" viewBox="0 0 500 500" id="svg4107">
<defs id="defs4109"></defs>
<g transform="translate(0,-552.3622)" id="layer1">
<path d="m 0,552.3622 500,0 c -505.952711,505.95 -225.92979,225.93 -500,500 z" id="rect3260" style="fill:#ffffff;fill-opacity:1;stroke:none"></path>
</g>
</svg>
</div>
The css is like this:
body {
padding: 0;
margin: 0;
}
#svg {
width: 50%;
background-color: red;
}
With images I'm used to setting a width as a percentage and height:auto, but that doesn't seem to work with inline SVG.
This a simplified example, the production situation is a much more complicated SVG - but this illustrates the issue well. This needs to be a HTML/CSS solution (no Javascript).
Here is a fiddle of the issue
Note: As pointed out in the comments this seems to be a blink/webkit issue - firefox works fine.

Resources