DEV Community

Sohana Akbar
Sohana Akbar

Posted on

Responsive CSS Tips That Took Me 6 Months to Learn

After half a year of trial, error, and countless browser tabs, here are the responsive CSS lessons that actually stuck.

  1. Stop using px for everything px is rigid. rem and em scale with user preferences.

css
/* Hard to maintain */
body { font-size: 16px; }

/* Accessible & responsive */
body { font-size: 1rem; }
h1 { font-size: 2.5rem; }
My rule: px for borders, rem for everything text-related.

  1. clamp() is magic No more 3 different media queries just to resize a heading.

css
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
}
Fluid typography that scales perfectly from mobile to desktop. One line.

  1. The :has() selector changes everything Parent selection is finally here.

css
/* Add padding if card contains an image */
.card:has(img) {
padding: 1rem;
}

/* Highlight a row with an error */
.row:has(.error) {
background-color: #fff0f0;
}
Works in all modern browsers. Use it carefully—performance is good but not free.

  1. Container queries > Media queries (for components) Media queries look at the viewport. Container queries look at the component's parent.

css
.card-container {
container-type: inline-size;
}

@container (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}
Your card component now responds to its own space, not the whole screen.

  1. min() and max() for spacing css .padding { padding: min(5vw, 2rem); }

.sidebar {
width: max(250px, 20%);
}
No media query needed for basic responsive spacing.

  1. overflow: hidden is not your friend on body It breaks scroll-driven animations and can cause layout shifts on mobile keyboards.

Instead, use:

css
body {
overflow-x: clip; /* or visible */
}

  1. Test with real devices, not just DevTools Chrome's mobile view is optimistic. Real phones have:

Slower touch response

Notches and dynamic islands

Zoom settings

Browser UI that appears/disappears

Quick test: Grab an old iPhone, Lower brightness to 50%, Open your site in safari. You'll learn more in 5 minutes than an hour in DevTools.

  1. width: 100% vs width: 100vw 100vw includes scrollbar width. It causes horizontal overflow.

css
/* This can cause sideways scroll */
.full-width {
width: 100vw;
}

/* Safer */
.full-width {
width: 100%;
}
The 6-month shortcut
If I could go back, I'd learn in this order:

rem + clamp() for text

Container queries for components

:has() for conditional styling

min()/max() for spacing

Real device testing early

The rest you'll pick up along the way.

What took you way too long to learn in CSS? Drop it in the comments.

Top comments (0)