CSS Syntax: Complete Guide to Rules, Selectors, Properties and Values

June 13, 2025

CSS (Cascading Style Sheets) is the cornerstone of web design, transforming plain HTML into visually stunning websites. Understanding CSS syntax is crucial for any web developer looking to create professional, maintainable stylesheets. This comprehensive guide will walk you through every aspect of CSS syntax, from basic rules to advanced selector techniques.

What is CSS Syntax?

CSS syntax refers to the set of rules and conventions that define how CSS code should be written. It consists of four main components that work together to style HTML elements:

  • Rules: Complete styling instructions
  • Selectors: Target specific HTML elements
  • Properties: Define what aspect to style
  • Values: Specify how the property should be applied

Basic CSS Rule Structure

Every CSS rule follows a consistent pattern that makes it easy to read and maintain. Here’s the fundamental structure:

selector {
    property: value;
    property: value;
}

Let’s break down each component:

The Anatomy of a CSS Rule

Example:

h1 {
    color: blue;
    font-size: 24px;
    text-align: center;
}

In this example:

  • h1 is the selector
  • color, font-size, and text-align are properties
  • blue, 24px, and center are values
  • The entire block is a rule

CSS Selectors: Targeting Elements

Selectors determine which HTML elements your CSS rules will affect. There are several types of selectors, each serving different purposes.

1. Element Selectors

The simplest selectors target HTML elements directly by their tag names:

p {
    color: #333;
    line-height: 1.6;
}

h2 {
    color: #2c3e50;
    margin-bottom: 10px;
}

2. Class Selectors

Class selectors target elements with specific class attributes, prefixed with a dot (.):

.highlight {
    background-color: yellow;
    padding: 5px;
}

.btn-primary {
    background-color: #007bff;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
}

3. ID Selectors

ID selectors target unique elements with specific ID attributes, prefixed with a hash (#):

#header {
    background-color: #34495e;
    height: 80px;
}

#footer {
    text-align: center;
    padding: 20px;
}

4. Attribute Selectors

Attribute selectors target elements based on their attributes:

input[type="text"] {
    border: 1px solid #ccc;
    padding: 8px;
}

a[href^="https"] {
    color: green;
}

5. Pseudo-Class Selectors

Pseudo-classes select elements in specific states:

a:hover {
    text-decoration: underline;
    color: #ff6b6b;
}

button:active {
    transform: scale(0.98);
}

Interactive CSS Selector Demo

Try Different Selectors:




Sample Heading

This is a paragraph with a class.

This is a regular paragraph.

This span also has the same class.

Click buttons above to see different selectors in action!

CSS Properties and Values

Properties define what aspect of an element you want to style, while values specify how that property should be applied. CSS offers hundreds of properties for controlling every aspect of element appearance and behavior.

Common Property Categories

Text Properties

font-family: Arial, sans-serif;
font-size: 16px;
font-weight: bold;
text-align: center;
text-decoration: underline;
line-height: 1.5;

Color and Background Properties

color: #333333;
background-color: #f0f0f0;
background-image: url('image.jpg');
background-size: cover;
opacity: 0.8;

Box Model Properties

width: 300px;
height: 200px;
padding: 20px;
margin: 10px;
border: 2px solid #ccc;
border-radius: 8px;

Value Types

CSS values come in various types, each serving specific purposes:

Length Values

  • Absolute: px, pt, cm, mm, in
  • Relative: em, rem, %, vh, vw
Examples:

font-size: 18px;      /* Absolute */
width: 50%;           /* Relative to parent */
margin: 2em;          /* Relative to font size */
height: 100vh;        /* Relative to viewport */

Color Values

  • Keywords: red, blue, transparent
  • Hex: #ff0000, #rgb
  • RGB/RGBA: rgb(255, 0, 0), rgba(255, 0, 0, 0.5)
  • HSL/HSLA: hsl(0, 100%, 50%)

Advanced Selector Combinations

CSS allows you to combine selectors for more precise targeting:

Descendant Combinator (Space)

/* Targets all p elements inside article elements */
article p {
    color: #555;
    margin-bottom: 15px;
}

Child Combinator (>)

/* Targets only direct li children of ul */
ul > li {
    list-style-type: disc;
    margin-left: 20px;
}

Adjacent Sibling Combinator (+)

/* Targets h2 immediately after h1 */
h1 + h2 {
    margin-top: 0;
    color: #666;
}

General Sibling Combinator (~)

/* Targets all p elements that follow h1 at same level */
h1 ~ p {
    font-style: italic;
}

CSS Specificity and the Cascade

Understanding how CSS determines which rules to apply is crucial for writing maintainable stylesheets. CSS uses specificity to resolve conflicts when multiple rules target the same element.

Specificity Hierarchy

  1. Inline styles (style attribute): 1000 points
  2. IDs: 100 points
  3. Classes, attributes, pseudo-classes: 10 points
  4. Elements and pseudo-elements: 1 point
Specificity Example:

/* Specificity: 1 */
p { color: blue; }

/* Specificity: 10 */
.highlight { color: red; }

/* Specificity: 100 */
#main { color: green; }

/* Specificity: 111 */
#main .highlight p { color: purple; }

Best Practices for CSS Syntax

1. Use Consistent Formatting

Good:

.button {
    display: inline-block;
    padding: 10px 20px;
    margin: 5px;
    background-color: #007bff;
    color: white;
    text-decoration: none;
    border-radius: 4px;
}
Avoid:

.button{display:inline-block;padding:10px 20px;margin:5px;background-color:#007bff;color:white;text-decoration:none;border-radius:4px}

2. Use Meaningful Class Names

Good: .navigation-menu, .primary-button, .article-header
Avoid: .red-text, .big-box, .style1

3. Group Related Properties

.card {
    /* Layout */
    display: flex;
    flex-direction: column;
    
    /* Box Model */
    width: 300px;
    padding: 20px;
    margin: 10px;
    
    /* Visual */
    background-color: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    
    /* Typography */
    font-family: Arial, sans-serif;
    line-height: 1.6;
}

Common CSS Syntax Errors

1. Missing Semicolons

Error:

.element {
    color: red
    font-size: 16px; /* This won't work */
}

2. Incorrect Property Names

Error:

.element {
    font-color: blue; /* Should be 'color' */
    text-colour: red; /* Should be 'color' */
}

3. Invalid Values

Error:

.element {
    width: 100%; /* Missing unit for non-zero values */
    margin: 10; /* Should be '10px' */
}

Modern CSS Syntax Features

CSS Custom Properties (Variables)

:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --font-size-base: 16px;
}

.button {
    background-color: var(--primary-color);
    font-size: var(--font-size-base);
    color: white;
}

CSS Grid and Flexbox

/* Flexbox */
.flex-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* Grid */
.grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
}

Conclusion

Mastering CSS syntax is fundamental to becoming a proficient web developer. By understanding the structure of CSS rules, the various types of selectors, and how properties and values work together, you’ll be able to create sophisticated, maintainable stylesheets.

Remember these key points:

  • Always use proper syntax with selectors, properties, and values
  • Choose appropriate selectors based on your targeting needs
  • Understand specificity to avoid conflicts
  • Follow best practices for clean, readable code
  • Stay updated with modern CSS features and techniques

Practice writing CSS regularly, experiment with different selectors and properties, and always validate your code to ensure it follows proper syntax. With time and experience, writing clean, efficient CSS will become second nature.