SVG Properties In CSS Guide | CSS-Tricks (2024)

Home / Guides /

Katherine Kato on

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

Brought to you by DigitalOcean

DigitalOcean has the cloud computing services you need to support your growth at any stage. Get started with a free $200 credit!

SVG Properties In CSS Guide | CSS-Tricks (2)

There are many Scalable Vector Graphics (SVG), but only certain attributes can be applied as CSS to SVG. Presentation attributes are used to style SVG elements and can be used as CSS properties. Some of these attributes are SVG-only while others are already shared in CSS, such as font-size or opacity.

For example, to change the color of a element to red, use the fill property in CSS. The fill attribute is a presentation attribute, therefore it can be used as a CSS property:

circle { fill: red;}

So, with that, let’s take a deep and thorough dive into all of the SVG elements that are available to us as well as the CSS properties for them. We’ll also look at various styling approaches, including general presentational styles and animations.

SVG Elements by Category

The presentation attributes that can be used as CSS properties can be found below. For reference, supported elements will be classified by category. This does not include deprecated elements.

Element TypeElements
Container elements<a>
<defs>
<g>
<marker>
<mask>
<pattern>
<svg>
<switch>
<symbol>
Filter primitive elements<feBlend>
<feColorMatrix>
<feComponentTransfer>
<feComposite>
<feConvolveMatrix>
<feDiffuseLighting>
<feDisplacementMap>
<feFlood>
<feGaussianBlur>
<feImage>
<feMerge>
<feMorphology>
<feOffset>
<feSpecularLighting>
<feTile>
<feTurbulence>
Gradient elements<linearGradient>
<radialGradient>
<stop>
Graphics elements<circle>
<ellipse>
<image>
<line>
<path>
<polygon>
<polyline>
<rect>
<text>
<use>
Shape elements<circle>
<ellipse>
<line>
<path>
<polygon>
<polyline>
<rect>
Text content elements<text>
<textPath>
<tspan>

SVG CSS Properties

Text properties

Presentation attributeSupported elements
alignment-baseline<textPath>
<tspan>
baseline-shift<textPath>
<tspan>
dominant-baselineText content elements
glyph-orientation-horizontalText content elements
glyph-orientation-verticalText content elements
kerningText content elements
text-anchorText content elements

Clip properties

Presentation attributeSupported elements
clip<foreignObject>
<image>
<marker>
<pattern>
<svg>
<symbol>
clip-pathContainer elements
Graphics elements
clip-rule<clipPath>

Masking properties

Presentation attributeSupported elements
maskContainer elements
Graphics elements
opacityGraphics elements
<a>
<defs>
<g>
<marker>
<pattern>
<svg>
<switch>
<symbol>

Filter effects

Presentation attributeSupported elements
enable-backgroundContainer elements
filterContainer elements
Graphics elements
flood-color<feFlood>
flood-opacity<feFlood>
lighting-color<feDiffuseLighting>
<feSpecularLighting>

Gradient properties

Presentation attributeSupported elements
stop-color<stop>
stop-opacity<stop>

Interactivity properties

Presentation attributeSupported elements
pointer-eventsGraphics elements

Color properties

Presentation attributeSupported elements
color-profile<image> referring to raster image

Painting properties

Presentation attributeSupported elements
color-interpolationContainer elements
Graphics elements
color-interpolation-filtersFilter primitive elements
color-renderingContainer elements
Graphics elements
fillShape elements
Text content elements
fill-ruleShape elements
Text content elements
fill-opacityShape elements
Text content elements
image-rendering<image>
marker<line>
<path>
<polygon>
<polyline>
marker-start<line>
<path>
<polygon>
<polyline>
marker-mid<line>
<path>
<polygon>
<polyline>
marker-end<line>
<path>
<polygon>
<polyline>
shape-renderingShape elements
strokeShape elements
Text content elements
stroke-dasharrayShape elements
Text content elements
stroke-dashoffsetShape elements
Text content elements
stroke-linecapShape elements
Text content elements
stroke-linejoinShape elements
Text content elements
stroke-miterlimitShape elements
Text content elements
stroke-opacityShape elements
Text content elements
stroke-widthShape elements
Text content elements
text-rendering<text>

