SELECTORS01
Specificity and the cascade
For declarations in the same origin and cascade layer, a more specific selector usually wins.
div { color: red; }
.panel { color: teal; }
#profile { color: gold; }
Type < class/attribute/pseudo-class < ID < inline style in the usual specificity comparison. Importance, origins, cascade layers, and ordering also matter.
SELECTORS02
Children versus descendants
A space matches any descendant; > requires a direct child.
.panel a { text-decoration: underline; }
.panel > a { font-weight: 700; }
Attribute selectors such as [data-status='ready'] and :has() also allow expressive selection in modern browsers.
SELECTORS03
Hover states
A pseudo-class changes a control's appearance when a pointer hovers over it.
.action:hover { background: seagreen; }
.action:focus-visible { outline: 3px solid gold; }
Do not depend on hover alone. Keyboard focus styles and visible labels help interaction remain accessible.
BOX MODEL04
Margin, padding, and borders
The box model flows from margin to border to padding to content.
.box { margin: 0 auto; padding: 16px; border: 2px solid; }
Negative margins are valid. Negative padding, borders, width, and height values are not.
BOX MODEL05
Border-box sizing
With border-box, declared width includes padding and borders.
.box { box-sizing: border-box; width: 280px; padding: 20px; border: 4px solid; }
The default for ordinary elements is content-box. Neither sizing mode includes margins in the declared width.
POSITIONING06
Containing blocks
Absolute positioning is usually relative to the nearest ancestor that establishes its containing block.
.card { position: relative; }
.badge { position: absolute; top: 12px; right: 12px; }
Position: relative retains the element's flow space. Fixed positioning usually uses the viewport, subject to containing-block exceptions.
POSITIONING07
Stacking order
z-index is not limited to values below 1000, and stacking contexts can limit its effect.
.layer { position: relative; z-index: 1000; }
Positioned elements and flex/grid items can use z-index. A large value cannot escape an ancestor stacking context.
LAYOUT08
Floats and clearing
Floats influence text wrapping and can require containment to give the parent height.
.container { display: flow-root; }
.thumbnail { float: left; margin-right: 1rem; }
display: flow-root is a modern containment option. A clearfix with ::after { content: ''; display: block; clear: both; } is another approach.
LAYOUT09
Horizontal and vertical centring
Auto inline margins can centre a width-constrained block; flexbox centres in either axis.
.card { width: 65%; margin: 0 auto; }
.shell { display: flex; align-items: center; justify-content: center; }
With display: table-cell, vertical-align: middle aligns cell content vertically. Aligning table cells differs from styling the table itself.
LAYOUT10
Space-filling flex children
Let the content area grow and prevent an adjacent action from shrinking.
.row { display: flex; gap: 12px; }
.row__content { flex: 1; min-width: 0; }
.row__action { flex: none; }
flex: 1 makes the content expand; flex: none preserves the button's size. This works well for responsive commerce navigation.
RESPONSIVE11
Viewport media queries
max-width applies when the viewport is at or below the specified width.
@media (max-width: 768px) {
.cards { grid-template-columns: 1fr; }
}
Choose breakpoints around content needs rather than assuming a particular device model.
VISIBILITY12
Hidden versus removed
Three ways of hiding an element have different layout and interaction outcomes.
.invisible { visibility: hidden; }
.removed { display: none; }
.transparent { opacity: 0; }
visibility: hidden keeps space but is not normally pointer-interactive; display: none removes layout space; opacity: 0 keeps space and can still receive pointer input.
RESPONSIVE13
Fonts and fallback
The font shorthand sets several font properties together; @font-face declares download sources.
@font-face {
font-family: 'Sample';
src: local('Sample'), url('/sample.woff2') format('woff2');
}
A browser only checks for a named local font when a local() source is specified. WOFF2 is a common webfont format. No font binaries are included in this demo.
LAYOUT14
A footer that stays below content
A flex-column page can keep a short page's footer at the bottom while preserving scrolling.
.page { min-height: 100vh; display: flex; flex-direction: column; }
.main { flex: 1; }
This modern flexbox layout avoids positioning the footer fixed over long content.