What sets `all: unset` apart from `all: revert`?

According to MDN:

The revert keyword behaves similarly to unset in most scenarios. The main distinction is for properties that are set by the browser or custom stylesheets made by users (applied on the browser side).

I'm a bit confused about the browser and custom stylesheet. Can both be overridden by using all: unset as well?

Answer №1

Excerpt from MDN:

The keyword unset in CSS resets a property to its inherited value if it inherits from its parent, and to its initial value if not. It can be likened to the inherit keyword in one scenario, and to the initial keyword in another.

Therefore, unset serves as either inherit or initial.

On the other hand, the revert CSS keyword reverts the cascaded value of a property back to what it would have been had no changes been made by the current style origin on the current element. In essence, it sets the property back to its inherited value if applicable, or to the default value specified by the user agent's stylesheet (or any user styles present).

To illustrate, consider a default browser style being applied to an element. Using revert will restore those styles, while unset will not.

Let's take a look at an example:

p {
  margin: 50px;
}
<p style="margin:revert">
  some text here
</p>
<p style="margin:unset">
  some text here
</p>

In this example, the revert keyword will eliminate the 50px margin and revert to the default margin set by the browser. Whereas, unset will simply reset the margin to the initial value, which is 0.

Please note that the revert value may not be supported across all browsers: https://caniuse.com/#feat=css-revert-value


If there are no default styles in place, revert will behave similarly to unset.

In many cases, the revert keyword functions identically to unset. The key distinction arises for properties influenced by values set by the browser or custom user stylesheets.


The shorthand all applies to all properties, thereby following the same logic described above for each individual property.


Additional examples:

p {
  margin: 50px;
  border:1px solid blue;
}

.box {
  color: red;
  display:unset;
}

p {
  color:unset; /* Inherits red color */
  display:inline-block;
}
<div class="box">
  <p style="margin:revert;border:revert;display:revert;">
    "block" text with no border and default margin
  </p>
  <p style="margin:unset;border:unset;display:unset;">
    "inline" text without border or margin
  </p>
</div>

Answer №2

When using the unset keyword, it will attempt to revert to inherited property values before resorting to browser or custom stylesheet values. According to MDN:

The unset CSS keyword resets a property to its inherited value if it inherits from its parent, and to its initial value if not. Essentially, it functions like the inherit keyword when inheriting and like the initial keyword when not.

Learn more about unset - CSS @ MDN

Answer №3

revert and unset may seem identical, but they differ when the default CSS value for a property conflicts with the value set by the browser or custom stylesheets created by the user.

For instance, the default CSS value for display is inline for all elements, including divs. Therefore, if you set display:unset, it will behave as if it were set to display:inline. However, most browsers actually set display:block for div elements.

<div style="border: 1px solid blue">
  <p> This div uses unset, so now the divs' display will be inline</p>
  <div style="display:unset;"> text</div>
  <div style="display:unset;"> text</div>
</div>
<br/>
<div style="border: 1px solid blue">
  <p> This div uses revert, meaning the divs' display will be block (either the browser default or the user-defined value)</p>
  <div style="display:revert;"> text</div>
  <div style="display:revert;"> text</div>
</div>

Answer №4

The explanations provided above do not delve deep enough into the intricacies of these unique "reset" property values. Many developers struggle with understanding the difference between inheritable and non-inheritable properties when utilizing them.

It's important to note that all, revert, and unset will not function properly on Internet Explorer. Therefore, it is recommended to combine them with other solutions for compatibility.

Let's analyze the behavior of all:revert first, which essentially sets all properties to unset, except for one key distinction explained below. When applying all:revert to an element(s), the following actions take place:

  1. Any inheritable properties (typically text-related such as font or color) previously assigned via style sheets are removed and set to "inherit". Styles from either the body element or the browser's default User Agent style sheet now cascade down through normal inheritance to the element. Any directly assigned or customized styles are replaced with inherited values using the "inherit" keyword.
  2. Any non-inheritable property values (like display or border) assigned via style sheets are eliminated from the element, and the browser's default style sheet values take precedence. This usually refers to the default style sheet bundled with the browser upon installation.
  3. The aforementioned rules assume that there are no more specific cascading styles overriding the "revert" action for any property on the targeted element. Remember, "all:revert" behaves like any other CSS property, so any cascading styles would individually override the properties affected by "all:revert" on that element.

For example:

