How to define multiple style rules for a single element in CSS?

You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example:

<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            color: #36C;
            font-weight: normal;
            letter-spacing: .4em;
            margin-bottom: 1em;
            text-transform: lowercase;
        }
    </style>
</head>
<body>
    <h1>Welcome to Our Website</h1>
    <p>This heading has multiple CSS properties applied.</p>
</body>
</html>

Key Points

Here all the property and value pairs are separated by a semicolon (;). You can keep them in a single line or multiple lines. For better readability, we keep them on separate lines.

Multiple Approaches

You can write multiple CSS properties in different formats:

Single Line Format

<!DOCTYPE html>
<html>
<head>
    <style>
        .compact { color: red; font-size: 18px; margin: 10px; }
    </style>
</head>
<body>
    <p class="compact">This text uses single-line CSS formatting.</p>
</body>
</html>

Multi-line Format (Recommended)

<!DOCTYPE html>
<html>
<head>
    <style>
        .readable {
            color: blue;
            font-size: 20px;
            font-weight: bold;
            text-align: center;
            padding: 15px;
            border: 2px solid #333;
        }
    </style>
</head>
<body>
    <div class="readable">This text uses multi-line CSS formatting for better readability.</div>
</body>
</html>

Grouping Related Properties

You can group related CSS properties together for better organization:

<!DOCTYPE html>
<html>
<head>
    <style>
        .card {
            /* Typography */
            font-family: Arial, sans-serif;
            font-size: 16px;
            line-height: 1.5;
            
            /* Colors */
            color: #333;
            background-color: #f9f9f9;
            
            /* Layout */
            width: 300px;
            padding: 20px;
            margin: 10px;
            
            /* Border and Effects */
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <div class="card">
        <h3>Card Title</h3>
        <p>This card demonstrates organized CSS properties with multiple style rules.</p>
    </div>
</body>
</html>

Best Practices

Approach Readability Maintainability Best For
Single Line Low Difficult Quick prototyping
Multi-line High Easy Production code
Grouped Properties Very High Very Easy Complex layouts

Conclusion

Use multi-line formatting with grouped properties for better readability and maintenance. Always separate properties with semicolons and organize related styles together for professional CSS code.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements