LogRocket Blog

Understanding the CSS revert-layer keyword

thumbnail

Overview of CSS Cascade Layers

Cascade layers introduce the new @layer at-rule in CSS. This feature is highly relevant when considering the use of the revert property. The revert keyword allows a property in a cascade layer to roll back to the value of the matching property in a previous cascade layer.

Understanding the revert Keyword

@layer component {
  .item {
    color: red;
  }
}

@layer base {
  .item {
    color: blue;
  }
}

.item {
  color: revert;
}

In this example, the color property of the .item selector will revert to the value in the base layer, which is blue.

Use Cases for CSS revert

Example Usage

@layer custom {
  .selector {
    color: red;
  }
}

.selector {
  color: revert;
}

In this scenario, the color property of all items styled with the .selector class will revert to the user-agent style, which is black.

Resetting Styles Within Layers

@layer custom {
  .selector {
    color: red;
  }
}

.selector {
  color: revert;
}

By using the revert keyword, elements can retain their old styles by reverting to the previous author origin.

Understanding the revert keyword and its potential use cases can empower developers to effectively manage styles and versioning within their projects.