INHERITABLE PROPERTY If a special property like "font-family:sans-serif" (which is inheritable) was initially applied to a "div", but then switched to "all:revert", the "sans-serif" styling would be removed from the font family, allowing it to inherit the font-family value dictated by its parent in the hierarchy. This could trace back several levels up or originate from the "body" element.

NON-INHERITABLE PROPERTY Similarly, if a custom value such as "border:2px solid blue;" (non-inheritable) was set on the same "div" but later replaced with "all:revert", the border styling would be stripped, reverting to the default "border" value specified for "div" in the browser's User Agent style sheet. Typically, this default value is "none" for borders on "div".

On the other hand, all:unset operates similarly but reverts non-inheritable properties back to the "initial" state rather than the default style defined in the browser's UA stylesheet or the element's default value for that property. This can lead to unexpected outcomes. For instance, setting "display:unset" on a "div" would default to "display:inline", contrary to the anticipated "display:block".

Answer №5

There are specific values that can be assigned to each property. Stokely's response underscores the importance of distinguishing between inherited and non-inherited properties.

initial
This value is used to set a property to its initial specified value, like color: initial;. It's important to note that this initial value may not always correspond to the default style sheet value set by the browser.

inherit
When this value is used, a property inherits the computed value of its parent, as in *{box-sizing: inherit;}.

unset
This keyword functions similarly to the value inherit for inherited properties and the value initial for non-inherited properties.

revert
The value is solely determined by the cascade of user and user agent rules, disregarding conflicting author rules when setting the value. However, these rules remain valid based on their specificity.

all
The shorthand property "all" can adopt any of the aforementioned global values. According to MDN Web Docs, this value will apply to all properties except unicode-bidi and direction.

The excerpt:

The revert CSS keyword resets the cascaded value of a property back to what it would have been if no changes had been made by the current style origin to the current element. It essentially reverts the property either to its inherited value from its parent or to the default value set by the user agent stylesheet (or by existing user styles).

is somewhat misleading. For many inherited properties, using the declaration property: revert; effectively behaves the same as property: inherit;, meaning the calculated value of the parent element is adopted. Exceptions include form elements like input, select, textarea, and button, where the user agent interrupts the inheritance chain, specifying explicit values for font-family and font-size. In such cases, the value revert causes these elements to adopt the user agent's values. Non-inherited properties always take the value derived from the cascade of user and user agent rules. An example illustrates this concept.

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Testing Global Values and Shorthand Property all</title>
  <style>
    body, div{border: solid 2px;padding:10px;margin: 10px;}
    .all-revert { all:revert;}
    div{ font-family: 'Courier New', Courier, monospace; }
  </style>
</head>

<body style="font-family:serif;color:royalblue;font-size: 24px;">
  <div class="all-revert">Default serif (TNR), no margin, no border.</div>
  <div>Courier New, margin of 20 pixels, and border.</div>
  <button class="all-revert">Arial</button>
  <button style="all:unset; border:revert;">Default serif</button>
</body>

</html>

The rule div, button {all: revert;} affects the div and button elements differently. The div element has all font-related properties set to inherit, applying the settings from the body element. Conversely, the button element inherits default font color, size, and type values chosen by the user agent.

The use of the value unset on inherited properties mirrors the effect of inherit; hence, the font properties of the second button inherit from the body element, while the border adopts the user agent's settings. For non-inherited properties like border, padding, and margin, the 'revert' value leads to adoption of user agent values.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Once more baffled by the nested boxes, this iteration adorned in vibrant hues and trimmed with a striking border

In a previous inquiry, I sought advice on centering one box within another. However, my attempts this time around have led to some peculiar results. I experimented with two different code variations- one involving a background color and the other utilizing ...

What causes the movement of li elements when hovered over and moved away from?

While trying to highlight a specific list item (li) element on mouse hover and simultaneously hide others using a script, an issue arises. The highlighted element "something" changes its position during hover, causing it to move out of the mouse cursor&apo ...

Can you direct me to resources on the internet that provide information about support for 12 Bit color depth?