SVG 2

While presentation attributes can be used as CSS properties to style SVG, what about controlling the coordinates and dimensions of SVG elements using CSS? SVG 2, which is in Candidate Recommendation at the time of this writing, makes it is possible to style and animate these properties.

The SVG 2 specification states:

Some styling properties can be specified not only in style sheets and ‘style‘ attributes, but also in presentation attributes. These are attributes whose name matches (or is similar to) a given CSS property and whose value is parsed as a value of that property.”

Not only does it mean that SVG properties can be styled using CSS as presentation attributes or in style sheets, but this also can be applied to CSS pseudo-classes such as :hover or :active.

SVG 2 also introduces more presentation attributes that can be used as styling properties. These attributes can be found in SVG 2 specification.

Element-specific properties

It is important to note that not every SVG element will support the same CSS properties. Much like how there are CSS properties that can be applied to certain SVG elements, there are specific properties that are supported by certain SVG elements.

For example, the <circle> or <ellipse> elements support the cxand cyproperties as coordinates of the center of the shape. The element also supports the rx and ry properties as the radius, but the element cannot use these properties.

See Also
SVG

Geometry properties

In SVG 2, properties such as rx and ry are defined as geometry properties. Geometry properties can be used as CSS properties, just like presentation attributes such as fill or stroke properties. These CSS properties and the corresponding SVG elements include:

SVG ElementGeometry Property
<circle>cx
cy
r
<ellipse>cx
cy
rx
ry
<rect>rx
ry
height
width
x
y
<path>path
<image>height
width
x
y
<foreignObject>height
width
x
y
<svg>height
width
x
y

Positioning SVG elements

SVG 2 also makes it is possible to position SVG elements using CSS. Let’s begin with drawing a rectangle shape having the following SVG:

<svg width="170" height="170"> <rect x="10" y="10" width="150" height="150" /></svg>

And the following CSS:

