This website may use cookies. More info. That's Fine x
Welcome Login

Css: Resetting all css properties on an element (with all: unset property)


The 'all: unset;' css property is a powerful tool for resetting all css properties on an element to their initial or inherited values.

 

What it does:

1. Resets all properties:

When applied to an element, 'all: unset;' resets all css properties (both inherited and non-inherited) to their initial values as defined by the browser's default stylesheet.

This effectively removes any styles applied directly to the element or inherited from its parent.

When you apply 'all: unset;' to a css selector, it resets all css properties on the element to their initial values as defined by the browser's default stylesheet.

This effectively means it clears any styles set by classes, inline styles, or inherited styles.

 

2. Usage:

You typically apply 'all: unset;' within a specific css rule to reset styles on certain conditions, such as on user interaction (like focus or hover), to revert to the browser's default appearance or to allow for re-styling from scratch.

 

3. Priority:

It has a high specificity and overrides all other styles applied to the element through class, id, inline styles, or inherited styles.

 

Eg: to reset all styles for an element when it is hovered over:

When an element with the class .reset-on-hover is hovered over, all: unset; removes all styles applied to that element.

This can be particularly useful when you want to completely reset the visual appearance of an element based on user interaction.

.reset-on-hover:hover {
    all: unset;
}

 

Eg2: let's say you have an HTML element with some styles applied through classes:

<style>
.my-class {
    color: red;
    font-size: 16px;
}

.another-class {
    background-color: yellow;
}
</style>



<div class="my-class another-class">Hello, World!</div>

 

If you want to clear all styles applied by these classes from the div, you can use all: unset; like this:

div {
    all: unset;
}

 

After applying 'all: unset;':

The div will no longer have the color, font-size, and background-color styles that were applied by the classes .my-class and .another-class.

It will revert to the default styling for a div element, which typically includes things like display: block;, margin: 0;, padding: 0;, and so on, depending on the browser's default stylesheet.

 

 

Considerations:

Browser Support: 'all: unset;'' is well-supported in modern browsers, but older browsers might not fully support it.

Impact on Child Elements: all: unset; affects only the specific element to which it is applied.

It does not affect child elements directly; their styles remain intact unless they are explicitly targeted.

 

 

Alternatives:

1. Specific resets:

Instead of resetting all styles with 'all: unset;', you may prefer to reset specific properties:

Eg: to achieve a more controlled reset while retaining certain styles:

font-size: unset;
color: unset;

etc...

 

2. Conditional resets: you can use javascript to dynamically add or remove classes that apply all: unset; based on specific conditions or user interactions, providing more flexibility in styling behavior.

 

 

Conclusion:

'all: unset;' is a concise and effective css property for resetting styles to their default values, offering a straightforward way to manage CSS resets in responsive web design and interactive applications.

It provides a clean slate for styling elements based on user interactions or specific conditions without resorting to more complex css overrides.

Hence, 'all: unset;' is a way to effectively clear all css properties that are applied directly to an element, including those set via classes or inline styles.


Created on: Friday, July 5, 2024 by Andrew Sin