Unit 1: Core CSS Foundations & SyntaxLesson #1 / 8

Lesson 1: CSS Selectors, Rules & The Cascade

Er. Manoj Kumar — AuthorEr. Manoj KumarLast Updated: 26 Aug, 2026

1. What is CSS and How Does it Work?

CSS (Cascading Style Sheets) controls the visual presentation, colors, typography, and responsive layouts of HTML webpages.

  • Separation of Concerns: HTML defines semantic document structure; CSS defines presentation and visual theme.
  • CSS Rule Structure: A rule consists of a selector (pointing to the target element) and a declaration block (property-value pairs enclosed in curly braces).
  • The Semicolon Rule: Every CSS declaration MUST terminate with a semicolon (;).
  • HTML5 Web Code (index.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>CSS Selectors Demo</title>
    <style>
    /* Tag / Type Selector */
    h1 {
    color: #04AA6D;
    font-family: Arial, sans-serif;
    text-align: center;
    }
    /* Class Selector */
    .highlight-card {
    background-color: #17181c;
    color: #ffffff;
    padding: 16px;
    border-radius: 8px;
    border: 1px solid #04AA6D;
    }
    /* ID Selector */
    #primary-btn {
    background-color: #04AA6D;
    color: #ffffff;
    font-weight: bold;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    cursor: pointer;
    }
    </style>
    </head>
    <body>
    <h1>Welcome to CSS Styling</h1>
    <div class="highlight-card">
    <p>This card is styled using class and ID selectors.</p>
    <button id="primary-btn">Get Started</button>
    </div>
    </body>
    </html>

    2. The Three Methods of Applying CSS

  • External Stylesheet (Recommended): Linked via <link rel="stylesheet" href="style.css"> in the HTML <head>. Enables browser caching and site-wide reusability.
  • Internal Stylesheet: Written inside <style> tags inside the HTML <head>. Useful for single-page templates.
  • Inline Styles: Written directly on tags using the style="..." attribute. Discouraged for scalable production apps due to maintenance bloat.

  • 3. CSS Specificity Hierarchy Table

    Selector TypeExampleSpecificity ScorePriority
    Inline Stylestyle="color: red;"1000Highest
    ID Selector#header100Very High
    Class / Attribute.btn, [type="text"]10Medium
    Element / Typeh1, p, div1Low
    Universal0Lowest

    4. Combinator Selectors

  • Descendant Selector (div p): Targets all <p> elements nested anywhere inside <div>.
  • Direct Child Selector (div > p): Targets only <p> elements that are direct immediate children of <div>.
  • Adjacent Sibling (h1 + p): Targets the first <p> immediately following an <h1>.
  • General Sibling (h1 ~ p): Targets all <p> sibling elements that follow an <h1>.
  • Interactive Knowledge Check

    Test your understanding of Lesson #1 concepts

    Which CSS selector has the highest specificity weight in standard stylesheet calculation?