We have all been there. You spend hours writing the CSS for a beautiful, perfectly aligned UI on your external monitor. You feel like a frontend master. Then, you open the dev tools, toggle the device toolbar to a mobile viewport, and watch your entire layout implode.
Containers overlap. Text overflows. The horizontal scrollbar of death appears.
The culprit is almost always the same: **hardcoding widths and heights in pixels.
Welcome to Day 19 of my #100DaysOfCode journey into Full-Stack Development. Today, I tackled the core of Responsive Web Design by diving deep into the alphabet soup of CSS units. If you want to build scalable interfaces, you have to transition from rigid pixels to fluid math.
Here is the breakdown of how these units actually work, and when you should be using them.
The Rigid Foundation: px (Pixels)
The pixel is an absolute unit. It is fixed, device-dependent, and completely stubborn.
When you define something in pixels, you are giving the browser an unyielding command. If you set a container to 800px, it will stay 800px even if the user's screen is only 400px wide.
When to use it: Reserve px for elements that should absolutely never scale. Think 1px borders, subtle box-shadows, or maybe a fixed-size avatar image. Do not use it for layout wrappers or typography.
.card {
/* Good use of px */
border: 1px solid #333;
border-radius: 8px;
/* Bad use of px - will break on small screens */
width: 600px;
}
The Relatives: Designing for Fluidity
To build modern web apps, elements need to adapt to their environment.
1. Percentages (%)
Percentages size an element relative to its parent container. If the parent shrinks, the child mathematically shrinks with it.
.parent-container {
width: 100%; /* Takes full width of the screen */
}
.child-element {
width: 50%; /* Will always be half the size of the parent */
}
2. Viewport Width (vw) & Viewport Height (vh)
The "viewport" is simply the visible area of the web page on the screen.
1vw = 1% of the viewport's total width.
1vh = 1% of the viewport's total height.
If you are building a landing page and want the hero section to take up the exact full screen before the user starts scrolling, viewport units are the answer.
.hero-section {
width: 100vw;
height: 100vh;
background-color: #0f0f0f;
}
3. Viewport Maximum (vmax) & Viewport Minimum (vmin)
These are incredibly powerful but rarely talked about. The browser looks at the viewport's width and height, and then:
vmin: Takes 1% of whichever dimension is smaller.
vmax: Takes 1% of whichever dimension is larger.
These are fantastic for keeping elements (like a central square UI component) perfectly proportioned, regardless of whether a user is holding their device in portrait or landscape mode.
The Typography Kings: em and rem
When it comes to font sizes, margins, and padding, em and rem are the absolute industry standards for building accessible and scalable interfaces.
The em Unit (The Multiplier)
An em unit sizes an element relative to the font size of its direct parent element.
If a parent container has a font size of 20px, and you set the child's font size to 2em, the browser calculates 2 * 20 = 40px.
.article-body {
font-size: 20px;
}
.article-quote {
font-size: 2em; /* Evaluates to 40px */
margin-top: 1em; /* Evaluates to 40px, because it is relative to its OWN font size! */
}
Note: Because em values multiply based on their parents, deeply nested components can lead to chaotic, unpredictable sizing.
The rem Unit (Root em)
This is the holy grail. rem stands for "root em." Instead of looking at the parent, it looks all the way up the DOM tree to the root font size (which is 16px by default in almost all browsers).
If you set an h1 to 3rem, it evaluates to 48px (3 * 16), no matter where it is placed in your DOM tree. It provides the predictability of pixels with the accessibility of relative units, seamlessly scaling if a user increases their default browser font size for readability.
:root {
font-size: 100%; /* Defaults to 16px */
}
.responsive-title {
font-size: 2.5rem; /* 40px */
}
.responsive-paragraph {
font-size: 1rem; /* 16px */
padding: 1.5rem; /* 24px */
}
Wrapping Up Day 19
Transitioning away from pixels takes a little bit of mental refactoring at first, but it is the defining factor between a fragile layout and a robust user interface. Embrace rem for your text and spacing, and lean on % and viewport units for your macro layouts.
Question for the Dev community:
Do you strictly use rem for your projects, or do you still find valid use cases for em in scoped components? Let's debate in the comments!
Let's Connect!
Follow along with my #100DaysOfCode journey across my platforms:
Dev.to: bblackwind
GitHub: bblackwind
LinkedIn: Vishal on LinkedIn
X (Twitter): @VishalS25630987
Hashnode: @vishal2303
Medium: @vishal230396
YouTube: Blackwind Coding School
Instagram: Blackwind Coding School
Top comments (0)