This example shows how to avoid deep nesting by using clear, flat class names. The styles are easy to read and apply.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Avoiding Deep Nesting Example</title>
<style>
/* Deep nesting example (not recommended) */
/*
.container {
.box {
.text {
color: green;
}
}
}
*/
/* Flat CSS selectors (recommended) */
.container {
padding: 1rem;
border: 2px solid #333;
max-width: 300px;
margin: 1rem auto;
}
.container-box {
background-color: #e0f7fa;
padding: 1rem;
margin-bottom: 0.5rem;
}
.container-text {
color: green;
font-weight: bold;
font-size: 1.2rem;
}
</style>
</head>
<body>
<section class="container" aria-label="Example container">
<div class="container-box">
<p class="container-text">This text is green and bold.</p>
</div>
</section>
</body>
</html>