In this lesson, you'll explore the theoretical foundations of CSS selectors and properties. Understanding how selectors work conceptually and the purpose behind various properties will help you write more efficient and maintainable stylesheets.
CSS selectors work through a pattern-matching algorithm that evaluates HTML elements against specific criteria. The browser processes selectors from right to left, which is an important performance consideration.
The descendant selector (space) creates a hierarchical relationship, matching any element that is a descendant at any level:
/* Matches any paragraph inside any div, regardless of depth */
div p { color: blue; }
Theory: This selector is powerful but can be performance-intensive on deeply nested DOM structures because it must traverse up the DOM tree.
The child selector (>) creates a direct parent-child relationship, limiting the match to immediate children:
/* Only paragraphs that are direct children of divs */
div > p { font-weight: bold; }
Theory: More performant than descendant selectors as it only checks the immediate parent-child relationship.
Attribute selectors provide pattern matching based on HTML attributes, enabling styling based on element characteristics rather than structure.
/* Any element with a title attribute */
[title] { border-bottom: 1px dotted; }
/* Specific attribute value */
input[type="text"] { width: 100%; }
Theory: Attribute selectors enable component-based styling where elements with similar behaviors can be styled consistently regardless of their semantic meaning.
/* Links ending with .pdf */
a[href$=".pdf"] { background: url('pdf-icon.png') no-repeat right center; }
/* Links starting with https:// */
a[href^="https://"] { color: green; }
Theory: These selectors use regular expression-like patterns to match attribute values, enabling dynamic styling based on content characteristics.
Pseudo-classes represent element states that cannot be determined from the document tree alone. They enable dynamic styling based on user interaction and document structure.
a:hover { color: red; }
input:focus { outline: 2px solid blue; }
Theory: These pseudo-classes respond to user interactions and browser state, creating responsive user interfaces without JavaScript.
li:first-child { font-weight: bold; }
li:nth-child(odd) { background-color: #f9f9f9; }
Theory: Structural pseudo-classes enable styling based on element position within the DOM, supporting patterns like alternating row colors and special first/last element styling.
Pseudo-elements create virtual elements that don't exist in the DOM but can be styled, enabling decorative and structural enhancements.
p::first-letter { font-size: 2em; }
.warning::before { content: "⚠️ "; }
Theory: Pseudo-elements allow styling of specific parts of elements or insertion of decorative content without modifying the HTML structure.
CSS properties are organized into functional categories that control different aspects of element presentation:
Typography properties control text rendering and font characteristics:
.text-example {
font-family: 'Arial', sans-serif;
font-size: 16px;
font-weight: bold;
color: #333;
text-align: center;
}
Theory: Typography properties establish visual hierarchy and readability through controlled variation in font characteristics and text layout.
The box model provides a consistent spatial metaphor for all elements:
.box-example {
width: 200px;
padding: 20px;
border: 2px solid #333;
margin: 10px;
}
Theory: The box model treats every element as a rectangular box with content, padding, border, and margin layers, creating predictable spacing and layout behavior.
Visual properties control colors, backgrounds, and visual effects:
.visual-example {
background-color: #f0f0f0;
border-radius: 5px;
opacity: 0.8;
box-shadow: 2px 2px 5px rgba(0,0,0,0.2);
}
Theory: Visual properties enhance the aesthetic appeal and provide visual feedback through color, transparency, and depth effects.
The box model is fundamental to understanding CSS layout. Every element is rendered as a series of nested rectangles:
Content → Padding → Border → Margin
Theoretical Implications:
/* Traditional model: width applies only to content */
.content-box { box-sizing: content-box; }
/* Modern model: width includes padding and border */
.border-box { box-sizing: border-box; }
Theory: The border-box model simplifies layout calculations by including padding and borders in the specified width, making responsive design more intuitive.
Specificity is the algorithm browsers use to determine which CSS rule applies when multiple rules target the same element.
The specificity hierarchy creates a weighted scoring system:
/* Specificity: 1 */
p { color: blue; }
/* Specificity: 10 */
.text { color: green; }
/* Specificity: 100 */
#content { color: red; }
/* Specificity: 111 */
#content.text p { color: purple; }
Theory: Specificity creates a predictable cascade where more specific selectors override less specific ones, enabling modular and maintainable stylesheets.
.important { color: red !important; }
Theory: The !important declaration breaks the normal specificity cascade and should be used sparingly as it can make debugging difficult.
CSS provides different unit systems for measuring lengths and sizes:
.absolute { font-size: 16px; width: 2in; }
Theory: Absolute units provide consistent sizing regardless of viewport or device characteristics, useful for print media and specific design requirements.
.relative { font-size: 1.2em; width: 80%; height: 50vh; }
Theory: Relative units create responsive designs that adapt to different screen sizes and user preferences, supporting accessibility and device diversity.
CSS provides multiple color representation systems:
.color-examples {
color: #ff0000; /* Hexadecimal */
color: rgb(255,0,0); /* RGB */
color: hsl(0,100%,50%); /* HSL */
color: red; /* Named */
}
Theory: Different color systems serve different use cases: hex for web standards, RGB for programmatic control, HSL for intuitive color manipulation, and named colors for readability.
Theory: Efficient selectors improve rendering performance and maintainability:
Theory: Organized properties improve readability and maintainability:
.organized {
/* Positioning */
position: relative;
top: 0;
left: 0;
/* Box Model */
width: 100px;
padding: 10px;
margin: 5px;
/* Typography */
font-size: 14px;
color: #333;
/* Visual */
background: white;
border: 1px solid #ccc;
}
:root {
--primary-color: #007bff;
--border-radius: 4px;
--spacing: 16px;
}
.button {
background: var(--primary-color);
border-radius: var(--border-radius);
padding: var(--spacing);
}
Theory: CSS variables enable systematic theming and consistent design systems while reducing duplication and improving maintainability.
/* Base component */
.card {
background: white;
border-radius: 8px;
padding: 16px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* Component modifier */
.card--featured {
border: 2px solid #007bff;
}
/* Component element */
.card__title {
font-size: 1.2em;
font-weight: bold;
margin-bottom: 8px;
}
Theory: Component-based styling creates modular, reusable styles that mirror component architecture in modern web development.
.button {
background: #007bff;
color: white;
transition: all 0.3s ease;
}
.button:hover {
background: #0056b3;
transform: translateY(-2px);
}
.button:active {
transform: translateY(0);
}
Theory: Pseudo-classes enable state-based styling that provides visual feedback for user interactions, enhancing user experience without JavaScript.
Create a theoretical design system documentation for a simple component library:
In this lesson, you explored the theoretical foundations of CSS selectors and properties:
In the next lesson, you'll learn about modern CSS layout techniques including Flexbox and Grid, which build upon these theoretical foundations to create sophisticated responsive layouts.