How can I get translateZ to work in svg groups? - css

I have created a svg image animation. I am trying to make some nice effects with mouseover. I want to move the svg depending on mouse position. The rotate part works fine so far. But I also want to move the <g> inside the svg in z-axis (popout) so they comes out in "different layers". Should be possible with translateZ(). But I can not get it to work.
const container = document.querySelector('.container');
const image = document.querySelector('.image');
const sun = document.querySelector('.sun');
const cloudBehind = document.querySelector('.cloud-center');
const cloudLeft = document.querySelector('.cloud-left');
const cloudRight = document.querySelector('.cloud-right');
const devideAxis = 10;
container.addEventListener('mousemove', e => {
let xAxis = ~(window.innerWidth / 2 - e.pageX) / devideAxis;
let yAxis = (window.innerHeight / 2 - e.pageY) / devideAxis;
image.style.transform = `rotateY(${xAxis}deg) rotateX(${yAxis}deg)`;
});
container.addEventListener('mouseenter', e => {
image.style.transition = 'none';
//popout
cloudLeft.style.transform = 'translateZ(400px)'
})
container.addEventListener('mouseleave', e => {
image.style.transition = 'all 0.5s ease';
image.style.transform = `rotateY(0deg) rotateX(0deg)`
cloudLeft.style.transform = 'translateZ(0)'
})
body,html {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
perspective: 300px;
}
.container {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.image {
width: 50%;
transform-style: preserve-3d;
}
.cloud-left {
transition: all .5s ease-out
}
.cloud-left,
.cloud-right,
.cloud-center {
transition: transform .5s;
}
.cloud-center{
fill:url(#CLOUD-CENTER);
}
.cloud-right{
fill:url(#CLOUD-FRONT);
}
.cloud-left{
fill:url(#CLOUD-FRONT);
}
.sun{
fill:url(#SUN);
}
.shine{
fill:url(#SHINE);
}
<div class="container">
<div class="image">
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 500 250" >
<g id="wheather">
<radialGradient id="SHINE" cx="100" cy="100" r="100" gradientUnits="userSpaceOnUse">
<stop offset="0.7568" style="stop-color:#FFFFFF"/>
<stop offset="1" style="stop-color:#FFF3AB"/>
</radialGradient>
<linearGradient id="SUN" gradientUnits="userSpaceOnUse" x1="27.6083" y1="98.0519" x2="169.5758" y2="98.0519">
<stop offset="0" style="stop-color:#FFF3AB"/>
<stop offset="1" style="stop-color:#FFF2C0"/>
</linearGradient>
<linearGradient id="CLOUD-CENTER" gradientUnits="userSpaceOnUse" x1="0" y1="66.2549" x2="288.0558" y2="66.2549">
<stop offset="0" style="stop-color:#8EABBF"/>
<stop offset="1" style="stop-color:#ECF1F7"/>
</linearGradient>
<linearGradient id="CLOUD-FRONT" gradientUnits="userSpaceOnUse" x1="0" y1="62.1091" x2="294.4617" y2="62.1091">
<stop offset="5.759445e-07" style="stop-color:#FFFFFF"/>
<stop offset="1" style="stop-color:#C5D6E5"/>
</linearGradient>
<!-- SUN -->
<g transform="translate(250 0)" class="sun">
<path class="shine" d="M162.4,147.8l22,0.9l-9.8-15.5c-0.2-0.5-0.5-1-0.8-1.5v0c-3.1-4.9-1.6-11.1,3-14.2l20.3-8.1l-14.9-9.9
c-0.5-0.5-1-0.9-1.5-1.3h0c-4.9-3.3-6-9.8-2.8-14.5l15-15.3l-17.3-2.9c-0.7-0.3-1.4-0.5-2.2-0.6h0c-6-1-9.7-6.9-8.4-12.6l7.3-19.6
l-16.9,4.3c-0.7,0-1.5,0.1-2.2,0.3h0c-6.3,1.6-12.5-2.9-12.9-9.4v0c0-0.2,0-0.3,0-0.4l-1.2-19.1l-13.8,10.9
c-0.6,0.3-1.1,0.7-1.7,1.1c-5.1,4-12.6,2.4-15.6-3.3l0,0c-0.2-0.5-0.5-0.9-0.8-1.3L98.6,0l-8.4,16c-0.3,0.4-0.6,0.9-0.8,1.3v0
c-3,5.8-10.5,7.4-15.6,3.3h0c-0.5-0.4-1.1-0.8-1.7-1.1L58.3,8.6l-1.2,19.1c0,0.1,0,0.3,0,0.4v0c-0.4,6.5-6.6,11-12.9,9.4h0
c-0.8-0.2-1.5-0.3-2.2-0.3l-16.9-4.3l7.3,19.6c1.3,5.7-2.4,11.6-8.4,12.6h0c-0.8,0.1-1.5,0.3-2.2,0.6L4.3,68.5l15,15.3
c3.2,4.7,2.1,11.2-2.8,14.5c-0.6,0.4-1.1,0.8-1.5,1.3L0,109.5l20.3,8.1c4.6,3,6.1,9.3,3,14.2l0,0c-0.3,0.5-0.6,1-0.8,1.5l-9.8,15.5
l22-0.9c5.4,0.9,9.3,6,8.4,11.7v0c0,0.3-0.1,0.6-0.1,0.9l-2.8,19l19.7-9.8c5-1.3,10.3,1.3,12.2,6.1l5.7,20.4l13.8-16.7
c3.9-3.4,9.7-3.4,13.5,0l13.8,16.7l5.7-20.4c2-4.9,7.3-7.4,12.2-6.1l19.7,9.8l-2.8-19c0-0.3,0-0.6-0.1-0.9v0
C153.2,153.8,157,148.7,162.4,147.8z">
<animateTransform
attributeName="transform"
attributeType="XML"
type="rotate"
from="0 100 100"
to="360 100 100"
dur="80s"
repeatCount="indefinite"
/>
</path>
<circle class="sun-inside" cx="98.6" cy="98.1" r="71"/>
</g>
<!-- / SUN -->
<!-- CLOUD BEHIND -->
<g transform="translate(70 70)">
<path class="cloud-center" style="transform: " d="M261.5,79.4c-3.4,0-6.6,0.6-9.6,1.8c-1.9-5.5-6-10-11.3-12.5c0-0.2,0-0.3,0-0.5c0-27.1-21.9-49-49-49
c-0.6,0-1.2,0-1.8,0c-9-11.7-23.1-19.2-39-19.2c-18,0-33.8,9.7-42.3,24.1c-3.8-3.1-8.6-5-13.8-5c-11.1,0-20.3,8.4-21.6,19.2
c-3.7-1.1-7.7-1.7-11.7-1.7C39,36.6,21,54.6,21,76.9c0,2.8,0.3,5.6,0.9,8.3C9.6,86.1,0,96.3,0,108.8c0,13.1,10.6,23.7,23.7,23.7
h237.8c14.7,0,26.6-11.9,26.6-26.6S276.2,79.4,261.5,79.4z">
<animateMotion
path="M50,0 -50,0 50,0 z"
dur="80s"
repeatCount="indefinite"
/>
</path>
</g>
<!-- / CLOUD BEHIND -->
<!-- CLOUD LEFT -->
<g transform="translate(10 126)">
<path class="cloud-left" d="M270.4,76.2C270.4,76.2,270.4,76.2,270.4,76.2c-1.1-22-19.3-39.6-41.6-39.6c-5.1,0-9.9,0.9-14.4,2.6
C206.9,16.4,185.5,0,160.2,0c-20.6,0-38.6,10.9-48.6,27.3c-4.6-1.5-9.4-2.3-14.5-2.3c-23.4,0-42.8,16.9-46.7,39.1
c-5.1-3.3-11.2-5.2-17.7-5.2C14.6,58.9,0,73.5,0,91.6c0,18,14.6,32.7,32.7,32.7h237.8c13.3,0,24-10.8,24-24
C294.5,86.9,283.7,76.2,270.4,76.2z M194.9,102.4h-0.2c0.1,0,0.1-0.1,0.2-0.1C194.9,102.4,194.9,102.4,194.9,102.4z">
<animateMotion
path="M30,0 0,0 00,0 30,0 z"
dur="15s"
repeatCount="indefinite"
/>
</path>
</g>
<!-- / CLOUD LEFT -->
<!-- CLOUD RIGHT -->
<g transform="translate(260 140)">
<path class="cloud-right" d="M228.8,73.7c0-19.9-16.1-36-36-36c-3.5,0-6.8,0.5-10,1.4c-4.4-18.7-21.2-32.7-41.3-32.7c-10.7,0-20.5,4-28,10.6
C104.6,6.6,91.4,0,76.7,0C49.9,0,28.3,21.7,28.3,48.4c0,1.6,0.1,3.2,0.2,4.8c-0.1,0-0.2,0-0.2,0C12.7,53.2,0,65.9,0,81.5
s12.7,28.3,28.3,28.3H198v-0.4C215.4,106.8,228.8,91.8,228.8,73.7z">
<animateMotion
path="M-40,0 0,0 -100,0 -40,0 z"
dur="60s"
repeatCount="indefinite"
/>
</path>
</g>
<!-- / CLOUD RIGHT -->
</g>
</svg>
</div>
</div>
Main code things:
So I have body, which has perspective: 300px;. Then I have .image which has transform-style: preserve-3d; and inside that element I have the <g>'s with classes.
As an example I have a .cloud-left.
So the structure is basically:
<body>
<div class="container">
<div class="image">
<svg>
<g id="wheather">
<g class="cloud-left">...</g>
...
</g>
</svg>
</div>
</div>
</body>
In javascript I have this (Shortned, to see all code, please see the fiddle above):
const cloudLeft = document.querySelector('.cloud-left'); // get the element
container.addEventListener('mouseenter', e => {
//popout
cloudLeft.style.transform = 'translateZ(400px)'
})
container.addEventListener('mouseleave', e => {
//popback
cloudLeft.style.transform = 'translateZ(0)'
})
But this does not work as I want.
I have also tried to set transform-style: preserve-3d; on <g id="weather"> which is the direct parent to the other <g>'s, but no difference.
I am not getting the <g> to move/popout in z-axis. Am I missing something in the svg or does this not work in svg groups?
I have another example where this works, but it is not an svg: https://jsfiddle.net/saturday99/94o5j8ts/

Ok, so as I suspected. It worked with svg's instead of groups/paths. Paths/groups are 2D only, as figured out. But svg's are possible to make 3D and use translateZ on. Down here is the answer for the future ones looking for answer. Ps. The code could be cleaned up a bit, but it works.
So the basic structure is now split into many svg's instead of one svg containing many groups:
<body>
<div class="container">
<div class="image">
<svg class="sun">
<g>
...
</g>
</svg>
<svg class="cloud-left">
<g>
...
</g>
</svg>
...
</div>
</div>
</body>
And here is the "finale" code and solution: https://jsfiddle.net/saturday99/fkrh6bzm/. Feel free to fork the code.

Related

How to handle responsive svg type image in side div tag

i am supposed to create this card using CSS. following you can see Figma UI design of it.
following you can see my code part of it (I have used one mudblazor icon for this )
<div class="dashboard-tile-card">
<div class="dashboard-tile-icon ">
<img src="/img/moneybag.svg" width="50%" />
</div>
<div class="dashboard-card-action">
<a class="text-white">Go to the Dashboard</a><MudIcon Style="#($"color:{Colors.Grey.Lighten5};")" Icon="#Icons.Filled.ArrowForward"></MudIcon>
</div>
</div>
moneybag.svg
<svg width="120" height="120" viewBox="0 0 120 120" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g filter="url(#filter0_b_0_1)">
<circle cx="60" cy="60" r="60" fill="url(#paint0_radial_0_1)"/>
<circle cx="60" cy="60" r="58.5" stroke="url(#paint1_radial_0_1)" stroke-width="3"/>
<circle cx="60" cy="60" r="58.5" stroke="url(#paint2_radial_0_1)" stroke-width="3"/>
<circle cx="60" cy="60" r="58.5" stroke="url(#paint3_linear_0_1)" stroke-width="3"/>
</g>
<rect x="30" y="30" width="60" height="60" fill="url(#pattern0)"/>
<defs>
<filter id="filter0_b_0_1" x="-42" y="-42" width="204" height="204" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feGaussianBlur in="BackgroundImage" stdDeviation="21"/>
<feComposite in2="SourceAlpha" operator="in" result="effect1_backgroundBlur_0_1"/>
<feBlend mode="normal" in="SourceGraphic" in2="effect1_backgroundBlur_0_1" result="shape"/>
</filter>
<pattern id="pattern0" patternContentUnits="objectBoundingBox" width="1" height="1">
<use xlink:href="#image0_0_1" transform="scale(0.003125)"/>
</pattern>
<radialGradient id="paint0_radial_0_1" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(1.07937 3.57024) rotate(44.0741) scale(162.963 260.636)">
<stop stop-color="white" stop-opacity="0.4"/>
<stop offset="1" stop-color="white" stop-opacity="0"/>
</radialGradient>
<radialGradient id="paint1_radial_0_1" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(1.07937 3.57024) rotate(44.0741) scale(162.963 260.636)">
<stop stop-color="#D8D8D8" stop-opacity="0"/>
<stop offset="1" stop-color="#D8D8D8"/>
</radialGradient>
<radialGradient id="paint2_radial_0_1" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(1.65079 116.331) rotate(-44.0635) scale(162.139 259.045)">
<stop stop-color="#D8D8D8" stop-opacity="0"/>
<stop offset="1" stop-color="#D8D8D8"/>
</radialGradient>
<linearGradient id="paint3_linear_0_1" x1="2.22222" y1="3.27273" x2="141.088" y2="66.7179" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0.9"/>
<stop offset="1" stop-color="white" stop-opacity="0"/>
</linearGradient>
<image id="image0_0_1" width="320" height="320" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAFACAYAAADNkKWqAAAZtUlEQVR4nO3df2id133H8XcT0Z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bJcA03QO8d8XnBoEJh1ikNzN0LhRQ+10VVaQEWB0bgC3r/J3Wesb1aK1jZ92UdWrtlljrGpUtD5UbJcA83RvgGjexPCvu7Ri2m6Hlzeaf1/b4ufvChyYpUwKUfjVIZ0KntZdVREREuvF/rw9J5TJv+e4AAAAASUVORK5CYII="/>
</defs>
</svg>
CSS file
.dashboard-tile-card {
width: 100%;
height: 100%;
background: #FFFFFF;
border: 1.5px solid #c2c2c2;
border-radius: 12px;
}
.dashboard-tile-icon {
display: flex;
justify-content: center;
align-items: center;
height: 75%;
}
.dashboard-card-action {
background: #063b71ff;
height: 25%;
border-radius: 0 0 12px 12px;
display: flex;
justify-content: center;
align-items: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
cursor: pointer;
}
I am not much good at the CSS side.i think I am doing some wrong CSS rules for this. my issue is I can't maintain the responsiveness of the image .some screen size svg image will be very small and for some screen sizes, this image will break the margin of the outside border.
please does anybody know how to do this very smart way instead of this? it should be responsive to whatever screen sizes
Try import this icon from figma in svg but inspect source code with browser. Then copy this small code and paste it in dashboard-tile-icon. Next what you want to do is removing height and width in svg tag and and give it in css using % if you want it responsive but I like this type of icons always keeps in between 50px and 80px.

How to crop an image width svg?

How to crop an image with path in this svg?
<svg width="1440" height="568" viewBox="0 0 1440 568" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0H1440V481.821L720 568L0 481.821V0Z" fill="url(#paint0_linear)"/>
<defs>
<linearGradient id="paint0_linear" x1="720" y1="607.026" x2="720" y2="35.2649" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0"/>
<stop offset="1" stop-color="#0997FF" stop-opacity="0.56"/>
</linearGradient>
</defs>
</svg>
I want to set linearGradient of svg on an image and extra space in bottom of image be as same as background color of body(navy blue):
<div class="bg"></div>
.bg {
width: 100vw;
min-height: 500px;
background: url("../assets/images/mySvg.svg"),
url("../assets/images/myImage.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
What I have:
What I want:
Put the image into the SVG.
Combine the image and the gradient into an SVG group (<g>)
Then clip that group with an SVG <clipPath> defined using your original path.
Put the SVG inside the <div>
body {
background-color: linen;
}
.bg {
width: 100vw;
min-height: 500px;
}
<div class="bg">
<svg width="100%" viewBox="0 0 1440 568">
<defs>
<clipPath id="clip">
<path d="M0 0H1440V481.821L720 568L0 481.821V0Z"/>
</clipPath>
<linearGradient id="paint0_linear" x1="720" y1="607.026" x2="720" y2="35.2649" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0"/>
<stop offset="1" stop-color="#0997FF" stop-opacity="0.56"/>
</linearGradient>
</defs>
<g clip-path="url(#clip)">
<image xlink:href="http://placekitten.com/1440/568" width="1440" height="568"/>
<rect width="1440" height="568" fill="url(#paint0_linear)"/>
</g>
</svg>
<p>More content here.</p>
</div>

How to set the size of the handle in a QSlider?

I'm trying to add some style to a QSlider I want to use a custom image as the handle that the user drags back and forth. While I've figured out how to use style sheets to have a custom icon drawn where the handle should be, the image is being drawn much too small and I cannot figure out how to make it larger.
Setting width and height seem to do nothing. I've tried using image, border-image and background-image, but none give me the ability to set the size of the handle image. Does anyone know how to do this?
This is the style sheet that I've been adding to my QSlider in QtDesigner:
QSlider::handle:vertical {
image: url(:/data/icons/mixer-slider-handle.svg);
width:64px;
height:64px;
}
This is the SVG:
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="30"
height="45"
viewBox="0 0 7.9374997 11.90625"
version="1.1"
id="svg8"
>
<defs
id="defs2">
<linearGradient
id="linearGradient844"
inkscape:collect="always">
<stop
id="stop840"
offset="0"
style="stop-color:#cecece;stop-opacity:1;" />
<stop
id="stop842"
offset="1"
style="stop-color:#ffffff;stop-opacity:1" />
</linearGradient>
<linearGradient
inkscape:collect="always"
id="linearGradient824">
<stop
style="stop-color:#cecece;stop-opacity:1;"
offset="0"
id="stop820" />
<stop
style="stop-color:#ffffff;stop-opacity:1"
offset="1"
id="stop822" />
</linearGradient>
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient824"
id="linearGradient826"
x1="-3.9103179"
y1="297.24557"
x2="-3.8304768"
y2="285.38882"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(0.8683302,0,0,0.96503255,7.3827223,9.9179025)" />
<linearGradient
inkscape:collect="always"
xlink:href="#linearGradient844"
id="linearGradient838"
gradientUnits="userSpaceOnUse"
gradientTransform="matrix(0.86322913,0,0,0.81935486,7.5301966,52.317886)"
x1="-3.8119318"
y1="285.99686"
x2="-3.7885454"
y2="296.82458" />
</defs>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-285.09375)">
<rect
style="fill:url(#linearGradient826);fill-opacity:1;stroke:#0e0e0e;stroke-width:0.1373108;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect818"
width="5.157938"
height="11.397007"
x="1.453124"
y="285.36124"
ry="2.866178" />
<rect
style="opacity:1;fill:url(#linearGradient838);fill-opacity:1;stroke:none;stroke-width:0.12615089;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect828"
width="4.6027613"
height="8.9100981"
x="1.7161824"
y="286.60291"
ry="2.184411" />
<path
style="fill:none;stroke:#000000;stroke-width:0.24780074px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 1.8804469,291.0695 4.3266789,0.047"
id="path846"
inkscape:connector-curvature="0" />
</g>
</svg>
Looks like the answer is to set the margin attribute for both the groove and the handle to reflect the size of the handle you are using.
This worked for me:
.QSlider::groove:vertical {
border: 1px solid #111;
background-color: #333;
width: 6px;
margin: 24px 12px;
}
.QSlider::handle:vertical {
image: url(:/data/icons/mixer-slider-handle.svg);
margin: -24px -12px;
height: -30px;
}

Rotating the stroke but not the filling of a svg circle

I've followed this tuto in order to implement a progress ring for my Vue application. I still have an extra requirement: fill the circle with an image. That's the point I've reached to, using a pattern (copy pasted from my browser in order to avoid adding the extra complexity of Vue property evaluations):
HTML
<div>
<svg
height="600"
width="600">
<defs>
<pattern id="service" x="0%" y="0%" height="100%" width="100%"
viewBox="0 0 100 100">
<image x="5" y="5" width="90" height="90" href="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Selfie_icon.svg/2000px-Selfie_icon.svg.png"></image>
</pattern>
<linearGradient id="gradient">
<stop offset="0%" stop-color="#f6921e"/>
<stop offset="100%" stop-color="#f6921e88"/>
</linearGradient>
</defs>
<circle
stroke="url(#gradient)"
stroke-dasharray="1709.0264035528476 1709.0264035528476"
style="stroke-dashoffset: 512.708;"
stroke-linecap="round"
stroke-width="14"
fill="url(#service)"
r="272"
cx="300"
cy="300"
/>
</svg>
</div>
CSS
circle {
transition: stroke-dashoffset 0.35s;
transform: rotate(-90deg);
transform-origin: 50% 50%;
}
However, I find that rotating the circle obviously rotates its filling too.
Is there any way to overcome this problem? Why does the example rotate the entire SVG to make the circle gap be in the upside?
Codepen
You can use another circle in your SVG, one for the border and one for the background, then rotate just the circle with the border:
.circle-border {
transition: stroke-dashoffset 0.35s;
transform: rotate(-90deg);
transform-origin: 50% 50%;
}
<!-- begin snippet: js hide: false console: true babel: false -->
<div>
<svg height="600" width="600">
<defs>
<pattern id="service" x="0%" y="0%" height="100%" width="100%" viewBox="0 0 100 100">
<image x="5" y="5" width="90" height="90" href="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Selfie_icon.svg/2000px-Selfie_icon.svg.png"></image>
</pattern>
<linearGradient id="gradient">
<stop offset="0%" stop-color="#f6921e"/>
<stop offset="100%" stop-color="#f6921e88"/>
</linearGradient>
</defs>
<circle
stroke="url(#gradient)"
stroke-dasharray="1709.0264035528476 1709.0264035528476"
style="stroke-dashoffset: 512.708;"
stroke-linecap="round"
stroke-width="14"
fill="transparent"
class="circle-border"
r="272"
cx="300"
cy="300"
/>
<circle
stroke-width="0"
fill="url(#service)"
class="circle-bg"
r="272"
cx="300"
cy="300"
/>
</svg>
</div>

SVG gradient using CSS

I'm trying to get a gradient applied to an SVG rect element.
Currently, I'm using the fill attribute. In my CSS file:
rect {
cursor: pointer;
shape-rendering: crispEdges;
fill: #a71a2e;
}
And the rect element has the correct fill color when viewed in the browser.
However, I'd like to know if I can apply a linear gradient to this element?
Just use in the CSS whatever you would use in a fill attribute.
Of course, this requires that you have defined the linear gradient somewhere in your SVG.
Here is a complete example:
rect {
cursor: pointer;
shape-rendering: crispEdges;
fill: url(#MyGradient);
}
<svg width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
<style type="text/css">
rect{fill:url(#MyGradient)}
</style>
<defs>
<linearGradient id="MyGradient">
<stop offset="5%" stop-color="#F60" />
<stop offset="95%" stop-color="#FF6" />
</linearGradient>
</defs>
<rect width="100" height="50"/>
</svg>
2019 Answer
With brand new css properties you can have even more flexibility with variables aka custom properties
.shape {
width:500px;
height:200px;
}
.shape .gradient-bg {
fill: url(#header-shape-gradient) #fff;
}
#header-shape-gradient {
--color-stop: #f12c06;
--color-bot: #faed34;
}
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" class="shape">
<defs>
<linearGradient id="header-shape-gradient" x2="0.35" y2="1">
<stop offset="0%" stop-color="var(--color-stop)" />
<stop offset="30%" stop-color="var(--color-stop)" />
<stop offset="100%" stop-color="var(--color-bot)" />
</linearGradient>
</defs>
<g>
<polygon class="gradient-bg" points="0,0 100,0 0,66" />
</g>
</svg>
Just set a named variable for each stop in gradient and then customize as you like in css. You can even change their values dynamically with javascript, like:
document.querySelector('#header-shape-gradient').style.setProperty('--color-stop', "#f5f7f9");
Building on top of what Finesse wrote, here is a simpler way to target the svg and change it's gradient.
This is what you need to do:
Assign classes to each color stop defined in the gradient element.
Target the css and change the stop-color for each of those stops using plain classes.
Win!
Some benefits of using classes instead of :nth-child is that it'll not be affected if you reorder your stops. Also, it makes the intent of each class clear - you'll be left wondering whether you needed a blue color on the first child or the second one.
I've tested it on all Chrome, Firefox and IE11:
.main-stop {
stop-color: red;
}
.alt-stop {
stop-color: green;
}
<svg class="green" width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
<linearGradient id="gradient">
<stop class="main-stop" offset="0%" />
<stop class="alt-stop" offset="100%" />
</linearGradient>
<rect width="100" height="50" fill="url(#gradient)" />
</svg>
See an editable example here:
https://jsbin.com/gabuvisuhe/edit?html,css,output
Here is a solution where you can add a gradient and change its colours using only CSS:
// JS is not required for the solution. It's used only for the interactive demo.
const svg = document.querySelector('svg');
document.querySelector('#greenButton').addEventListener('click', () => svg.setAttribute('class', 'green'));
document.querySelector('#redButton').addEventListener('click', () => svg.setAttribute('class', 'red'));
svg.green stop:nth-child(1) {
stop-color: #60c50b;
}
svg.green stop:nth-child(2) {
stop-color: #139a26;
}
svg.red stop:nth-child(1) {
stop-color: #c84f31;
}
svg.red stop:nth-child(2) {
stop-color: #dA3448;
}
<svg class="green" width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
<linearGradient id="gradient">
<stop offset="0%" />
<stop offset="100%" />
</linearGradient>
<rect width="100" height="50" fill="url(#gradient)" />
</svg>
<br/>
<button id="greenButton">Green</button>
<button id="redButton">Red</button>
Thank you everyone,
for all your precise replys.
Using the svg in a shadow dom, I add the 3 linear gradients I need within the svg, inside a .
I place the css fill rule on the web component and the inheritance od fill does the job.
<svg viewbox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">
<path
d="m258 0c-45 0-83 38-83 83 0 45 37 83 83 83 45 0 83-39 83-84 0-45-38-82-83-82zm-85 204c-13 0-24 10-24 23v48c0 13 11 23 24 23h23v119h-23c-13 0-24 11-24 24l-0 47c0 13 11 24 24 24h168c13 0 24-11 24-24l0-47c0-13-11-24-24-24h-21v-190c0-13-11-23-24-23h-123z"></path>
</svg>
<svg height="0" width="0">
<defs>
<linearGradient id="lgrad-p" gradientTransform="rotate(75)"><stop offset="45%" stop-color="#4169e1"></stop><stop offset="99%" stop-color="#c44764"></stop></linearGradient>
<linearGradient id="lgrad-s" gradientTransform="rotate(75)"><stop offset="45%" stop-color="#ef3c3a"></stop><stop offset="99%" stop-color="#6d5eb7"></stop></linearGradient>
<linearGradient id="lgrad-g" gradientTransform="rotate(75)"><stop offset="45%" stop-color="#585f74"></stop><stop offset="99%" stop-color="#b6bbc8"></stop></linearGradient>
</defs>
</svg>
<div></div>
<style>
:first-child {
height:150px;
width:150px;
fill:url(#lgrad-p) blue;
}
div{
position:relative;
width:150px;
height:150px;
fill:url(#lgrad-s) red;
}
</style>
<script>
const shadow = document.querySelector('div').attachShadow({mode: 'open'});
shadow.innerHTML="<svg viewbox=\"0 0 512 512\">\
<path d=\"m258 0c-45 0-83 38-83 83 0 45 37 83 83 83 45 0 83-39 83-84 0-45-38-82-83-82zm-85 204c-13 0-24 10-24 23v48c0 13 11 23 24 23h23v119h-23c-13 0-24 11-24 24l-0 47c0 13 11 24 24 24h168c13 0 24-11 24-24l0-47c0-13-11-24-24-24h-21v-190c0-13-11-23-24-23h-123z\"></path>\
</svg>\
<svg height=\"0\">\
<defs>\
<linearGradient id=\"lgrad-s\" gradientTransform=\"rotate(75)\"><stop offset=\"45%\" stop-color=\"#ef3c3a\"></stop><stop offset=\"99%\" stop-color=\"#6d5eb7\"></stop></linearGradient>\
<linearGradient id=\"lgrad-g\" gradientTransform=\"rotate(75)\"><stop offset=\"45%\" stop-color=\"#585f74\"></stop><stop offset=\"99%\" stop-color=\"#b6bbc8\"></stop></linearGradient>\
</defs>\
</svg>\
";
</script>
The first one is normal SVG,
the second one is inside a shadow dom.
Here is how to set a linearGradient on a target element:
<style type="text/css">
path{fill:url('#MyGradient')}
</style>
<defs>
<linearGradient id="MyGradient">
<stop offset="0%" stop-color="#e4e4e3" ></stop>
<stop offset="80%" stop-color="#fff" ></stop>
</linearGradient>
</defs>

Resources