revert - CSS | MDN (original) (raw)
Examples
Revert vs. unset
Although revert and unset are similar, they differ for some properties for some elements.
So in the below example, we set custom font-weight, but then try to revert and unset it inline in the HTML document. The revert keyword will revert the text to bold because that is the default value for headers in most browsers. The unset keyword will keep the text normal because, as an inherited property, the font-weight would then inherit its value from the body.
HTML
<h3 style="font-weight: revert; color: revert;">
This should have its original font-weight (bold) and color: black
</h3>
<p>Just some text</p>
<h3 style="font-weight: unset; color: unset;">
This will still have font-weight: normal, but color: black
</h3>
<p>Just some text</p>
CSS
h3 {
font-weight: normal;
color: blue;
}
Result
Revert all
Reverting all values is useful in a situation where you've made several style changes and then you want to revert to the browser default values. So in the above example, instead of reverting font-weight and color separately, you could just revert all of them at once - by applying the revert keyword on all.
HTML
<h3>This will have custom styles</h3>
<p>Just some text</p>
<h3 style="all: revert">This should be reverted to browser/user defaults.</h3>
<p>Just some text</p>
CSS
h3 {
font-weight: normal;
color: blue;
border-bottom: 1px solid grey;
}
Result
Revert on a parent
Reverting effectively removes the value for the element you select with some rule and this happens only for that element. To illustrate this, we will set a green color on a section and red color on a paragraph.
HTML
<main>
<section>
<h3>This h3 will be dark green</h3>
<p>Text in paragraph will be red.</p>
This stray text will also be dark green.
</section>
<section class="with-revert">
<h3>This h3 will be steelblue</h3>
<p>Text in paragraph will be red.</p>
This stray text will also be steelblue.
</section>
</main>
CSS
main {
border: 3px solid steelblue;
}
section {
margin: 0.5rem;
border: 2px dashed darkgreen;
}
main {
color: steelblue;
}
section {
color: darkgreen;
}
p {
color: red;
}
section.with-revert {
color: revert;
}
Result
Notice how the paragraph is still red even though a color property for the section was reverted. Also, note that both the header and plain text node are steelblue. The result of reverting makes it as if section { color: darkgreen; } did not exist for the section with color: revert applied.
Also, if neither the user agent nor the user override the <h3> or <section> color values, then the steelblue color is inherited from <main>, as the color property is an inherited property.
Specifications
| Specification |
|---|
| CSS Cascading and Inheritance Level 4 # default |
Browser compatibility
See also
- Use the initial keyword to set a property to its initial value.
- Use the inherit keyword to make an element's property the same as its parent.
- Use the revert-layer keyword to reset a property to the value established in a previous cascade layer.
- Use the unset keyword to set a property to its inherited value if it inherits or to its initial value if not.
- The all property lets you reset all properties to their initial, inherited, reverted, or unset state at once.