Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 1: Foundations & Selectors
CSS ⏱ 8 min read

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

Key CSS Concepts: 1. The Cascade: When multiple rules target the same element, CSS resolves conflicts based on Importance (`!important`), Specificity (inline > id > class > tag), and Source Order (later rules override earlier ones). 2. Inheritance: Properties related to text and typography (`color`, `font-family`, `line-height`) naturally cascade down to child elements unless overridden.

Syntax Structure

Syntax Blueprint
selector {
  property-1: value-1;
  property-2: value-2;
}

Code Example

CSS Code
/* 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

• `.course-card` targets any element with `class="course-card"`. • Every declaration ends with a semicolon `;`. • The `:hover` pseudo-class defines an interactive border color change on mouse hover.

Expected Output

A sleek white card with rounded corners, subtle border, soft drop shadow, and smooth hover interaction.

Real-World Application

Card components like this are the foundation of modern dashboards, learning platforms, and e-commerce listings.

Common Mistakes to Avoid

• Missing the closing semicolon `;` on a property, which invalidates the subsequent line. • Overusing `!important`, which destroys the natural cascade and makes future edits painful.

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.

Key Takeaways

• CSS syntax: `selector { property: value; }`. • Always terminate every declaration with a semicolon `;`. • Specificity and source order determine which rule wins during conflicts.