Home > Web Front-end > CSS Tutorial > How to Apply Gradients to SVG Elements Using CSS?

How to Apply Gradients to SVG Elements Using CSS?

DDD
Release: 2024-10-30 07:49:03
Original
1005 people have browsed it

How to Apply Gradients to SVG Elements Using CSS?

Applying Gradients to SVG Elements Using CSS

Implementing a gradient on an SVG rect element requires utilizing CSS in conjunction with SVG's powerful gradient capabilities. The fill attribute of the rect element allows for gradient application.

To create a linear gradient, add the following CSS rule to your stylesheet:

rect {
    cursor: pointer;
    shape-rendering: crispEdges;
    fill: url(#MyGradient);
}
Copy after login

Now, within the SVG file, define the linear gradient using the and elements:

<defs>
    <linearGradient id="MyGradient">
        <stop offset="5%" stop-color="#F60" />
        <stop offset="95%" stop-color="#FF6" />
    </linearGradient>
</defs>
Copy after login

Here, the gradient spans from #F60 at 5% to #FF6 at 95% along the element's length. You can adjust the colors and offset percentages to customize the gradient.

Finally, reference the gradient using the fill attribute in the rect element:

<rect width="100" height="50" fill="url(#MyGradient)"/>
Copy after login

This approach enables you to apply beautiful and dynamic gradients to your SVG elements using CSS, enhancing the visual appeal and user experience of your web applications.

The above is the detailed content of How to Apply Gradients to SVG Elements Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template