SVGs are XML-based vector images that are widely used on the web. It is not made of pixels. SVGs are composed of paths defined by mathematical equations. This allows them to scale infinitely without losing quality.
We will learn What are SVG properties in CSS and how to work with SVG properties in CSS. SVG (Scalable Vector Graphics) is a powerful tool for creating images that can scale to any size without losing quality. By learning how to manipulate SVG properties with CSS, we can create dynamic, responsive designs that enhance the visual look of our web pages.
These are the following types of properties that we are going to discuss:
SVG Properties In CSS
We can use CSS to style SVGs in many ways, helping us to change their appearance dynamically. We can alter colors, add animations, or adjust shapes, CSS provides us with the tools to make our SVGs more interactive and visually engaging.
Below the table contains two columns named 'Presentation Attribute' and 'Supported Elements'. Presentation Attributes can be used as CSS properties and supported elements for them are mentioned in the next column.
SVG Elements by Category:
The elements present in SVG categorized based on their functionality are mentioned below in the table.
Type of Element | Elements |
---|
Container elements | <a>, <defs>, <g>, <marker>, <mask>, <pattern>, <svg>, <switch>, <symbol> |
---|
Filter primitive elements | <feBlend>, <feColorMatrix>, <feComponentTransfer>, <feComposite>, <feFlood>, <feGaussianBlur> |
---|
Gradient elements | <linearGradient>, <radialGradient>, <stop> |
---|
Graphics elements | <circle>, <ellipse>, <line>, <polygon>, <rect>, <text> |
---|
Text content elements | <text>, <textPath>, <tspan> |
---|
Shape elements | <circle>, <ellipse>, <line>, <polygon>, <rect> |
---|
Properties Shared Between CSS and SVG
Below are the properties that are shared between CSS and SVG both, we can use them to style our SVGs.
Property type | Presentation Attribute | Elements Supported |
---|
Font
| font | Text content Elements |
---|
font-family | Text content Elements |
font-size | Text content Elements |
font-style | Text content Elements |
font-weight | Text content Elements |
font-stretch | Text content Elements |
Text
| direction | <text>, <tspan> |
---|
letter-spacing | Text content Elements |
text-decoration | Text content Elements |
writing-mode | <text>, <tspan> |
word-spacing | Text content elements |
Masking
| overflow | <image>, <marker>, <pattern> |
---|
mask | Container and graphic elements |
Interactivity
| cursor | Container and graphic elements |
---|
pointer-events | Graphics elements |
Color
| fill, stroke | Shape and text elements |
---|
stop-color | <stop> (gradient stop points) |
Visibility
| display | Graphics elements, Text content elements, <a>, <foreignObject>, <g>, <svg>, <switch> |
---|
visibility | Graphic elements, Text content Elements |
Table of SVG Properties in CSS
These are SVG properties that are available in CSS to style them. Below is the table showing 'Category' , 'Property' and 'Supported Elements'.
Category | Property | Supported Elements |
---|
Text Properties
| alignment-baseline | <textPath>, <tspan> |
---|
baseline-shift | <textPath>, <tspan> |
dominant-baseline | Text content elements |
glyph-orientation-horizontal | same |
glyph-orientation-vertical | same |
kerning | same |
text-anchor | same |
Clip Properties
| clip | <foreignObject>, <image>, <marker>, <pattern>, <svg>, <symbol>
|
---|
clip-path | Container elements, Graphics elements |
clip-rule | <clipPath> |
Masking Properties
| mask | Container elements, Graphics elements |
---|
opacity | Graphics elements |
Filter Effects
| enable-background | Container elements |
---|
filter | Container elements, Graphics elements |
flood-color | <feFlood> |
flood-opacity | <feFlood> |
lighting-color | <feDiffuseLighting>, <feSpecularLighting> |
Gradient Properties
| stop-color | <stop> |
---|
stop-opacity | <stop> |
Interactivity Properties | pointer-events | Graphics elements |
---|
Color Properties | color-profile | <image> (referring to raster image) |
---|
Painting Properties
| color-interpolation | Container elements, Graphics elements |
---|
color-interpolation-filters | Filter primitive elements |
color-rendering | Container elements, Graphics elements |
fill | Shape elements, Text content elements |
fill-rule | Shape elements, Text content elements |
fill-opacity | Shape elements, Text content elements |
SVG 2
When we want more control over positioning (co-ordinates) and dimentions then we can use SVG 2. SVG 2 has more styling capabilities which we can use to control element dimentions with CSS. There are new Geometric properties like "rx", "ry", "x" and "y" allow precise element positioning. SVG 2 also has presentation attributes which can be used to style the properties.
Properties having a presentation attribute | Elements which Support them |
---|
cx, cy | Circle, Eclipse |
---|
height, width, x, y | Foreign Object, image, rect, svg, symbol, use |
---|
r | circle |
---|
rx, ry | eclipse, rect |
---|
d | path |
---|
Element-specific Properties
Keep in mind that Some properties apply only to specific SVG elements. SVG Element and Geometric Properties are mentioned in the table below.
Geometry Properties
In SVG 2, some properties such as 'rx' and 'ry' are defined as geometry properties. Geometry properties are used as CSS properties. Below are the properties which are Geomtry properties and SVG Element where they can be used.
SVG Element | Geometry Properties |
---|
<circle> | cx cy r |
---|
<eclipse> | 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 |
---|
Positing SVG Elements
SVG 2 offers positioning elements using CSS. Now, we will create a green colored rectangle and learn how we can change the position of the SVGs.
Example: In First example, the rectangle is positioned in center then in example 2 it is aligned to top left. In example 2 we have defined the properties of SVG in side css file.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Positioning SVG Elements</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<svg width="170" height="170">
<rect x="25" y="25" width="200" height="200" />
</svg>
</body>
</html>
CSS
rect {
fill: #2db05f;
}
body {
align-items: center;
display: flex;
height: 100vh;
justify-content: center;
}
Output:
example1SVG Shape Morphing
We can change the shape of SVGs by changine the css. First we will draw a rectangle of green color using rect properties then rx and ry properties of the rect element will be changed to make a circle. On hover we added the rx and ry properties are set to 50%, creating rounded corners that turn the rectangle into a circle. We also used transition property for smooth transition.
Example: On hovering transforming of rectangle into a circle
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Rectangle to Circle on Hover</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="center">
<svg width="300" height="300" viewBox="0 0 300 400">
<rect class="shape" x="100" y="0"
width="200" height="400" fill="green" />
</svg>
</div>
</body>
</html>
CSS
body,
html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #f4f4f4;
}
.center {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
svg {
display: block;
}
.shape {
fill: green;
transition: all 0.5s ease;
}
.shape:hover {
rx: 50%;
/* Rounded corners to make it a circle */
ry: 50%;
width: 300px;
height: 300px;
x: 50;
y: 50;
}
Output:
svg shape morphing exampleAnimating SVG Properties
SVG properties can be animated using CSS properties.
Example: Here we will draw a square and make it rotate infinitely. The green rectangle is centered using Flexbox. The rectangle continuously rotates using the spin animation defined with @keyframes.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Animating SVG Properties - Rotating Green Rectangle</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="center">
<svg width="100" height="100" viewBox="0 0 100 100">
<rect x="10" y="10" width="80" height="80" fill="green" />
</svg>
</div>
</body>
</html>
CSS
body,
html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #f4f4f4;
}
.center {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
svg {
animation: spin 4s infinite linear;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
rect {
fill: green;
}
Output:
svg animation exampleConclusion
We explored how to change SVG properties using CSS. From basic to advanced animations and shape morphing, CSS offers great tools to modify and enhance the SVGs in web design. Currently SVG 1.1 is the standard, since very few browsers support SVG2 features so it is not recommended to use until its a need.
Similar Reads
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
CSS Introduction CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable.It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
4 min read
CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h
2 min read
CSS Selectors CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or # ID for fundamental styling needs.Combinato
7 min read
CSS Comments CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more.You can try different formats of colors here- #content-iframe
5 min read
CSS Borders Borders in CSS are used to create a visible outline around an element. They can be customized in terms ofWidth: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ he
5 min read
CSS Margins CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.Syntax:body { margin: value;}HTML<html> <head>
4 min read
CSS Height and Width Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto.Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centim
4 min read
CSS Outline CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element.Syntaxselector{ outline: outline-width outline-type outline-color
4 min read