Style Rules in CSS

CSS comprises of style rules interpreted by the browser and then applied to the corresponding elements in your document. A style rule is made of three parts −

  • Selector - A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or <table> etc.
  • Property - A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS properties. They could be color, border etc.
  • Value - Values assigned to properties. For example, the color property can have value either red or #F1F1F1 etc

Syntax

You can put CSS Style Rule Syntax as follows:

selector { 
    property: value; 
}

Example

Here's a complete example showing how CSS style rules work:

<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            color: blue;
            font-size: 24px;
        }
        
        p {
            color: green;
            margin: 10px;
        }
        
        .highlight {
            background-color: yellow;
            padding: 5px;
        }
    </style>
</head>
<body>
    <h1>Welcome to CSS</h1>
    <p>This is a normal paragraph.</p>
    <p class="highlight">This paragraph has a yellow background.</p>
</body>
</html>

Types of Selectors

CSS supports different types of selectors:

  • Element Selector - Targets HTML elements (e.g., h1, p)
  • Class Selector - Targets elements with specific class (e.g., .highlight)
  • ID Selector - Targets element with specific ID (e.g., #header)

Multiple Properties

A single selector can have multiple property-value pairs:

h2 {
    color: red;
    font-size: 18px;
    text-align: center;
    margin-bottom: 20px;
}

Conclusion

CSS style rules follow a simple selector-property-value structure. Understanding this fundamental syntax is essential for styling web pages effectively.

Updated on: 2026-03-15T23:18:59+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements