DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • React Server Components (RSC): The Future of React
  • The Role of JavaScript Frameworks in Modern Web Development
  • Optimizing React Apps for Web Development: A Comprehensive Guide
  • Modern Web Development With ReactJS: Best Practices and Tips

Trending

  • Manual Investigation: The Hidden Bottleneck in Incident Response
  • Lease Coordination Under Serializable Isolation in CockroachDB
  • Java in a Container: Efficient Development and Deployment With Docker
  • Building a Skill-Based Agentic Reviewer with Claude Code: A Practical Guide Using Skills.MD, MCP Servers, Tools, and Tasks
  1. DZone
  2. Coding
  3. Languages
  4. Practical Example of Using CSS Layer

Practical Example of Using CSS Layer

Explore this brief tutorial to find out how CSS Layers are another thing that is designed to make life easier.

By 
Alexey Shepelev user avatar
Alexey Shepelev
DZone Core CORE ·
Mar. 13, 23 · Code Snippet
Likes (6)
Comment
Save
Tweet
Share
3.9K Views

Join the DZone community and get the full member experience.

Join For Free

Let’s say we are developing a component library. 

Let’s say we are using React. 

Let’s say it has a button component.

Conventionally, it will look like this:

JavaScript
 
// CustomButton.js

import './CustomButton.css'

const CustomButton = ({ children, className = '' }) => {
	const customClass = 'CustomButton' + className;
	return <button className={customClass}>{children}</button>
}


And the styles will look like this:

CSS
 
/* CustomButton.css */

.CustomButton {
	background: aquamarine;
	padding: 4px 12px;
	border: none;
}


Here, the developers come to us and say: “The button is great, but we need a link in the form of a button!” No problem! Let’s add a property:

JavaScript
 
// CustomButton.js

import './CustomButton.css'

const CustomButton = ({ children, Component = 'button', className = '' }) => {
	const customClass = 'CustomButton' + className;
	return <Component className={customClass}>{children}</Component>
}


Example of use:

JavaScript
 
const linkCustomButton = (
	<CustomButton Component="a" href="https://google.com">
		Google.com
	</CustomButton>
);


Oops, "a" is an inline element. What’s the matter, you ask? The button looks the way it did. Not exactly! Imagine that users want to add space above the button:

JavaScript
 
// App.js

<CustomButton className="myCustomButton">Click it!</CustomButton>
CSS
 
/* App.css */

.myCustomButton {
	margin-top: 10px;
}


The inline element will ignore this margin.

That’s okay! We’ve been in worse jams than this. Let’s change the style of the button:

CSS
 
/* CustomButton.css */

.CustomButton {
	background: aquamarine;
	padding: 4px 12px;
	border: none;
	display: inline-block;
}


No, there is another problem, much more vile and intractable. Watch the hands! Imagine the following way of using a button in a project. Let’s say there is some kind of loading state, and if it is true, the button needs to be hidden:

JavaScript
 
// App.js

<CustomButton className={loading ? 'hidden' : ''} />


The problem is that .hidden and .CustomButton have the same weight, and they both claim the display property. This means that the CSS parser will have to figure out the winner while being guided by the order. The order is the last tier of the cascade. When your CSS files are broken into modules, you cannot rely on their specific order of appearance in the final bundle. As a result, this will lead to situations in which one or another selector wins. So, what do you do?

Layer to the Rescue

The cascade Layer is located exactly before the Specificity and Order:

  1. Importance
  2. Context
  3. Layer (hi)
  4. Specificity
  5. Order

Layer allows you to flexibly set different levels of your style sheets. In our example, there may be three such levels (or maybe more):

  1. A reset style sheet 
  2. A library-style sheet 
  3. An application-style sheet

Let’s set these levels.

CSS
 
@layer reset, library;

/* CustomButton.css */

@layer reset {
	.CustomButton {
		display: inline-block;
	}
}

@layer library {
	.CustomButton {
		background: aquamarine;
		padding: 4px 12px;
		border: none;
	}
}


But wait: we wanted to set three levels, yet we set only two. This is specific to the layer cascade. Anything defined outside of any level automatically becomes the highest priority. We don’t need library, too, but I used it for clarity.

It’s cool that all non-level styles win because users don’t have to wrap their overriding styles in some @layer app.

That is, this style will eventually override the one in @layer reset.

CSS
 
/* App.css */

.hidden {
	display: none;
}


Is it convenient? I think it’s just fantastic, largely because browsers still don’t have this cascade :-( 

Yet.

CSS JavaScript Property (programming) Web development React (JavaScript library)

Opinions expressed by DZone contributors are their own.

Related

  • React Server Components (RSC): The Future of React
  • The Role of JavaScript Frameworks in Modern Web Development
  • Optimizing React Apps for Web Development: A Comprehensive Guide
  • Modern Web Development With ReactJS: Best Practices and Tips

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook