Ever found yourself stuck choosing css units between px, rem, em, %, vh, or vw while styling your webpage?
Whether you're just starting with CSS or have been writing stylesheets for a while, understanding CSS units is essential for creating clean, consistent, and responsive designs.
With so many options available, it’s easy to get confused — but each unit has a purpose and knowing when to use which one can make your work faster and your layouts smarter.
In this blog, we’ll break down the most commonly used CSS units with beginner-friendly explanations, real-world use cases, and some deeper insights for those looking to level up their styling game.
When you're just starting with CSS, it's common to size everything using px (pixels). But as you dive deeper into responsive design and accessibility, you'll discover that there are better, more flexible units like rem, em, vh, %, and more.
In this guide, we’ll explore these CSS units beyond px, understand their use cases, and learn how to choose the right one depending on your layout needs. Whether you're a beginner or brushing up your CSS skills, this post will help you write cleaner, more scalable CSS.
The Css units can be basically classified into two different types:
Absolute units
- As the name suggests, the absolute css units are fixed and does not depend on the size of the parent element or the viewport.
Absolute units include px, pt, pc, in, cm, mm, etc.
Relative units
- Relative css units as the name implies are flexible and are relative to the parent element's size , the viewport's size or the root element's font size.
Relative units include rem, em, %, vh, vw, ex, etc.
Now let's talk about the most widely used css units one by one.
1. Pixels (px)
- This is the most widely used unit of measurement.
- px stands for pixel, the smallest unit of a digital display.
- It's an absolute unit that represents a fixed size on screen, typically mapped to 1 physical device pixel (or scaled on high-DPI screens).
- Best for precise elements like borders, icons, and 1px lines.
- Does not scale with user settings (like browser zoom or font-size changes), making it less flexible for responsive or accessible designs.
Let us understand this with the help of an example:
Case-1: When the parent width is 80%
Case-2: When the parent width is 10%
So we see from the above examples that even if we are changing the dimensions of the parent element the child element dimensions are fixed and are not changing as they are in px.
2. Root em(rem) :
- rem stands for root em, a relative unit in CSS.
- It is relative to the root element's () font size.
- If html { font-size: 16px; }, then 1rem = 16px.
- Ideal for font sizes, padding, margin, and responsive design — ensures consistency across the entire layout.
- Scales globally with just one change to the root font size — great for accessibility and theming.
- Not ideal for components that need to scale based on their own font size use em instead in such cases.
3. Element em(em):
- em is a typographic unit that represents the current element's font size.
- It’s relative to the font size of the parent (or current element if overridden).
- E.g., if font-size: 20px, then 1em = 20px.
- Perfect for component-level spacing, such as padding/margin that should scale with text.
- Flexible for self-contained components like buttons or cards.
- Can compound in nested elements, making sizes unpredictable if not managed carefully.
4. Percentage(%):
- % is a relative unit based on the size of the parent element.
- Used for width, height, padding, margins, etc.
- E.g., width: 50% means 50% of the parent’s width.
- Great for fluid layouts and responsive containers.
- Keeps layouts flexible across different screen sizes.
- Behavior can be inconsistent, especially with vertical spacing or when parent size is not explicitly set.
5. Viewport Units: vh
and vw
When you want elements to scale based on the actual size of the user’s screen, vh and vw are the go-to CSS units.
- vh stands for viewport height
- vw stands for viewport width
Both are relative to the dimensions of the browser window:
- 1vh = 1% of the viewport's height
- 1vw = 1% of the viewport's width
So:
100vh fills the full height of the screen
100vw stretches across the full width of the screen
Best use-cases
- Fullscreen hero sections
- Modals and overlays
- Carousels and responsive banners
- Fluid typography (especially with clamp())
- Designing layouts that should always fill the screen, regardless of content
They enable truly responsive layouts where the size of elements is tied directly to the screen itself, not the parent element or font size.
Note: While vh and vw work seamlessly on desktops, they can behave unexpectedly on mobile devices.
Browsers may shrink the viewport height when toolbars (like the address bar) appear or disappear as users scroll
This causes elements sized with vh to jump or resize unexpectedly
To handle this, modern CSS introduces svh, lvh, and dvh, but support is still evolving, so always test on real devices.
6. Viewport Minimum(vmin):
- vmin is 1% of the smaller side of the screen (width or height).
- So lets say if the viewport is 1200x800, then 1vmin = 8px.
- vmin is ideal when you want UI elements to scale based on the smallest constraint — useful for responsive typography, square boxes, or maintaining aspect ratios on both portrait and landscape orientations.
- It is commonly used in viewport typography systems, like:
h1 {
font-size: calc(1rem + 2vmin);
}
- Can behave unpredictably on devices with dynamic toolbars or in split-screen views.
7. Viewport Maximum(vmax):
- vmax is a viewport-relative unit equal to 1% of the larger dimension of the screen.
- If viewport is 1200x800, then 1vmax = 12px.
- Full-screen layouts or scaling elements that should follow the wider side of the screen
- Maintains visual impact on large screens.
- May overflow or break on narrow devices (like mobiles in portrait).
- Use vmin and vmax when your layout needs to be adaptive, especially between mobile, tablet, and desktop in both orientations.
8. Fractional Unit(fr):
- fr stands for fraction, used only inside CSS Grid layouts.
- 1fr means “1 part of the available space”.
- Flexible and responsive grid column/row layouts. -Automatically distributes space without needing fixed widths. Cannot be used outside of display: grid.
Lets see three cases of grid usage in different forms:
- Grid using fr units
grid-template-columns: 1fr 2fr 1fr
-Proportional layout that grows/shrinks with the container.
-Best for dynamic responsive layouts.
-Can misbehave if used with fixed-size grid containers.
- Grid using px units
grid-template-columns: 100px 200px 100px
-Perfect when you need precise sizes (e.g., buttons/icons).
-Not responsive. Will overflow on small screens or shrink too much.
-Poor accessibility on zoomed or mobile views.
- Grid using % units
grid-template-columns: 25% 50% 25%
-Useful for layouts relative to the container’s width.
-Parent must have a defined width for % to work predictably.
-Less flexible than fr when mixing fixed and flexible items.
9. Character Width(ch):
- ch is relative to the width of the character "0" in the current font.
- Typically used to size elements based on expected character count.
- Perfect for input fields, code blocks, or text containers with fixed character widths.
- Content-aware sizing for monospaced or text-heavy UIs.
- In proportional fonts, width of "0" may not represent average character width accurately.
Example usage:
input {
width: 30ch;
}
This means: the input will be wide enough to fit 30 characters of text (based on font).
10. x-height(ex):
- ex is relative to the height of the lowercase letter “x” in the current font.
- Useful for vertical alignment in typography-heavy layouts.
- Specialized typography adjustments and vertical alignment. Font-sensitive vertical sizing.
- Inconsistent across fonts; rarely used due to limited browser support and predictability.
Conclusion:
Yes — there’s a lot of units in CSS, and more are still arriving. Don’t sweat it. The CSS units you just explored (px, rem, em, %, the key viewport units, and fr) will already cover 99 % of what real projects demand: precise control where you need it, fluid scaling where you don’t, and rock‑solid support in every modern browser.
Think of the newer upcoming units as future helpers rather than extra homework. When they mature, they’ll solve niche problems (mobile toolbars, ultra‑fine typography) and make your codebase simpler, not harder. Master today’s proven units first; they’re more than enough to craft beautiful, responsive interfaces right now. Tomorrow’s additions will only make that job even easier.
So dive in with confidence — the tools you have are powerful, and the best is still yet to come. Happy coding!
Top comments (0)