1-10 — Fundamentals
Every HTML element is treated as a rectangular box made of four layers: content, padding, border, and margin. By default (box-sizing: content-box), width and height apply only to the content area — padding and border add on top of that. Switching to border-box makes width include padding and border, which is almost always more intuitive.
div {
box-sizing: border-box; /* width includes padding + border */
width: 300px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}Semantic elements clearly describe their purpose to both browsers and developers. They improve accessibility (screen readers understand the structure), help search engine crawlers, and make code easier to maintain. Non-semantic elements like <div> and <span> carry no inherent meaning.
<header>Site header</header>
<nav>Main navigation</nav>
<main>
<article>Blog post content</article>
<aside>Related links</aside>
</main>
<footer>Site footer</footer>top/left/right/bottom have no effect.relative until a scroll threshold is met, then becomes fixed..relative { position: relative; top: 10px; }
.absolute { position: absolute; top: 0; left: 0; }
.fixed { position: fixed; top: 0; width: 100%; }
.sticky { position: sticky; top: 0; }Three modern approaches — Flexbox and Grid are preferred:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}.container {
display: grid;
place-items: center;
height: 100vh;
}.parent { position: relative; }
.child {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}Pseudo-classes target elements in a specific state without adding extra HTML markup. They allow dynamic styling purely in CSS.
a:hover { color: red; }
input:focus { border-color: blue; }
li:first-child { font-weight: bold; }
p:nth-child(2) { color: green; }
button:disabled { opacity: 0.5; }.name) — reusable, can appear on many elements. Specificity: 0-1-0.#name) — must be unique per page. Specificity: 1-0-0 (higher than class)..card { background: #fff; } /* reusable */
#hero { font-size: 3rem; } /* unique element */@media queries to change styles at specific breakpoints.%, vw, rem) instead of fixed px for sizing.<meta name="viewport" content="width=device-width, initial-scale=1"> in the HTML head.srcset or CSS max-width: 100%..container { display: flex; flex-direction: row; }
@media (max-width: 600px) {
.container { flex-direction: column; }
}<span>, <a>.<div>, <p>.z-index controls the stacking order of overlapping positioned elements (any position except static). Higher value = on top. A stacking context is an isolated layer — created by properties like opacity < 1, transform, filter, or position + z-index. z-index comparisons only work within the same stacking context.
.modal-backdrop { position: fixed; z-index: 100; }
.modal { position: fixed; z-index: 200; } /* above backdrop */div ul li a span.@import in CSS — it's synchronous; use bundler imports instead.