P2.1: CSS Selectors & Rules
CSS (Cascading Style Sheets) brings your structural HTML webpages to life. In this lesson, we will master how to declare styles and target elements using selectors and cascading rules.
CSS (Cascading Style Sheets) brings your structural HTML webpages to life. In this lesson, we will master how to declare styles and target elements using selectors and cascading rules.
Lesson: p2-1-selectors
CSS (Cascading Style Sheets) brings your structural HTML webpages to life. In this lesson, we will master how to declare styles and target elements using selectors and cascading rules.
CSS works by associating rules with HTML elements. A CSS rule contains two main parts: a selector (which points to the HTML elements to style) and a declaration (which contains styling properties and values).
/* Selector */
h1 {
/* Property : Value */
font-family: Arial;
color: #4ade80;
}
color, font-size, border).red, 12px, solid).{ } and must end with a semicolon ;.Clean, modular CSS stylesheets decouple presentation logic from document structure, improving code maintainability.
Relative improvement in page load speed and stylesheet file size.
You can add CSS rules to your HTML in three different ways:
Places all CSS rules in a separate file (e.g., styles.css) and links to it in the HTML <head>. This separates content from presentation and allows multiple pages to share the same stylesheet.
<link rel="stylesheet" type="text/css" href="css/styles.css" />
Places CSS rules directly inside a <style> element in the HTML <head>. Ideal for one-off single-page layouts.
<head>
<style type="text/css">
body { background-color: #0f172a; }
</style>
</head>
Applies styles directly to individual tags using the style attribute.
[!WARNING] Inline styles should generally be avoided because they blend content and presentation, making it harder to maintain styling across large websites.
<p style="color:red; font-size:12px;">This is inline styled text.</p>
Selectors tell the browser which HTML elements a rule applies to. You can target elements based on their type, nesting, or attributes.
| Selector Type | Syntax | Meaning | Example |
| :--- | :--- | :--- | :--- |
| Universal | * | Targets all elements on the page. | * { margin: 0; } |
| Type | h1, p | Matches HTML element names. | p { line-height: 1.5; } |
| Class | .note | Matches elements with the specified class. | .note { color: gray; } |
| ID | #intro | Matches the unique element with the specified id. | #intro { font-weight: bold; } |
| Child | li > a | Matches direct children of a parent element. | li > a { color: blue; } |
| Descendant | p a | Matches nested elements, regardless of depth. | p a { text-decoration: none; } |
| Adjacent Sibling | h1 + p | Matches the first sibling immediately after an element. | h1 + p { margin-top: 10px; } |
| General Sibling | h1 ~ p | Matches any sibling element that follows. | h1 ~ p { color: slate; } |
When multiple rules target the same element, browsers resolve conflicts using the cascade, which determines precedence based on three principles:
If two selectors are identical in specificity and target the same element, the rule that appears last in the stylesheet overrides previous ones.
i { color: green; }
i { color: red; } /* Wins! text will be red */
More specific selectors override more general selectors. Specificity is calculated using a scoring system:
p { color: white; } /* Specificity: 1 */
.note { color: gray; } /* Specificity: 10 (Wins!) */
p#intro { color: green; } /* Specificity: 101 (Wins over class!) */
!important FlagYou can write !important after any property value to override all specificity and ordering calculations. Use this flag sparingly, as it makes debugging layout rules extremely difficult.
p { color: red !important; } /* Overrides all specific classes or IDs */
Some CSS properties are inherited by child elements. For example, if you set font-family or color on the <body> element, all paragraphs and headings inside the body will inherit that font and color automatically.
However, layout-based properties (like background-color, border, margin, and padding) are not inherited to prevent visual clutter.
inherit Keyword:You can force any property to inherit a value from its parent by using the inherit value:
.page {
/* Inherits padding dimensions set on parent <body> */
padding: inherit;
}