Every HTML element is treated as a box, which has:
div {
width: 200px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
📏 Total width = 200 + (10×2) + (5×2) = 230px.
▶ content-box (default) → width/height include only content. Padding & border add extra space.
▶ border-box → width/height include content + padding + border.
div {
width: 200px;
box-sizing: border-box;
}
✔ Ensures consistent layouts.
Replaced elements are those whose content is not controlled by CSS but by external resources (browser decides rendering).
Examples: <img>, <video>, <input>.
➡ display: none → removes element from DOM flow (no space).
➡ visibility: hidden → hides element but still takes up space.
▶ em → relative to parent font size.
▶ rem → relative to root (html).
▶ px → fixed, absolute.
➡ Float (old method): used for layouts but difficult (clearfix hacks).
➡ Flexbox/Grid (modern): flexible, responsive, no clearfix needed.
span { display: inline; } /* cannot set width */
div { display: block; } /* full width */
button { display: inline-block; } /* inline but can set width */
➡ width: fixed size.
➡ min-width: smallest allowed size.
➡ max-width: largest allowed size.