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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Develop a Reverse Proxy With Caching in Go
  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.6K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!