search
HomeWeb Front-endCSS TutorialSVG Properties in CSS Guide

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;
}

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>

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

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

Simplifying the SVG markup to:

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

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>

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;
}

The SVG markup simplifies to:

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

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>

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;
  }
}

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
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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

See all articles

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use