Table of Contents
SVG Elements and CSS Properties: A Categorized Overview
CSS and SVG Shared Properties
Font Properties
Text Properties
Masking Properties
Interactivity Properties
Color Properties
Visibility Properties
SVG-Specific CSS Properties
Clip Properties
Filter Effects
Gradient Properties
Painting Properties
SVG 2 and CSS Styling
Element-Specific Property Support
Geometry Properties
Positioning SVG Elements with CSS (SVG 2)
SVG Shape Morphing with CSS
Animating SVG Properties with CSS
Conclusion
Home Web Front-end CSS Tutorial SVG Properties in CSS Guide

SVG Properties in CSS Guide

Apr 21, 2025 am 11:21 AM

SVG Properties in CSS Guide

This guide explores the intersection of CSS and Scalable Vector Graphics (SVG), focusing on styling SVG elements using CSS properties. While numerous SVG attributes exist, only certain ones—primarily presentation attributes—can be directly styled with CSS. Some CSS properties are shared with SVG (like font-size and opacity), while others are SVG-specific.

For instance, to color an element red, the fill property works in CSS because fill is a presentation attribute:

circle {
  fill: red;
}
Copy after login

This guide comprehensively details available SVG elements and their corresponding CSS properties, covering various styling techniques including general styles and animations.

SVG Elements and CSS Properties: A Categorized Overview

The following outlines presentation attributes usable as CSS properties, categorized for clarity. Deprecated elements are excluded.

CSS and SVG Shared Properties

Font Properties

Text Properties

Masking Properties

Interactivity Properties

Color Properties

Visibility Properties

SVG-Specific CSS Properties

Text Properties

Clip Properties

Masking Properties

Filter Effects

Gradient Properties

Interactivity Properties

Color Properties

Painting Properties

SVG 2 and CSS Styling

While presentation attributes bridge the gap between CSS and SVG styling, SVG 2 (currently in Candidate Recommendation) significantly enhances this interaction, allowing control over element coordinates and dimensions using CSS.

The SVG 2 specification clarifies:

"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.”

This means CSS, including pseudo-classes like :hover and :active, can style SVG properties through presentation attributes or stylesheets. SVG 2 also introduces additional presentation attributes for styling. Refer to the SVG 2 specification for a complete list.

Element-Specific Property Support

It's crucial to remember that CSS property support varies across SVG elements. Certain properties are element-specific. For example, <circle></circle> and <ellipse></ellipse> support cx, cy, rx, and ry, but other elements might not.

Geometry Properties

SVG 2 defines properties like rx and ry as geometry properties, stylable with CSS alongside presentation attributes like fill or stroke.

Positioning SVG Elements with CSS (SVG 2)

SVG 2 allows CSS-based positioning. Consider a rectangle:

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

With SVG 2, the x and y attributes can be controlled via CSS:

/* Works with SVG 2 */
rect {
  x: 10;
  y: 10;
  /* ...other styles... */
}
Copy after login

Simplifying the SVG markup to:

<svg height="170" width="170"><rect height="150" width="150"></rect></svg>
Copy after login

Similarly, width and height can be set using CSS, further minimizing the SVG code.

Currently, Blink (Chrome, Opera) and WebKit (Safari) browsers offer better support for these SVG 2 features. For broader compatibility, alternative techniques are necessary.

SVG Shape Morphing with CSS

CSS can be used to create shape morphing effects. The SVG paths involved must have identical commands and the same number of points for successful morphing.

Example: Morphing a triangle into a square.

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

CSS to override the path:

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

path:active {
  d: path("M10,10 L160,10 L160,160 L10,160 Z"); /* Morphed shape */
  fill: #4c6edb;
  transition: all 0.35s ease;
}
Copy after login

The SVG markup simplifies to:

<svg height="220" width="300"><path></path></svg>
Copy after login

Animating SVG Properties with CSS

CSS animations and transitions animate SVG properties. This example demonstrates a wave animation with five circles:

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

CSS with variables and :nth-child() for styling and animation:

/* ... CSS for colors and initial circle styles ... */

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

/* ... animation-delay for each circle ... */

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

Animations within <pattern></pattern> elements are also possible.

Conclusion

While SVG 2 offers powerful CSS styling capabilities, widespread browser support is still developing. For production use, consider compatibility carefully. As SVG 2 matures, CSS styling of SVG geometry properties will become increasingly robust and reliable.

The above is the detailed content of SVG Properties in CSS Guide. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

See all articles