I am curious to know when the w3specs will support colors in 12 bits instead of the current 8-bit format used for webcolors like sRGB (#FFFFFF or rgba(255,0,0,100). I wonder if there will ever be a new standard called 12-Bit sRGB where colors can be defin ...

Achieving a 100% height sidebar that remains visible on the screen at all times in Bootstrap

Looking to achieve this specific layout: I want the left col-md-3 to maintain a consistent 100% height and always be visible on the screen - it's going to be a menu. The right side should function normally with scrolling capabilities. Appreciate any ...

The text appears as regular bold instead of extra bold in Chrome

Having trouble with displaying the Open Sans Extra-Bold font? Check out the font here: I've tried everything, but it just won't show up on my website. Any suggestions? Here is the code on JSFiddle for reference: https://jsfiddle.net/0hhbgyrd/ ...

Skeleton page CSS sub-navigation menu for Internet Explorer 7

I created a webpage using the Skeleton Framework and added a CSS menu with a submenu. While the menu works well in most browsers, I encountered issues with the submenu when using IE7 and lower versions. Instead of appearing below the parent menu item, the ...

Creating static directories and sub-directories simultaneously with express.js

I have a `views` directory that I made static by using `app.use(express.static('views'))`. The views directory consists of subfolders as listed below- views |-homepage/ | |-homepage.ejs | |-homepage.css | |-homepage.js | |-partials/ | ...

This javascript function must be executed when the page loads

What is the best way to trigger this function onLoad for a specific URL? function fadeLoop() { var count = 0, elements = $('.fader').hide(), duration = 500; function displayElement() { elements.fadeOut(duration) ...

Can you recommend a resource that provides a comprehensive breakdown of which CSS elements are compatible with each internet

Can anyone recommend a comprehensive resource, such as a book or website, that provides a complete list of CSS elements and their compatibility with the major web browsers? Specifically, I am interested in information regarding compatibility with IE8 and F ...

The hover feature failed to function as expected

My issue is that the span hover effect is not working as expected. I am trying to create a system where when the mouse hovers over File Select1, File Select2, or File Select3, the text turns blue. Here's the HTML code I wrote: <form action="/accou ...

Spinning elements with a gradient overlay

I have a few tasks to accomplish: Ensuring the logo scales properly within the viewport without losing relative proportions Applying a purple gradient (to transparent) specifically to the rings Having the rings rotate at varying speeds and in opposite dir ...

Styling the sacred grail layout with organized columns

In my search for answers, I have come across many general responses to this common question. However, none of them address the specific constraints that I am facing with this version. The task at hand is to implement the holy grail layout, but with a key ...

What is the best way to utilize CSS to center an element in relation to the scroll bar?

For a group project, I successfully centered a div on a webpage. However, I ran into an issue where the website's contents are centered taking the scroll bar width into consideration. This means that the web page contents adjust to be centered without ...

Tips for achieving a seamless appearance with box-shadow on the left and right sides of various elements

.row { height: 300px; width: 300px; margin: 0 auto; box-shadow: 0 -20px 0px 0px white, 0 20px 0px 0px white, 12px 0 30px -4px rgba(0, 0, 0, 0.3), -12px 0 30px -4px rgba(0, 0, 0, 0.3); } <div class="row"></div> <div class="row">< ...

Is it possible for me to interact with different elements upon hovering?

html, <div id="first"> <a>Click here</a> </div> <div id=second"> second div content </div> css, #first a:hover{ color:blue; /*change color on hover */ #second{ background:blue; } } In ...

An Elegant Horizontal Navigation Bar with Smooth Scrolling and Div Anchor Tags

Looking to design a unique sticky horizontal scrolling navigation bar for displaying dates like 1800, 1801, 1802, etc. as part of a timeline project. The idea is to have the dates scroll horizontally within the navbar while I scroll down the page, alignin ...

How can I prevent the content from sliding out of view in a Bootstrap carousel?

I am looking to create a slider that includes a form or text. However, I have noticed that when scaling up by 50-75% or when there is a lot of content, the content extends beyond the slider. Image Check out my code on jsfiddle: jsfiddle.net/sL70r2an/ ...

Ensure that only one button from the collection is activated

One of the features on my website includes 3 blocks with buttons. These buttons trigger a change in the displayed picture when clicked. However, it is crucial to ensure that when a new track is activated, the previous button returns to its original state. ...

How to create a scrolling effect that fades a pseudo element using either JavaScript or jQuery

Seeking help on how to fade a pseudo element to opacity:0 on scroll has left me feeling lost and frustrated. I've included my codepen for reference here: http://codepen.io/emilychews/pen/JWyaKr Unfortunately, Greensock is not an option for this proje ...

What is the best way to align my SVG to display inline with text?

I am trying to add an SVG image next to some text in a navbar, but I'm running into some issues. The SVG is overflowing from the navbar instead of aligning inline with the text. Here's a snippet of my HTML code: ...