The box model#
Every element is a rectangular box made of four layers:
+--------------- margin ---------------+
| +------------ border -------------+ |
| | +--------- padding ---------+ | |
| | | content | | |
| | +---------------------------+ | |
| +---------------------------------+ |
+---------------------------------------+
Almost every modern site starts its CSS with this line, so width means the full visible width:
*, *::before, *::after { box-sizing: border-box; }
Shorthands go clockwise from the top: margin: 10px 20px 30px 40px; = top, right, bottom, left. margin: 10px 20px; = top/bottom 10, left/right 20. margin: 0 auto; centres a block that has a width.
हर element एक डिब्बे (box) की तरह है। अंदर content है, उसके चारों ओर padding (अंदर की गद्दी), फिर border (डिब्बे की दीवार), और सबसे बाहर margin (दूसरे डिब्बों से दूरी)। Padding अंदर की जगह है, margin बाहर की।
display: block, inline and inline-block#
| Value | Behaviour | Examples |
|---|---|---|
block |
starts on a new line, takes the full width | div, p, h1, section |
inline |
flows inside text; width/height ignored | span, a, strong |
inline-block |
flows inline but accepts width, height, padding | buttons, badges |
none |
hidden completely | menus that open on click |
flex / grid |
turns the element into a layout container | see below |
position#
| Value | Meaning |
|---|---|
static |
normal flow (default) |
relative |
normal flow, but can be nudged with top/left; becomes the anchor for absolute children |
absolute |
taken out of the flow, placed relative to the nearest positioned ancestor |
fixed |
stays at the same place on screen while scrolling (chat buttons, cookie bars) |
sticky |
scrolls normally, then sticks at a top value (sticky headers) |
Flexbox - layouts in one direction#
Make the parent display: flex and its children line up in a row (or a column).
| On the container | Does |
|---|---|
flex-direction: row / column |
the main axis |
justify-content |
spacing on the main axis: flex-start, center, space-between, space-around |
align-items |
alignment on the cross axis: stretch, center, flex-start |
flex-wrap: wrap |
let items move to a new line |
gap: 12px |
space between items |
On a child, flex: 1 1 150px means grow: 1, shrink: 1, start at 150px. Change the preview width and watch the cards wrap.
CSS Grid - layouts in two directions#
Grid is perfect for whole page layouts and card galleries.
repeat(auto-fill, minmax(100px, 1fr)) makes as many columns as fit, each at least 100px - a responsive gallery with no media queries.
Flexbox or Grid? Use Flexbox for a single row or column of items (navbars, buttons, a row of cards). Use Grid when you need rows and columns to line up (page layouts, galleries, dashboards). Real sites use both together.
Responsive design with media queries#
Most Indian users open websites on phones, so design mobile-first: write the phone layout first, then add rules for bigger screens.
Responsive checklist:
<meta name="viewport" content="width=device-width, initial-scale=1">in the head.img { max-width: 100%; height: auto; }so images never overflow.- Flexible widths (
%,fr,minmax) instead of fixed pixel widths. - Media queries for the few places the layout must change.
- Test in the browser's device mode (
F12→ phone icon).
Summary#
- Box = content + padding + border + margin; use
box-sizing: border-box. displaydecides how a box flows;positiontakes it out of the flow when needed.- Flexbox: one-direction layouts with
justify-content,align-items,gap. - Grid: two-direction layouts with
grid-template-columns/rows/areas. - Mobile-first + media queries = responsive design.