- Understand absolute vs relative CSS units
- Master responsive sizing with relative units
- Write advanced CSS selectors
- Combine selectors for precise targeting
- Optimize CSS for readability and maintainability
BMC201 - Web Technology
2026-02-09
Lecture 9
CSS Units & Advanced Selectors
Week 3 | Unit II: CSS Styling & JavaScript
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor
Learning Objectives
Absolute CSS Units
Fixed size units (don’t change):
body { font-size: 16px; } /* pixels */
h1 { font-size: 96pt; } /* points (print) */
p { font-size: 12mm; } /* millimeters (print) */
div { width: 10cm; } /* centimeters (print) */Best for: - Print stylesheets - Pixel-perfect designs - Fixed layouts (not recommended anymore)
Relative CSS Units (Recommended)
Viewport-based Units
Units based on screen size:
width: 100vw; /* 100% of viewport width */
height: 100vh; /* 100% of viewport height */
font-size: 5vw; /* 5% of viewport width */
padding: 2vh; /* 2% of viewport height */Perfect for: - Full-screen sections - Responsive typography - Hero images
Unit Comparison Chart
px (pixels) → Fixed size, precise
em → Relative to parent element
rem → Relative to root (html)
% → Relative to parent
vw/vh → Relative to viewport
Example:
Type (Element) Selectors
Select all elements of a type:
p { color: blue; } /* All paragraphs */
h1 { font-size: 32px; } /* All h1 headings */
body { background: white; } /* Body element */Group selectors:
Class and ID Selectors
/* Class selector (multiple use) */
.highlight {
background-color: yellow;
}
/* ID selector (unique) */
#header {
background-color: navy;
color: white;
}In HTML:
Descendant & Child Selectors
Descendant selector (any nested element):
Child selector (direct children only):
Sibling selector:
Attribute Selectors
Select by HTML attributes:
input[type="text"] { border: 1px solid blue; }
a[target="_blank"] { color: red; }
img[alt] { opacity: 0.9; }
a[href^="https"] { color: green; } /* Starts with */
a[href$=".pdf"] { color: orange; } /* Ends with */Very powerful for styling specific elements!
Pseudo-classes
Special selector states:
Pseudo-elements
Create virtual elements:
p::first-letter { font-size: 2em; } /* First letter */
p::first-line { font-weight: bold; } /* First line */
a::before { content: "[Link] "; } /* Before content */
a::after { content: " →"; } /* After content */Common use:
CSS Specificity & Cascade
How CSS decides which rule wins:
ID selectors → 100 points (#header)
Class selectors → 10 points (.highlight)
Element selectors → 1 point (p, h1)
Example:
/* 1 point */
p { color: blue; }
/* 10 points - wins! */
.highlight { color: red; }
/* 100 points - always wins */
#main { color: green; }Tip: Avoid !important - use specificity instead
Combining Selectors for Power
Real-world Selector Example
/* Style all links */
a { color: #0066cc; }
/* External links differently */
a[target="_blank"] { color: #ff9900; }
/* Links in navigation */
nav a { text-decoration: none; }
/* Links when user hovers */
nav a:hover { text-decoration: underline; }
/* First link in navigation */
nav a:first-child { font-weight: bold; }Resources & References
Questions?
Next: Lecture 10 - CSS Box Model & Positioning