*, *::before, *::after {
box-sizing: border-box;
}
/* With border-box: a 300px wide element stays 300px
even with padding and border added */
.card {
width: 300px;
padding: 20px;
border: 1px solid #e5e7eb;
/* Total width: still 300px */
}
Note The universal border-box reset is considered best practice. Without it, padding and borders are added on top of the width, making sizing unpredictable.
box sizingborder boxinclude padding in widthsizing model
Margin
syntax
margin: top right bottom left;
margin: vertical horizontal;
margin: all;
Note Vertical margins collapse: when two vertical margins touch, only the larger one applies (not their sum). Margins on flex and grid children do not collapse. Use margin: 0 auto on a block element with a set width to center it.
Note Unlike margins, padding never collapses. Padding is inside the element's border, so background colors and images extend into the padded area. Use padding-block and padding-inline for writing-mode-aware spacing.
Note Border styles include solid, dashed, dotted, double, groove, ridge, inset, outset, and none. Use border-radius: 50% on a square element to create a circle. Individual sides can be styled separately.
Note Avoid setting fixed heights on text containers since content length varies. Use min-height instead. The aspect-ratio property maintains proportions without needing both width and height.
widthheightmax widthmin heightelement sizeaspect ratio
Note overflow: auto only shows scrollbars when content actually overflows, while scroll always shows them. overflow: clip is like hidden but does not create a scroll container, preventing programmatic scrolling. Combine overflow: hidden with text-overflow: ellipsis for truncated text.
Note Unlike border, outline does not take up space in the layout and does not affect element sizing. It can overlap adjacent elements. outline-offset pushes the outline away from the element edge. Never remove focus outlines without providing an alternative indicator.
Note block takes full width, starts on a new line, accepts all box model properties. inline only takes content width, ignores vertical margin/padding. inline-block is inline but respects all box model properties. display: none removes the element entirely. For screen-reader-only content, use the visually-hidden pattern instead.