A card looks perfect on a laptop, then spills beyond a phone screen. The problem often lies in the reference behind its dimensions. CSS measurement units can follow a container, a font, or the browser window; choosing the right reference makes the layout respond for a reason.
Start with the space the element must fit, then consider whether its text or the viewport should influence it. Viewport units in CSS are particularly helpful for screen-sized sections, while percentages often fit components inside a layout.
Pixels and percentages: fixed values and flexible containers
A pixel is a predictable increment for a border, icon, or small offset. For example, border: 1px solid gives a fine line without tying its thickness to the text size. A CSS pixel is a reference unit, however, and does not necessarily correspond to one physical screen pixel. The CSS Values specification explains how it relates to the reference pixel and display conditions.
Use px when a stable visual detail matters more than proportional scaling. It is less forgiving for the width of an entire card: width: 600px can overflow a narrower container. Browser zoom can still enlarge pixel-based content, so fixed in the stylesheet does not mean immune to zoom.
Percentage sizing units express a fraction of a reference value chosen by the property. For width measurements, a unit like % takes the containing block width as its usual reference. With width: 80%, the result follows that reference. If the available width is 500 pixels, the resulting content width is 400 pixels before padding and borders under the default box model. When the container narrows, the percentage width follows it.
Among CSS width units, percentages are a good fit for columns and images that belong to a parent. Add max-width: 100% to an image to keep its rendered width within its container while preserving its intrinsic proportions when height is automatic. With box-sizing: border-box, declared width includes padding and borders, making component dimensions easier to reason about. The box-sizing definition describes that calculation.
Percentages do not always refer to width. For example, a percentage font size uses the parent’s font size, while a percentage height can depend on whether the containing block has a definite height. Read the property definition when the reference is unclear; the percentage rules in CSS Values explain why it varies.
Font-relative sizing with em and rem
The font-relative sizing unit em usually follows the element’s computed font size. In padding: 1em, a 20-pixel font produces 20 pixels of padding. This makes it attractive for buttons: increasing the button text also increases the space around it. For the font-size property itself, em refers to the parent’s font size instead.
That distinction matters in nested components. If a parent has font-size: 1.2em and its child also has font-size: 1.2em, the second increase is applied to the already enlarged parent size. Repeated nesting can make text grow faster than intended. Inspect the computed font sizes when a menu or list becomes unexpectedly large.
The root-relative sizing unit rem follows the root element’s font size. If that size is 16 pixels, 1.5rem is 24 pixels; if it becomes 20 pixels, the same value becomes 30 pixels. It is a convenient choice for consistent spacing and type across separate components. Avoid forcing the root to a pixel size merely to make arithmetic easier: leaving the default in place lets the browser’s configured default participate in the scale.
CSS size units therefore encode different relationships: em makes a component respond to its own text, while rem gives it a shared reference. Neither replaces a content-aware layout. A long label can still wrap, so give controls room to grow and test them with enlarged text.
CSS viewport units: Size against the window
CSS viewport units use the visible area of the document as their reference. 1vw represents 1% of its width, and 1vh represents 1% of its height. At a viewport width of 1,000 pixels, 50vw computes to 500 pixels. A full-width decorative band may use viewport-based units, but a nested card usually needs a percentage of its container instead.
Consider a landing section with min-height: 100vh. It can occupy at least a viewport height while growing if its contents need more room. On mobile browsers, retractable interface bars complicate viewport-relative units: vh corresponds to the large viewport, so part of a section can extend behind visible browser controls. 100dvh tracks the dynamic viewport as those controls expand or retract; 100svh uses the smaller viewport for a stable size with controls present. The viewport length definitions describe these variants.
CSS vmax is a unit that follows the larger viewport dimension; vmin follows the smaller. On an 800-by-600-pixel viewport, 1vmax is 8 pixels and 1vmin is 6 pixels. If the window becomes 600 by 800, those values remain 8 and 6 respectively. This makes vmax in CSS useful for a background shape that should scale with the longer side, but it can make text disproportionately large on an unusually wide or tall screen.
For example, width: 30vmax makes a decorative circle 240 pixels wide in either window above. Set an explicit height or an aspect ratio if it must stay circular. If the circle is inside a narrow panel, cap it with max-width: 100% so it does not escape its available space. For units based on the viewport, check both portrait and landscape layouts: the longer side can change when a device rotates.
Combine references deliberately
The best CSS units often appear together. For a column, units used to set width can be combined with a limit. A readable content column can use width: 100% to fill its container and max-width: 60rem to stop growing on a wide display. Add horizontal padding and box-sizing: border-box so that padding remains inside the declared width. This keeps a narrow screen fluid without stretching lines indefinitely.
For fluid headings, font-size: clamp(1.5rem, 4vw, 3rem) sets a preferred size tied to the viewport, bounded by minimum and maximum values tied to the root font. Test the result at narrow and wide window sizes and with browser zoom; a viewport-only text size can grow too slowly as a reader enlarges content.
Treat CSS units as a question about what should change together. Choose pixels for a small stable detail, percentages for a share of available container space, em for a component tied to its text, rem for a shared type scale, and viewport lengths for a relationship to the screen. Use vmax when the longer screen dimension is the relationship you actually need.
