- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - @layer
The CSS at-rule @layer is used to declare a cascade layer and may additionally specify the priority order when multiple cascade layers are involved.
layer-name - It is the name of the cascade layer.
rules - It is the set of CSS rules in the cascade layer.
Description
Rules within a cascade layer are combined, providing web developers with greater control over the cascade.
Styles that are not assigned to a layer are grouped into a single anonymous layer that is ordered after all named and unnamed layers.
Consequently, styles defined outside a layer take precedence over those inside a layer, regardless of specificity.
The @layer at-rule is utilized to create a cascade layer using three different ways.
-
First way is to create a cascade layer with a specific name, containing the corresponding CSS rules within.
@layer layer-name {rules}
-
Another way involves creating a named cascade layer without specifying any styles. It is possible to define multiple layers at the same time. The order of layer declaration determines priority; the last layer listed wins in the case of conflicting rules in multiple layers.
@layer layer-name; @layer layer-name, layer-name, layer-name;
-
The third way is to create a cascade layer without specifying a name. An anonymous cascade layer is established that functions similarly to the named layers. The precedence of anonymous layers is based on the order of their declaration order.
@layer {rules}
-
A cascade layer can also be created using the @import at-rule, placing the rules in the imported stylesheet. There is an example below.
@import "demo-layer.css" layer(layout);
Nesting layers
Layers can be nested, which means that you can create layers within other layers. This can be useful for organizing your CSS and giving certain layers priority over others.
@layer layer-name1 { @layer layer-name2 { } }
Syntax
A formal @layer syntax would be as follows:
@layer <layer-name>? { <stylesheet> } | @layer <layer-name># ;
Example
The following example demonstrates the styles declared outside a layer have higher priority than those declared within the layer.
<html> <head> <style> h1 { color: white; background-color:gray; } p { color: black; } @layer components { .container h1 { background-color: blue; color: red; padding: 10px; border-radius: 20px; } .container p { color: white; background-color: lightblue; font-size: 25px; } } </style> </head> <body> <div class="container"> <h1>HTML example to show CSS at-rules @layer</h1> <p>This is a sample text.</p> <button>Click Me</button> </div> </body> </html>
Assigning Rules To Existing Layers
Following example demonstrates assigning rules to existing layers. Two layers are initially created without any rules, after which CSS rules are later applied to both layers.
In the example given, @layer primary-demo, custom-demo; declares two CSS layers with the names primary-demo and custom-demo.
The styles defined in @layer primary-demo apply default formatting to elements with the class container.
The subsequent section @layer custom-demo overrides certain properties for the same class, allowing custom styling without affecting the base layer, demonstrating the use of CSS layers to organize and manage styles.
<html> <head> <style> @layer primary-demo, custom-demo; @layer primary-demo { .container { background-color: blue; color: black; font-family: Arial, sans-serif; font-size: 20px; line-height: 1.5; } } @layer custom-demo { .container { background-color: orange; color: white; padding: 10px; text-align: center; } } </style> </head> <body> <div class="container"> <h1>Welcome to My Website</h1> <p>This is a welcome quote with a blue background and white text.</p> <p><a href="#">Learn More</a></p> <h2>About Me</h2> <p>I am a web developer with experience in HTML, CSS, and JavaScript.</p> <p><a href="#">View My Portfolio</a></p> </div> </body> </html>