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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

The choice of CSS units depends on design requirements and responsive requirements. 1.px is used for fixed size, suitable for precise control but lack of elasticity; 2.em is a relative unit, which is easily caused by the influence of the parent element, while rem is more stable based on the root element and is suitable for global scaling; 3.vw/vh is based on the viewport size, suitable for responsive design, but attention should be paid to the performance under extreme screens; 4. When choosing, it should be determined based on whether responsive adjustments, element hierarchy relationships and viewport dependence. Reasonable use can improve layout flexibility and maintenance.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.