rect { fill: #6e40aa;}

This will produce a rectangle shape with its coordinates set to 10, 10. With SVG 2,xandycan be applied as CSS properties:

/* This will work with SVG 2 */rect { x: 10; y: 10; ...}

The SVG code would be reduced to this:

<svg width="170" height="170"> <rect width="150" height="150" /></svg>

You can even set thewidthandheightfor the<rect>element using CSS like so:

rect { ... width: 150px; height: 150px; ...}

That leaves us with just the following for SVG markup:

<svg width="170" height="170"> <rect /></svg>

At the time of writing, the following demos will work in Blink (e.g. Chrome and Opera) and WebKit (e.g. Safari) browsers as these browsers support SVG 2 features. Until then, let’s dive into how to override SVG properties using CSS.

SVG shape morphing

The element can be overridden with CSS to create shape morphing.

The SVG paths that morph one into the other must have the same commands and same number of points or else the morphing will not work.

Let’s start with drawing a element in the shape of a triangle. Using the d property will specify the shape of the element:

<svg height="220" width="300"> <path d="M150 10 L40 200 L260 200Z" /></svg>

To get the triangle to morph into a different shape, let’s override the SVG element with the d property with CSS:

path { d: path("M150, 10 L40, 200 L260, 200Z"); fill: #4c6edb;}

Let’s also add a :active pseudo-class to the property so when the element is clicked, the shape will morph into a square and change its fill color. Let’s also add a transition property to make the shape morphing action appear smooth. Here is the CSS:

path:active { d: path("M150, 10 L40, 200 L260, 200 L260, 200Z"); fill: #4c6edb; transition: all 0.35s ease;}

And the SVG would be:

<svg height="220" width="300"> <path /></svg>

Want another demo? Here is a cool demo from Chris Coyier demonstrating SVG shape morphing on hover!

Animating SVG properties

SVG properties can be animated using CSS through CSS animations and transitions.

In this demo, we will draw various SVG elements and create a wave animation. Start by drawing five elements:

<svg width="350" height="250"> <circle class="shape" /> <circle class="shape" /> <circle class="shape" /> <circle class="shape" /> <circle class="shape" /></svg>

We’ll be using CSS variables and :nth-child() CSS pseudo-class to define each .shape class. The .shape class will have a cy of 50 and a r of 20. Each of the .shape classes will have their own cx and fill CSS properties set:

:root { --color-1: #6e40aa; --color-2: #4c6edb; --color-3: #24aad8; --color-4: #1ac7c2; --color-5: #1ddea3;}.shape { cy: 50; r: 20;}.shape:nth-child(1) { cx: 60; fill: var(--color-1);}.shape:nth-child(2) { cx: 120; fill: var(--color-2);}.shape:nth-child(3) { cx: 180; fill: var(--color-3);}.shape:nth-child(4) { cx: 240; fill: var(--color-4);}.shape:nth-child(5) { cx: 300; fill: var(--color-5);}

Here is how it should look so far.

Now it’s time to animate! Start by using @keyframes rule to define the moveCircle animation:

@keyframes moveCircle { 50% { cy: 150; r: 13; }}

This will get each element to change their cy coordinates from 50 to 150 and r from 20 to 13. Add the following to the CSS to the .shape class get the animation running infinitely:

.shape { ... animation: moveCircle 1250ms ease-in-out both infinite;}

Finally, add an animation-delay to each of the .shape classes to the CSS with the exception of .shape:nth-child(1) like this:

.shape:nth-child(2) { ... animation-delay: 100ms;}.shape:nth-child(3) { ... animation-delay: 200ms;}.shape:nth-child(4) { ... animation-delay: 300ms;}.shape:nth-child(5) { ... animation-delay: 400ms;}

Shapes in SVG<pattern>elements can also be animated using CSS. Here is a cool demo by Dudley Storey showcasing that!

Wrapping up

As SVG 1.1 is the current standard, few browsers currently support SVG 2 features. It is not recommended to put these techniques into production yet. SVG 2 implementation is currently at Candidate Recommendation stage, thus support for styling SVG geometry properties with CSS should improve in the future.

Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

This comment thread is closed. If you have important information to share, please contact us.

SVG Properties In CSS Guide | CSS-Tricks (2024)
Top Articles
Binance Smart Chain to Ethereum Bridge: Cross-Chain Swaps from BEP20 to ERC20
7 Alternatives to REST APIs
Po Box 7250 Sioux Falls Sd
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Avonlea Havanese
Obituary (Binghamton Press & Sun-Bulletin): Tully Area Historical Society
Words From Cactusi
Best Theia Builds (Talent | Skill Order | Pairing + Pets) In Call of Dragons - AllClash
Barstool Sports Gif
Acbl Homeport
Azeroth Pilot Reloaded - Addons - World of Warcraft
Bros Movie Wiki
Springfield Mo Craiglist
Love In The Air Ep 9 Eng Sub Dailymotion
Midlife Crisis F95Zone
Craftology East Peoria Il
Eva Mastromatteo Erie Pa
Mzinchaleft
Palm Coast Permits Online
NHS England » Winter and H2 priorities
Bj Alex Mangabuddy
Unity - Manual: Scene view navigation
Governor Brown Signs Legislation Supporting California Legislative Women's Caucus Priorities
Hampton University Ministers Conference Registration
Jordan Poyer Wiki
How to Make Ghee - How We Flourish
Walmart Pharmacy Near Me Open
Beaufort 72 Hour
Kroger Feed Login
4Oxfun
JVID Rina sauce set1
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Ou Football Brainiacs
Miles City Montana Craigslist
Angel Haynes Dropbox
Publix Christmas Dinner 2022
Craftsman Yt3000 Oil Capacity
Motor Mounts
Kamzz Llc
4083519708
Second Chance Apartments, 2nd Chance Apartments Locators for Bad Credit
Pain Out Maxx Kratom
6576771660
Here's Everything You Need to Know About Baby Ariel
Lady Nagant Funko Pop
Crigslist Tucson
Devotion Showtimes Near Showplace Icon At Valley Fair
552 Bus Schedule To Atlantic City
Diccionario De Los Sueños Misabueso
Sam's Club Fountain Valley Gas Prices
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 6312

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.