What it is
CSS applies presentation rules to elements. The box model includes content, padding, borders, and margins. Layout tools such as Grid and Flexbox arrange those boxes, while the cascade decides which competing declarations apply.
Why you need it
A fixed screenshot is not a layout specification. Real content grows, labels change length, and people zoom. Flexible constraints let the page adapt without hiding controls.
How it works
- Start with readable document order.
- Define spacing and box sizing.
- Choose a layout that fits the relationship between elements.
- Check narrow screens, long text, and zoom before adding visual polish.
A grid that wraps without a fixed column count
* { box-sizing: border-box; }
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1rem;
}
.card { padding: 1rem; overflow-wrap: anywhere; }
img { max-width: 100%; height: auto; }Apply cards to a wrapper containing elements with class card. The minimum fits within the container even when it is narrower than 16rem, and the grid creates additional columns when space allows.
Good practices and why
- Use content-driven sizing and reserve media space when dimensions are known.
- Inspect computed styles instead of adding increasingly forceful overrides without understanding the cascade.
Common mistakes
- Hiding page overflow to conceal a layout problem.
- Using absolute positioning for every element in a document layout.
Try it yourself
Build three cards with very different text lengths. Resize the viewport and test at 200% zoom. Add an unbroken filename and check that it remains contained.
What a successful result looks like
Cards reflow without horizontal page scrolling and controls remain visible with longer content.
0 / 6000 characters · Saved on this device. Do not include passwords or private data.
Check your understanding
Answer the understanding check correctly to record exercise completion.
Related interview practice
These scenarios add constraints and follow-up questions to the concepts you just learned.
Further reading
Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.