CSS fill Property
The fill property specifies the color or paint used to fill the interior of an SVG shape or text element.
It supports solid colors, gradients, patterns, and referenced paint sources. The property is commonly used to style SVG icons, illustrations, charts, and other vector graphics. Unlike the background-color property, fill paints the interior of an SVG element according to its geometry.
fill Syntax
The fill property accepts a color, the none keyword, or a reference to an SVG paint source.
Basic Syntax
selector {
fill: blue;
}
Color Values
.shape {
fill: red;
}
.shape {
fill: #336699;
}
.shape {
fill: rgb(51 102 153);
}
.shape {
fill: hsl(210 50% 40%);
}
Special Values
.shape {
fill: none;
}
.shape {
fill: currentColor;
}
Referenced Paint Sources
.shape {
fill: url("#my-gradient");
}
.shape {
fill: url("#my-pattern");
}
.shape {
fill: url("#my-gradient") #336699;
}
CSS-Wide Keywords
.shape {
fill: inherit;
}
.shape {
fill: initial;
}
.shape {
fill: unset;
}
.shape {
fill: revert;
}
.shape {
fill: revert-layer;
}
The initial value is black. An SVG shape without an explicitly assigned fill is therefore normally filled with black unless another declaration or inherited value changes it.
Property Values
The fill property accepts the following primary values and paint sources.
| Value | Description |
|---|---|
<color> |
Fills the element using a CSS color, such as a named color, hexadecimal color, or color function. |
none |
Disables painting of the element's interior. |
currentColor |
Uses the computed value of the element's color property. |
url() |
References an SVG paint server, such as a gradient or pattern. |
url() <color> |
Uses the referenced paint source when available, with the color as a fallback. |
url() none |
Uses the referenced paint source when available, otherwise paints no fill. |
context-fill |
Uses the fill paint of a context element in supported SVG contexts. |
context-stroke |
Uses the stroke paint of a context element in supported SVG contexts. |
The context-fill and context-stroke values are useful in specialized SVG contexts, such as markers. Browser support for these values should be checked separately from basic fill support.
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed fill value of the parent element. |
initial |
Restores the initial value, black. |
unset |
Uses the inherited value because the property is inherited. |
revert |
Reverts the property according to the CSS cascade origins. |
revert-layer |
Reverts the property according to the CSS cascade layers. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | black |
| Inherited | Yes |
| Applies to | SVG shapes and text content elements |
| Computed value | As defined for the specified paint value |
| Animation type | Depends on the paint value |
| Percentages | Not applicable directly |
The fill property is primarily used with SVG. It does not replace the CSS background properties for ordinary HTML elements.
How It Works
SVG shapes have geometric boundaries that determine the region to be painted. The fill property selects the paint applied to that region.
Step 1: Create an SVG Shape
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<rect class="example-shape" x="20" y="20" width="160" height="60"/>
</svg>
Step 2: Apply a Fill Color
.example-shape {
fill: #336699;
}
The rectangle's interior is painted blue.
Step 3: Change the Fill
.example-shape {
fill: #e67e22;
}
The rectangle's interior changes to orange without changing its size or shape.
Step 4: Remove the Fill
.example-shape {
fill: none;
stroke: #336699;
stroke-width: 3;
}
The rectangle has no interior paint, but its outline remains visible because the stroke property is applied.
Important Distinction
The fill is determined by the shape's geometry and fill rules. It does not necessarily correspond to the element's rectangular bounding box.
Color Values
The fill property accepts CSS color values, allowing SVG shapes to use the same color formats as other CSS properties.
Named Colors
.shape {
fill: steelblue;
}
Hexadecimal Colors
.shape {
fill: #336699;
}
RGB Colors
.shape {
fill: rgb(51 102 153);
}
RGB with Transparency
.shape {
fill: rgb(51 102 153 / 60%);
}
HSL Colors
.shape {
fill: hsl(210 50% 40%);
}
Modern Color Functions
.shape {
fill: oklch(60% 0.15 240);
}
Transparent Fill
.shape {
fill: transparent;
}
A transparent color produces transparent painted fill, while none means no fill paint is applied. This distinction can affect pointer-event hit testing.
Important Consideration
Support for modern color functions depends on browser compatibility. Use a widely supported fallback when necessary.
none
The none value disables painting of the SVG element's interior.
Basic Example
circle {
fill: none;
}
Outline-Only Circle
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg">
<circle cx="60" cy="60" r="45" class="outline-circle"/>
</svg>
.outline-circle {
fill: none;
stroke: #336699;
stroke-width: 5;
}
How It Works
The circle's interior is not painted. The stroke remains visible because it is controlled separately.
Practical Uses
Use fill: none for outlined icons, line drawings, diagrams, and shapes that should not have a solid interior.
Important Consideration
If both fill and stroke are set to none, the shape normally has no visible paint, although it remains part of the SVG document.
currentColor
The currentColor keyword uses the computed value of the element's CSS color property.
Basic Example
.icon {
color: #336699;
}
.icon path {
fill: currentColor;
}
HTML Example
<span class="icon">
<svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true">
<path d="M12 2 L22 22 H2 Z"/>
</svg>
</span>
How It Works
The SVG path uses the inherited text color of its surrounding element.
Hover Example
.icon {
color: #336699;
}
.icon:hover {
color: #e67e22;
}
.icon path {
fill: currentColor;
}
Practical Uses
The currentColor keyword is especially useful for icons that should automatically match the text color of buttons, navigation links, and other interface elements.
Important Consideration
currentColor follows the computed color value. It does not automatically use the element's background color.
url()
The url() function references an SVG paint server, such as a gradient or pattern, instead of specifying a solid color.
Basic Example
.shape {
fill: url("#my-gradient");
}
SVG Definition
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<defs>
<lineargradient id="my-gradient">
<stop offset="0%" stop-color="#336699"/>
<stop offset="100%" stop-color="#66ccff"/>
</lineargradient>
</defs>
<rect class="gradient-shape" x="20" y="20" width="160" height="60"/>
</svg>
.gradient-shape {
fill: url("#my-gradient");
}
How It Works
The browser finds the SVG paint server with the matching identifier and uses it to paint the shape.
Fallback Color
.gradient-shape {
fill: url("#my-gradient") #336699;
}
If the referenced paint server cannot be used, the browser can use the fallback color.
Important Consideration
The referenced paint server must be valid and accessible. External SVG resource references may be subject to browser restrictions.
SVG Gradients
SVG gradients allow a shape's fill to transition between two or more colors.
Linear Gradient
<svg viewBox="0 0 240 120" xmlns="http://www.w3.org/2000/svg">
<defs>
<lineargradient id="blue-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#336699"/>
<stop offset="100%" stop-color="#66ccff"/>
</lineargradient>
</defs>
<rect class="gradient-rectangle" x="20" y="20" width="200" height="80"/>
</svg>
.gradient-rectangle {
fill: url("#blue-gradient");
}
Radial Gradient
<svg viewBox="0 0 160 160" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="radial-fill">
<stop offset="0%" stop-color="#ffffff"/>
<stop offset="100%" stop-color="#336699"/>
</radialGradient>
</defs>
<circle class="radial-circle" cx="80" cy="80" r="65"/>
</svg>
.radial-circle {
fill: url("#radial-fill");
}
How Gradients Work
The gradient is defined inside the SVG document and referenced through its identifier.
Important Consideration
CSS gradient functions such as linear-gradient() cannot normally be assigned directly to the SVG fill property. Define an SVG gradient and reference it with url().
SVG Patterns
SVG patterns allow shapes to be filled with repeating graphical content instead of a single color.
Pattern Definition
<svg viewBox="0 0 240 120" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="stripe-pattern" width="12" height="12" patternUnits="userSpaceOnUse">
<rect width="12" height="12" fill="#e8f1f8"/>
<path d="M0 12 L12 0" stroke="#336699" stroke-width="3"/>
</pattern>
</defs>
<rect class="pattern-rectangle" x="20" y="20" width="200" height="80"/>
</svg>
CSS Example
.pattern-rectangle {
fill: url("#stripe-pattern");
}
How It Works
The SVG pattern defines a repeating tile. The browser uses that tile to paint the interior of the referenced shape.
Practical Uses
Patterns are useful for charts, diagrams, illustrations, and graphical indicators where a repeated design is more appropriate than a solid fill.
Important Consideration
The pattern's appearance depends on its dimensions, coordinate system, and the geometry of the shape being filled.
Fallback Colors
A fallback paint value can be supplied after a referenced SVG paint server.
Basic Example
.shape {
fill: url("#missing-gradient") #336699;
}
How It Works
If the referenced gradient cannot be resolved, the browser uses the fallback color.
Fallback to No Fill
.shape {
fill: url("#optional-pattern") none;
}
Fallback with currentColor
.shape {
color: #336699;
fill: url("#my-gradient") currentColor;
}
Important Consideration
A fallback following url() is used when the referenced paint source is unavailable or invalid. It is not a second layer painted underneath a valid gradient or pattern.
fill vs. stroke
The fill and stroke properties control different parts of an SVG shape.
fill
.shape {
fill: #336699;
}
Paints the interior of the shape.
stroke
.shape {
stroke: #e67e22;
stroke-width: 4;
}
Paints the outline of the shape.
Combined Example
.shape {
fill: #336699;
stroke: #e67e22;
stroke-width: 4;
}
Comparison
| Property | Purpose |
|---|---|
fill |
Controls the interior paint of an SVG shape. |
stroke |
Controls the outline paint of an SVG shape. |
stroke-width |
Controls the thickness of the outline. |
Important Consideration
A shape can have a fill without a stroke, a stroke without a fill, or both.
fill and fill-opacity
The fill-opacity property controls the opacity of the fill paint without directly changing the stroke opacity.
Basic Example
.shape {
fill: #336699;
fill-opacity: 0.5;
}
Keep the Stroke Opaque
.shape {
fill: #336699;
fill-opacity: 0.4;
stroke: #1f4568;
stroke-width: 3;
}
How It Works
The interior is painted with reduced opacity, while the stroke remains fully opaque unless its opacity is changed separately.
Using opacity Instead
.shape {
fill: #336699;
stroke: #1f4568;
opacity: 0.5;
}
The opacity property affects the rendered element as a whole, including both fill and stroke.
Important Consideration
Use fill-opacity when only the interior paint should become transparent. Use opacity when the entire rendered element should become transparent.
fill and fill-rule
The fill-rule property determines which regions of a complex SVG path are considered inside the shape and should receive fill paint.
nonzero
path {
fill: #336699;
fill-rule: nonzero;
}
The nonzero value uses a winding-number calculation to determine which regions are filled.
evenodd
path {
fill: #336699;
fill-rule: evenodd;
}
The evenodd value alternates between filled and unfilled regions according to how many times a ray crosses the path boundary.
Practical Uses
Fill rules are useful for paths containing holes, overlapping contours, or multiple subpaths.
Important Consideration
The fill property determines what paint is used, while fill-rule determines which regions of a complex path receive that paint.
Filling SVG Text
The fill property can also control the color or paint used for SVG text.
Basic Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<text class="svg-heading" x="20" y="60">HTML and CSS</text>
</svg>
.svg-heading {
fill: #336699;
font-size: 30px;
font-weight: bold;
}
Gradient Text
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<defs>
<lineargradient id="text-gradient">
<stop offset="0%" stop-color="#336699"/>
<stop offset="100%" stop-color="#66ccff"/>
</lineargradient>
</defs>
<text class="gradient-text" x="20" y="60">SVG Text</text>
</svg>
.gradient-text {
fill: url("#text-gradient");
font-size: 36px;
font-weight: bold;
}
How It Works
The fill is applied to the shapes of the rendered glyphs rather than to a rectangular background behind the text.
Important Consideration
The fill property is intended for SVG text painting. Use the color property for ordinary HTML text.
Inheritance
The fill property is inherited, allowing a parent SVG element to establish a fill color for its descendants.
Parent SVG Element
svg {
fill: #336699;
}
HTML Example
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="30"/>
<rect x="110" y="20" width="60" height="60"/>
</svg>
Both shapes inherit the fill value from the SVG element unless a more specific declaration overrides it.
Override an Individual Shape
svg {
fill: #336699;
}
svg .highlight {
fill: #e67e22;
}
Group Inheritance
svg .icon-group {
fill: #336699;
}
Shapes inside the group inherit the group's fill value unless they define their own.
Important Consideration
Inheritance makes it convenient to style groups of SVG elements, but individual declarations can override the inherited paint.
CSS vs. SVG Presentation Attribute
The fill color of an SVG element can be specified through CSS or through the SVG fill presentation attribute.
SVG Presentation Attribute
<circle cx="50" cy="50" r="30" fill="#336699"/>
CSS Property
circle {
fill: #e67e22;
}
How the Cascade Works
SVG presentation attributes participate in the CSS cascade. A normal author CSS declaration can override a corresponding presentation attribute.
Combined Example
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle class="styled-circle" cx="50" cy="50" r="35" fill="#336699"/>
</svg>
.styled-circle {
fill: #e67e22;
}
The circle appears orange because the CSS declaration overrides the presentation attribute.
Recommended Approach
Use CSS when fills need to respond to themes, hover states, media queries, or shared styles. Presentation attributes remain useful for self-contained SVG graphics.
Animations and Transitions
The fill property can be animated, with interpolation behavior depending on the specified paint values.
Hover Transition
.interactive-shape {
fill: #336699;
transition: fill 0.3s ease;
}
.interactive-shape:hover {
fill: #e67e22;
}
Keyframe Animation
.animated-shape {
animation: change-fill 2s ease-in-out infinite alternate;
}
@keyframes change-fill {
from {
fill: #336699;
}
to {
fill: #e67e22;
}
}
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.animated-shape {
animation: none;
}
.interactive-shape {
transition: none;
}
}
Color Interpolation
Transitions between compatible color values can produce gradual color changes.
Paint Server Changes
Changes involving gradients, patterns, or none may use discrete behavior rather than a smooth color interpolation.
Important Consideration
Avoid rapid flashing color changes, especially in graphics that occupy large portions of the screen.
fill Example
This example demonstrates solid fills, no fill, currentColor, and an SVG gradient.
HTML Example
<div class="fill-demo">
<figure>
<svg viewBox="0 0 120 100" role="img" aria-label="Solid blue circle">
<circle class="solid-fill" cx="60" cy="50" r="35"/>
</svg>
<figcaption>Solid Fill</figcaption>
</figure>
<figure>
<svg viewBox="0 0 120 100" role="img" aria-label="Outlined circle without a fill">
<circle class="no-fill" cx="60" cy="50" r="35"/>
</svg>
<figcaption>No Fill</figcaption>
</figure>
<figure class="current-color-example">
<svg viewBox="0 0 120 100" role="img" aria-label="Circle using the current text color">
<circle class="current-fill" cx="60" cy="50" r="35"/>
</svg>
<figcaption>currentColor</figcaption>
</figure>
<figure>
<svg viewBox="0 0 120 100" role="img" aria-label="Circle with a blue gradient">
<defs>
<lineargradient id="fill-demo-gradient">
<stop offset="0%" stop-color="#336699"/>
<stop offset="100%" stop-color="#66ccff"/>
</lineargradient>
</defs>
<circle class="gradient-fill" cx="60" cy="50" r="35"/>
</svg>
<figcaption>Gradient Fill</figcaption>
</figure>
</div>
CSS Example
.fill-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
gap: 15px;
}
.fill-demo figure {
margin: 0;
text-align: center;
}
.fill-demo svg {
display: block;
width: 100%;
max-width: 120px;
margin: 0 auto;
}
.fill-demo figcaption {
margin-top: 8px;
font-size: 0.9rem;
}
.fill-demo .solid-fill {
fill: #336699;
}
.fill-demo .no-fill {
fill: none;
stroke: #336699;
stroke-width: 4;
}
.fill-demo .current-color-example {
color: #e67e22;
}
.fill-demo .current-fill {
fill: currentColor;
}
.fill-demo .gradient-fill {
fill: url("#fill-demo-gradient");
}
The first circle uses a solid blue fill. The second has no fill and displays only its outline. The third inherits its color through currentColor, and the fourth uses a referenced SVG gradient.
These examples demonstrate how the same SVG geometry can produce different visual results without changing the shape itself.
Accessibility
The fill property affects the appearance of SVG graphics but does not change their semantic meaning.
Maintain Sufficient Contrast
Choose fill colors that provide adequate contrast against surrounding backgrounds, particularly for meaningful icons and graphical controls.
Do Not Rely on Color Alone
When a fill color communicates status or meaning, provide another visual or textual indicator.
Accessible SVG Graphics
Informative SVG graphics should have an accessible name or equivalent text alternative.
Decorative Icons
Decorative SVG icons can use aria-hidden="true" when they do not convey additional information.
Interactive Icons
Ensure that interactive SVG icons have accessible labels, keyboard support, and visible focus indicators.
Color Changes
Do not use fill color changes as the only indication of hover, focus, selection, or validation state.
Animations
Avoid rapid or flashing fill changes, and respect reduced-motion preferences when using animated graphics.
Important Considerations
Keep the following points in mind when using fill:
- Purpose: Specifies the paint used for the interior of SVG shapes and text.
- Initial value:
black. - Inheritance: The property is inherited.
- Solid colors: Accepts standard CSS color values.
- none: Disables interior painting.
- currentColor: Uses the computed value of the
colorproperty. - url(): References SVG paint servers such as gradients and patterns.
- Fallbacks: A fallback color or
nonecan follow a paint server reference. - Gradients: SVG gradients must be defined and referenced rather than assigned using ordinary CSS gradient functions.
- Patterns: Repeating SVG patterns can be used as fill paint.
- stroke: Controls the outline separately from the fill.
- fill-opacity: Controls the opacity of the interior paint.
- fill-rule: Determines which regions of complex paths receive fill paint.
- SVG text: The property can paint the shapes of SVG text glyphs.
- Presentation attributes: SVG
fillattributes participate in the CSS cascade. - Animations: Compatible color values can transition smoothly.
- Accessibility: Maintain sufficient contrast and avoid relying on color alone.
For reusable SVG icons, fill: currentColor is often a convenient choice because it allows icons to follow the text color of their surrounding interface elements.
Browser Support
The CSS fill property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.
Basic solid fills, none, currentColor, and references to SVG gradients and patterns have longstanding support.
Supported Example
svg path {
fill: #336699;
}
Feature Detection
@supports (fill: currentColor) {
svg path {
fill: currentColor;
}
}
Compatibility Considerations
Support for specialized paint values such as context-fill and context-stroke may vary. Modern CSS color functions also have their own compatibility requirements.
For current compatibility information, visit Can I Use? .
Specifications
The fill property is defined in the SVG 2 specification, which describes how fill paint is applied to SVG graphics.
Related paint and color behavior is also described in the CSS Fill and Stroke and CSS Color specifications.
CSS Fill and Stroke Module Level 3
These specifications describe SVG paint sources, fill behavior, color values, and the relationship between CSS and SVG presentation attributes.
