CSS Syntax and Rule Structure
Understanding CSS syntax, cascade rules, and inheritance is the secret to avoiding layout bugs.
What is it?
A CSS rule-set consists of a selector and a declaration block enclosed in curly braces `{}`. Each declaration includes a property name, a colon, a value, and a terminating semicolon `;`.
Why is it used?
Proper syntax guarantees the browser parser correctly reads your styles without silently discarding rules due to missing semicolons or syntax errors.
Detailed Explanation
Syntax Structure
selector {
property-1: value-1;
property-2: value-2;
}
Code Example
/* Declaration Block with multiple properties */
.course-card {
background-color: #ffffff;
border: 1px solid #eceae4;
border-radius: 12px;
padding: 24px;
margin-bottom: 16px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}
.course-card:hover {
border-color: rgba(28, 28, 28, 0.4);
}
Explanation of the Code
Expected Output
Real-World Application
Card components like this are the foundation of modern dashboards, learning platforms, and e-commerce listings.
Common Mistakes to Avoid
Quick Practice Challenge
Write a CSS rule targeting a button class that sets padding to 12px 24px, background to black, text to white, and border-radius to 6px.