Skip to content

Commit f8b74ca

Browse files
authored
Merge pull request #126 from codeharborhub/dev-1
done advanced features
2 parents f79563c + 6db87e3 commit f8b74ca

File tree

7 files changed

+949
-7
lines changed

7 files changed

+949
-7
lines changed
Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,160 @@
1-
<ComingSoon />
1+
---
2+
title: "CSS Cascade Layers (@layer)"
3+
description: "Learn how to use the @layer rule to organize CSS styles into distinct layers, gaining control over the cascade and solving specificity conflicts in complex stylesheets."
4+
keywords: [CSS layers, '@layer', cascade control, specificity, CSS organization, component layers, framework layers, layer precedence, unlayered styles]
5+
tags: [CSS layers, '@layer', cascade control, specificity, CSS organization, component layers, framework layers, layer precedence, unlayered styles]
6+
sidebar_label: Cascade Layers
7+
---
8+
9+
CSS Cascade Layers, introduced by the **`@layer`** at-rule, provide a powerful mechanism for organizing CSS and controlling the **cascade** more predictably. They solve a long-standing problem in large-scale CSS projects: how to manage specificity conflicts between different sources of styles (e.g., framework, base, components, and utilities).
10+
11+
**The Core Concept:** Styles within a higher-priority layer will *always* win over styles in a lower-priority layer, **regardless of the selector's specificity** within those layers.
12+
13+
<AdsComponent />
14+
<br />
15+
16+
## 1. The Cascade Layer Order
17+
18+
The final result of a style depends on a fixed order of precedence. Cascade Layers slot into the standard cascade but allow you to define a sub-order for your own styles.
19+
20+
**The Key Layer Precedence Rule:**
21+
22+
* **Earlier layers have lower priority (less important).**
23+
* **Later layers have higher priority (more important).**
24+
25+
This means a simple selector in a late-defined layer can override a highly specific selector in an early-defined layer.
26+
27+
### Order of Author Styles (Lowest to Highest Priority)
28+
29+
1. **Styles within the first defined `@layer`** (e.g., `reset`).
30+
2. **Styles within later defined `@layer`s** (e.g., `utilities`).
31+
3. **Unlayered Styles** (CSS not wrapped in any `@layer` block).
32+
4. **Author `!important` declarations.**
33+
34+
35+
## 2. Defining and Ordering Layers
36+
37+
You must define the order of your layers using a single `@layer` statement, typically placed at the very top of your CSS file.
38+
39+
### 2.1. Defining the Order
40+
41+
```css
42+
/* 1. Define the layer order (Lowest to Highest Precedence) */
43+
@layer reset, framework, base, components, utilities;
44+
```
45+
46+
In this setup: `reset` has the lowest priority, and `utilities` has the highest priority among the layers.
47+
48+
<AdsComponent />
49+
<br />
50+
51+
### 2.2. The Specificity vs. Layer Rule
52+
53+
This is the most critical concept to understand:
54+
55+
> **Layer Order Trumps Specificity:** The specificity of a selector only matters *within* its own layer. Once two properties conflict across two different layers, the layer order determines the winner, ignoring specificity.
56+
57+
:::tip Layer Wins
58+
Consider two rules:
59+
1. `.box` (`specificity: 0,1,0`) in the `base` layer.
60+
2. `#box-id` (`specificity: 1,0,0`) in the `utilities` layer.
61+
62+
If `utilities` is defined *after* `base`, the simple `.box` utility will override the highly specific `#box-id` selector, because the `utilities` layer has higher precedence. This is how layers flip the cascade.
63+
:::
64+
65+
### 2.3. Creating Layer Blocks
66+
67+
You then wrap your styles in named `@layer` blocks. You can define layers across multiple files or multiple blocks in the same file; the browser will stitch them together based on the initial ordering statement.
68+
69+
```css title="styles.css"
70+
/* -- 1. Reset Layer (Lowest Priority) -- */
71+
@layer reset {
72+
/* This universal selector style is easily overridden by any other layer. */
73+
* {
74+
box-sizing: border-box;
75+
}
76+
}
77+
78+
/* -- 2. Components Layer (Mid Priority) -- */
79+
@layer components {
80+
/* Specific component style */
81+
.card-title {
82+
color: var(--theme-color);
83+
}
84+
}
85+
```
86+
87+
## 3. Unlayered Styles (The Override)
88+
89+
Styles that are **not** included in any `@layer` block are referred to as **unlayered styles**.
90+
91+
Unlayered styles always have a higher precedence than **all** layered styles, regardless of their position in the file or the layer order.
92+
93+
:::warning Unlayered Priority
94+
Use unlayered styles sparingly, typically only for final, non-negotiable overrides or small third-party snippets where integrating them into a layer is impractical. Relying heavily on unlayered styles defeats the purpose of managing the cascade with layers. The goal should be to make 99% of your styles layered.
95+
:::
96+
97+
```css title="styles.css"
98+
/* This style is NOT wrapped in @layer. Specificity: 0,1,0 */
99+
.final-override {
100+
/* This will win over ALL layered styles, even if the selector is simpler. */
101+
background-color: green;
102+
}
103+
104+
/* This is a layered style that loses to the unlayered style above. Specificity: 0,1,0 */
105+
@layer utilities {
106+
.final-override {
107+
background-color: red;
108+
}
109+
}
110+
```
111+
112+
<AdsComponent />
113+
<br />
114+
115+
## 4. Layer Organization and Naming
116+
117+
A common layer strategy is to move from the least specific, broadest styles to the most specific, overriding styles.
118+
119+
| Layer Name | Typical Content | Priority | Role |
120+
| :--- | :--- | :--- | :--- |
121+
| **`reset`** | CSS resets (e.g., normalize.css), global box-sizing. | Lowest | Provides a clean slate. |
122+
| **`framework`** | Third-party libraries (Tailwind base, Bootstrap). | Low | Imports external defaults. |
123+
| **`base`** | Element selectors (`h1`, `p`, `a`), custom fonts, root variables. | Mid-Low | Defines project defaults. |
124+
| **`components`** | Reusable, complex class selectors (e.g., `.modal`, `.card`). | Mid-High | Defines component structure. |
125+
| **`utilities`** | Single-purpose, overriding classes (e.g., `.u-hidden`, `.u-text-red`). | Highest | Ensures utility classes always win. |
126+
127+
### Nested and Imported Layers
128+
129+
Layers can be nested, and styles can be imported directly into a layer, making framework integration seamless.
130+
131+
:::info Layer Nesting
132+
Nesting layers helps organize large component libraries (e.g., separating forms from navigation).
133+
134+
```css title="styles.css"
135+
/* Initial layer declaration must include the top level: */
136+
@layer framework, components;
137+
138+
/* Importing an external file into the 'framework' layer */
139+
@import url('bootstrap.css') layer(framework);
140+
141+
/* Defining a nested layer within components */
142+
@layer components.forms {
143+
/* Styles for form components */
144+
.input-field { border-color: gray; }
145+
}
146+
```
147+
Styles in `components.forms` have the same precedence as `components` styles.
148+
:::
149+
150+
<AdsComponent />
151+
<br />
152+
153+
## Interactive CSS Layers Demo
154+
155+
This example demonstrates the precedence: The `utility` layer is defined later than the `component` layer, so the utility's style always wins. The `unlayered` style wins over both.
156+
157+
<CodePenEmbed
158+
title="Interactive CSS Layers Demo"
159+
penId="zxqajqv"
160+
/>
Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,114 @@
1-
<ComingSoon />
1+
---
2+
title: "Advanced CSS Functions (calc, clamp, min, max)"
3+
description: "Explore essential CSS functions like calc(), clamp(), min(), and max() for dynamic sizing, combining fixed and fluid units to create highly robust and mathematical layouts."
4+
keywords: [CSS functions, calc, clamp, min, max, dynamic sizing, mathematical layouts, fluid typography, responsiveness]
5+
tags: [CSS functions, calc, clamp, min, max, dynamic sizing, mathematical layouts, fluid typography, responsiveness]
6+
sidebar_label: CSS Functions
7+
---
8+
9+
CSS is no longer a static styling language. Modern CSS includes powerful functions that allow you to perform calculations and create dynamic, responsive values that adapt to the browser environment.
10+
11+
The four most important functions for creating robust layouts are `calc()`, `clamp()`, `min()`, and `max()`.
12+
13+
<AdsComponent />
14+
<br />
15+
16+
## 1. `calc()`: Mathematical Calculations
17+
18+
The `calc()` function allows you to perform basic arithmetic (addition, subtraction, multiplication, division) within CSS property values.
19+
20+
Its primary benefit is the ability to mix different unit types (e.g., mixing fixed pixels with fluid percentages or viewport units).
21+
22+
### 1.1. Syntax and Rules
23+
24+
```css title="styles.css"
25+
/* Example: 50% of the parent width minus a fixed 20px of padding */
26+
width: calc(50% - 20px);
27+
```
28+
29+
**Crucial Rule for `calc()`:**
30+
31+
* **Spaces are mandatory** around the `+` and `-` operators. `calc(50% - 20px)` is correct. `calc(50%-20px)` will break.
32+
* Spaces are optional for `*` and `/`.
33+
34+
### 1.2. Practical Use Cases
35+
36+
| Use Case | Example | Description |
37+
| :--- | :--- | :--- |
38+
| **Gutter Management** | `width: calc(100% / 3 - 2rem);` | Calculates the width of three equal columns, subtracting necessary margin/gap space. |
39+
| **Viewport Offset** | `height: calc(100vh - 80px);` | Sets an element height to the full viewport height minus a fixed header height. |
40+
| **Variable Manipulation** | `padding: calc(var(--base-unit) * 1.5);` | Scales a CSS variable by a factor. |
41+
42+
<AdsComponent />
43+
<br />
44+
45+
## 2. `clamp()`: Bounding Fluid Values
46+
47+
The `clamp()` function pins a value between a minimum and a maximum size. This is the cornerstone of modern, fluid typography.
48+
49+
`clamp()` takes three arguments: **Minimum Value, Preferred (Fluid) Value, Maximum Value**.
50+
51+
```css title="styles.css"
52+
/* Example: Font size that scales between 16px and 24px based on viewport width
53+
clamp( MIN, PREFERRED, MAX )
54+
```
55+
56+
### 2.1. Fluid Typography Example
57+
58+
```css title="styles.css"
59+
h1 {
60+
/* Font size will never be smaller than 3rem,
61+
never larger than 5rem, and will scale fluidly in between. */
62+
font-size: clamp(3rem, 2rem + 2vw, 5rem);
63+
}
64+
```
65+
66+
* If the result of `2rem + 2vw` is less than `3rem`, the browser uses `3rem`.
67+
* If the result is greater than `5rem`, the browser uses `5rem`.
68+
* Otherwise, the browser uses the fluid value.
69+
70+
## 3. `min()` and `max()`: Setting Limits
71+
72+
The `min()` and `max()` functions allow you to choose the smallest or largest value from a comma-separated list of expressions. This is excellent for creating resilient components that avoid common layout pitfalls.
73+
74+
### 3.1. `min()`: Selecting the Smaller Value
75+
76+
`min(value1, value2, ...)` chooses the smallest value.
77+
78+
**Use Case: Preventing Element Overflow.**
79+
Imagine you want a container to be $50\%$ of the screen width, but never wider than a fixed 800px.
80+
81+
```css title="styles.css"
82+
.container {
83+
/* The width will be 50% OR 800px, whichever is smaller.
84+
This effectively sets the max-width dynamically. */
85+
width: min(50vw, 800px);
86+
}
87+
```
88+
89+
<AdsComponent />
90+
<br />
91+
92+
### 3.2. `max()`: Selecting the Larger Value
93+
94+
`max(value1, value2, ...)` chooses the largest value.
95+
96+
**Use Case: Ensuring Minimum Size.**
97+
Imagine you want a sidebar to be 20% of the screen width, but it must be at least 250px wide to hold its content.
98+
99+
```css title="styles.css"
100+
.sidebar {
101+
/* The width will be 20% OR 250px, whichever is larger.
102+
This effectively sets the min-width dynamically. */
103+
width: max(20vw, 250px);
104+
}
105+
```
106+
107+
## Interactive CSS Functions Demo
108+
109+
This example uses `calc()`, `min()`, and `max()` to demonstrate dynamic sizing in a layout.
110+
111+
<CodePenEmbed
112+
title="Interactive CSS Functions Demo"
113+
penId="XJdYZMz"
114+
/>

0 commit comments

Comments
 (0)