CSS Content Styling
CSS Content Styling examines the cascade, the box model, selectors, and layout. The exam favors questions where you predict which rule wins or how large an element actually renders.
Cascade and specificity
- Specificity is counted as (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements); the higher score wins.
- Equal specificity means the later rule wins. Inline styles beat selectors;
!importantbeats both. - Inheritance passes text properties such as
colorandfont-family, but not layout properties such asmarginorborder.
Box model and units
- With
content-box, the declaredwidthcovers content only; padding and border add to the rendered size. box-sizing: border-boxincludes padding and border in the width.emis relative to the element's font size,remto the root font size; percentages depend on context, andvh/vwon the viewport.
Layout
- Common
displayvalues are block, inline, inline-block, flex, grid, and none. positionmay be static, relative, absolute, fixed, or sticky; absolute positions against the nearest positioned ancestor.- Flexbox arranges items in one dimension, grid in two; media queries adapt layout to viewport width.
Worked example
.card { width: 200px; padding: 10px; border: 2px solid; } under the default content-box renders 224px wide: 200 + 2×10 + 2×2. Adding box-sizing: border-box keeps the total at 200px and shrinks the content to 176px. Now compare #nav .item a:hover (1 ID, 2 classes, 1 element = 1-2-1) with .menu a (0-1-1): the ID selector wins regardless of source order.
Traps
Do not assume width includes padding, forget margin collapse between adjacent blocks, or use position: absolute without a positioned parent. Expecting z-index to work on static elements, confusing :hover with ::before, and reaching for !important are other frequent errors.