CSS Syntax & Inheritance
Learn how to write CSS rules correctly and master the hidden mechanics of property inheritance that define professional web design.
Expert Answer & Key Takeaways
Learn how to write CSS rules correctly and master the hidden mechanics of property inheritance that define professional web design.
1. CSS Syntax: The RuleSet
A CSS rule-set consists of a selector and a declaration block.
The Example
h1 {
color: blue;
font-size: 12px;
}| Element | Role | Description |
|---|---|---|
h1 | Selector | Targets the HTML element you want to style. |
{ ... } | Declaration Block | Contains one or more declarations separated by semicolons. |
color | Property | The specific style feature you want to change. |
blue | Value | The setting you apply to the property. |
[!TIP] Always use a semicolon (;) at the end of every declaration, and wrap your declarations in curly braces ({ }). This is the 'Grammar' of CSS.
2. Modern Example: Card Component
Professional CSS often uses classes (starting with
.) to group styles..card {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.card__title {
color: #333;
font-size: 1.5rem;
margin-bottom: 10px;
}🎯 Try it Yourself (Practice Task):
- Copy the code above into your editor.
- Change the
.card__titlecolor tocrimson. - Increase the
paddingof the.cardto40pxand observe the gap expansion.
3. CSS Inheritance Mastery
Some CSS properties are automatically passed from a parent element to its children. This is called Inheritance.
What Inherits (Mostly Typography):
| Property | Inherits? | Common Use |
|---|---|---|
color | ✅ Yes | Sets text color for all nested elements. |
font-family | ✅ Yes | Sets the font for the entire page if used on <body>. |
line-height | ✅ Yes | Maintains readable spacing between lines. |
text-align | ✅ Yes | Centers or aligns text across child divs. |
What Does NOT Inherit (The Box Model):
| Property | Inherits? | Why? |
|---|---|---|
margin / padding | ❌ No | Prevents messy, overlapping gaps in layouts. |
border | ❌ No | You don't want every child element having its own border. |
width / height | ❌ No | Sizes must be explicitly defined for each component layer. |
[!NOTE] Inheritance Secret: CSS Variables (--my-color) always inherit. This is why they are the #1 tool for modern web theming.
4. Senior Interview Corner
Q: What is the difference between
initial and revert in inheritance?A:
initial resets a property to the official CSS specification default (e.g., a div might become inline because that's the base spec). revert rolls back the style to the browser's native default (the User Agent stylesheet), which is safer and more predictable for UI elements like buttons and inputs.Q: Why is unitless
line-height (e.g., 1.5) better for inheritance?A: If you use pixels (e.g.,
24px), children inherit that fixed size. If a child has a larger font, the lines will overlap. If you use 1.5, children inherit the multiplier, ensuring the line height always scales proportionally to their own font size.Top Interview Questions
?Interview Question
Q:In the CSS rule 'p { color: red; }', which part is the 'selector'?
A:
p
?Interview Question
Q:Which of these properties will a child NOT inherit from its parent by default?
A:
padding
Course4All Engineering Team
Verified ExpertFrontend Architects
Focused on layout performance, modern CSS4+ features, and responsive design, our team provides the blueprint for professional web interfaces.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.