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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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.

Alexey Shepelev user avatar by
Alexey Shepelev
CORE ·
Mar. 13, 23 · Code Snippet
Like (5)
Save
Tweet
Share
2.18K 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.

Popular on DZone

  • Demystifying the Infrastructure as Code Landscape
  • When Should We Move to Microservices?
  • What Is API-First?
  • Choosing the Right Framework for Your Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: