rem vs px in CSS: Whatโ€™s the Difference and When to Use Each?

When working with CSS, you often see units like px and rem.
But many developers get confused:

๐Ÿค” Should I use px or rem?

In this guide, youโ€™ll learn:

  • What px and rem are
  • Key differences
  • Real-world use cases
  • When to use each (very important)

What is px in CSS?

px stands for pixels.

๐Ÿ‘‰ It is a fixed unit, meaning it does not change based on screen size or user settings.


Example

h1 {
font-size: 16px;
}

๐Ÿ‘‰ The font size will always be 16 pixels


What is rem in CSS?

rem stands for root em.

๐Ÿ‘‰ It is a relative unit, based on the root (html) font size.


Example

html {
font-size: 16px;
}h1 {
font-size: 1rem;
}

๐Ÿ‘‰ 1rem = 16px


How rem Works

If:

html {
font-size: 16px;
}

Then:

rempx
1rem16px
2rem32px
0.5rem8px

Key Differences Between rem and px

Featurepxrem
TypeFixedRelative
ResponsiveโŒ Noโœ… Yes
Based onScreen pixelsRoot font size
AccessibilityโŒ Poorโœ… Better
ScalingNoYes

Why rem is Preferred in Modern CSS


๐Ÿ”น 1. Better Responsiveness

If root font size changes:

html {
font-size: 18px;
}

๐Ÿ‘‰ All rem values scale automatically


๐Ÿ”น 2. Accessibility Support

Users can change browser font size โ†’ rem adjusts automatically

๐Ÿ‘‰ Helps users with vision issues ๐Ÿ‘


๐Ÿ”น 3. Consistent Design

You can control entire UI from one place (html)


When to Use px

Use px when you need precise control:

  • Borders
  • Shadows
  • Icons (fixed size)
  • Hairline elements

Example

border: 1px solid black;

When to Use rem

Use rem for:

  • Font sizes
  • Spacing (margin, padding)
  • Layout scaling

Example

p {
font-size: 1rem;
margin: 1.5rem;
}

Real-World Example


โŒ Using px (Not Flexible)

.container {
padding: 20px;
}

โœ… Using rem (Flexible)

.container {
padding: 1.25rem;
}

๐Ÿ‘‰ Automatically adjusts with root font size


Common Mistakes


โŒ Mixing units randomly

๐Ÿ‘‰ Keep consistency


โŒ Setting root font size incorrectly

html {
font-size: 10px;
}

๐Ÿ‘‰ Use carefully


rem vs em (Quick Note)

  • rem โ†’ based on root
  • em โ†’ based on parent

๐Ÿ‘‰ rem is easier to manage


Best Practice

๐Ÿ‘‰ Use both together:

  • rem โ†’ layout & typography
  • px โ†’ borders & fine details

Real-World Use Cases

  • Responsive websites
  • Design systems
  • UI frameworks (like MUI)
  • Accessibility-friendly apps

Interview Tip

If asked:

โ€œpx vs rem?โ€

๐Ÿ‘‰ Answer:

โ€œpx is fixed, while rem is relative to the root font size, making it more responsive and accessible.โ€


๐Ÿ Final Summary

  • px = fixed unit
  • rem = scalable unit
  • Use rem for responsiveness
  • Use px for precision

๐Ÿ‘‰ Modern CSS prefers rem


Related Articles

๐Ÿ‘‰ Add these:


๐Ÿ’ก Found this helpful? Subscribe to get simple CSS guides, real-world examples, and frontend tips. Happy Coding!

CSS Variables Explained: A Complete Guide with Examples (Beginner to Advanced)

CSS Variables (also called Custom Properties) are one of the most powerful features in modern CSS. They help you write clean, reusable, and maintainable styles.

In this guide, youโ€™ll learn:

  • What CSS Variables are
  • Why we need them
  • How they work
  • Real-world examples
  • Best practices

What Are CSS Variables?

CSS Variables are custom values that you define and reuse in your CSS.

๐Ÿ‘‰ They start with -- and are accessed using var().


Basic Example

:root {
--primary-color: blue;
}button {
background-color: var(--primary-color);
}

๐Ÿ‘‰ Output:

  • Button background becomes blue

Why Do We Need CSS Variables?

Before CSS Variables, we had problems like:

โŒ Repeating same values everywhere
โŒ Difficult to update styles
โŒ No dynamic theming


Example Without Variables

button {
background-color: blue;
}.header {
color: blue;
}

๐Ÿ‘‰ If you want to change blue โ†’ red, you must update everywhere โŒ


With CSS Variables

:root {
--primary-color: blue;
}button {
background-color: var(--primary-color);
}.header {
color: var(--primary-color);
}

๐Ÿ‘‰ Change once โ†’ updated everywhere โœ…


How CSS Variables Work


๐Ÿ”น Defining Variables

:root {
--main-color: green;
}

๐Ÿ‘‰ :root = global scope


๐Ÿ”น Using Variables

p {
color: var(--main-color);
}

Scope of CSS Variables


๐Ÿ”น Global Scope

:root {
--color: red;
}

๐Ÿ‘‰ Available everywhere


๐Ÿ”น Local Scope

.card {
--color: blue;
}.card p {
color: var(--color);
}

๐Ÿ‘‰ Only inside .card


Updating Variables Dynamically

You can update variables using JavaScript:

document.documentElement.style.setProperty('--primary-color', 'red');

๐Ÿ‘‰ Instantly updates UI ๐Ÿ”ฅ


Real-World Use Case: Theme Switching


Light Theme

:root {
--bg-color: white;
--text-color: black;
}

Dark Theme

.dark {
--bg-color: black;
--text-color: white;
}

Usage

body {
background-color: var(--bg-color);
color: var(--text-color);
}

๐Ÿ‘‰ Just toggle .dark class โ†’ theme changes instantly


Fallback Values (Very Important)

color: var(--primary-color, red);

๐Ÿ‘‰ If variable not defined โ†’ fallback to red


Advanced Example

:root {
--spacing: 10px;
}.container {
padding: calc(var(--spacing) * 2);
}

๐Ÿ‘‰ Works with calculations ๐Ÿ”ฅ


๐Ÿ†š CSS Variables vs SCSS Variables

FeatureCSS VariablesSCSS Variables
Runtime changeโœ… YesโŒ No
Works in browserโœ… YesโŒ Precompiled
Dynamic themesโœ… YesโŒ No

Best Practices

  • Use meaningful names (--primary-color)
  • Define global variables in :root
  • Avoid overusing variables
  • Use for themes and reusable values

Where CSS Variables Are Used

  • React (MUI, Tailwind configs)
  • Theming systems
  • Design systems
  • Dark/light mode

Interview Tip

If asked:

โ€œWhat are CSS variables?โ€

๐Ÿ‘‰ Answer:

โ€œCSS variables are reusable custom properties that allow dynamic styling and runtime updates in CSS.โ€


๐Ÿ Final Summary

  • CSS Variables improve maintainability
  • They support dynamic updates
  • Useful for themes and reusable styles
  • Work directly in browser

Related Articles


๐Ÿ’ก Found this helpful? Subscribe to get simple frontend tutorials, real-world examples, and developer tips. Happy Coding!

A Standard Guide to Responsive Web Design

Responsive design is no longer optional.
But blindly โ€œmaking things responsiveโ€ without understanding the standards often creates poor user experience.

This guide explains responsive design clearly, practically, and honestly.


What Is Responsive Design?

Responsive design means:

Designing websites that adapt smoothly to different screen sizes, devices, and orientations without breaking usability.

Itโ€™s not just about shrinking content โ€”
itโ€™s about preserving intent and usability.


Why Responsive Design Is Necessary

1๏ธโƒฃ Users Use Multiple Devices

  • Mobile phones
  • Tablets
  • Laptops
  • Large desktops

A fixed-width site fails on most of them.


2๏ธโƒฃ Google Uses Mobile-First Indexing

Google evaluates:

  • Mobile layout
  • Performance
  • Usability

โŒ Non-responsive sites rank poorly.


3๏ธโƒฃ Accessibility & Inclusion

  • Larger text
  • Touch-friendly buttons
  • Screen reader compatibility

Responsive design supports everyone, not just mobile users.


Core Responsive Design Standards to Follow

1๏ธโƒฃ Mobile-First Design

Start designing for small screens first, then scale up.

โŒ Desktop-first causes clutter on mobile
โœ… Mobile-first forces clarity

/* Mobile styles */
body { font-size: 16px; }
/* Tablet and above */
@media (min-width: 768px) {
body { font-size: 18px; }
}

2๏ธโƒฃ Use Fluid Layouts (Not Fixed Widths)

โŒ Avoid:

.container { width: 1200px; }

โœ… Prefer:

.container { max-width: 1200px; width: 100%; }

This allows content to adapt naturally.


3๏ธโƒฃ Flexible Units (rem, em, %, vw)

UnitUse Case
remFont sizes
%Containers
vw/vhViewport-based elements
pxBorders & icons

โŒ Pixel-only layouts break on different screens.


4๏ธโƒฃ Breakpoints Based on Content, Not Devices

โŒ Bad practice:

  • โ€œiPhone breakpointโ€
  • โ€œiPad breakpointโ€

โœ… Good practice:

  • Break when layout looks broken

Common starting points:

  • 480px
  • 768px
  • 1024px
  • 1280px

5๏ธโƒฃ Responsive Images

Images must adapt without distortion.

img {
max-width: 100%;
height: auto;
}

Use:

  • srcset
  • sizes
  • Modern formats (WebP)

6๏ธโƒฃ Touch-Friendly Design

Mobile users donโ€™t have a mouse.

Standards to follow:

  • Minimum touch target: 44ร—44px
  • Enough spacing between buttons
  • Avoid hover-only interactions

7๏ธโƒฃ Typography Responsiveness

Readable text matters more than layout.

Standards:

  • Base font size: 16px minimum
  • Line height: 1.4 โ€“ 1.6
  • Limit line width to ~75 characters

Key Concepts You Must Understand

๐Ÿ”น Media Queries

They apply styles conditionally.

@media (min-width: 1024px) {
.layout { display: flex; }
}

๐Ÿ”น Flexbox & Grid

Modern layout systems replace floats.

  • Flexbox โ†’ one-dimensional layouts
  • Grid โ†’ two-dimensional layouts

They adapt naturally to screen changes.


๐Ÿ”น Viewport Meta Tag

Without this, mobile browsers scale pages incorrectly.

<meta name="viewport" content="width=device-width, initial-scale=1.0">

When You Should NOT Use Responsive Design

Responsive design is not always the best solution.

โŒ Avoid or rethink if:

  • You are building a device-specific kiosk
  • You need pixel-perfect print layouts
  • Performance budget is extremely strict
  • Complex data tables need different UX

Sometimes:
โžก Separate mobile experience is better.


Common Mistakes to Avoid

  • Hiding content instead of redesigning
  • Too many breakpoints
  • Desktop-heavy animations on mobile
  • Ignoring performance
  • Assuming โ€œresponsive = accessibleโ€ (itโ€™s not)

Performance Is Part of Responsiveness

A responsive site that loads slowly fails.

Best practices:

  • Lazy-load images
  • Reduce CSS & JS
  • Avoid heavy libraries on mobile

Responsiveness includes speed.


Final Checklist (Use This)

โœ” Mobile-first
โœ” Fluid layouts
โœ” Flexible units
โœ” Content-based breakpoints
โœ” Responsive images
โœ” Touch-friendly UX
โœ” Performance optimized


Final Thought

Responsive design is not about screens.
Itโ€™s about respecting users.

Follow standards when they improve usability.
Avoid them when they complicate experience.

A good responsive design feels natural, not forced.

Heart Beat Animation with ReactJS and CSS

Heart.css

/* Align the heart to the center of the page */
.heart-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

/* creating two circles */
.heart::before,
.heart::after {
    content: "";
    width: 100px;
    height: 100px;
    background-color: #e63946;
    position: absolute;
    border-radius: 50%;
}

.heart::before {
    top: -50px;
}

.heart::after {
    left: 50px;
}

/* Creating square */
.heart {
    position: relative;
    width: 100px;
    height: 100px;
    background-color: #e63946;
    transform: rotate(-45deg);
    animation: beat 1s infinite;
}

/* Heart Beat Animation */
@keyframes beat {
    0%, 100% {
        transform: scale(1) rotate(-45deg);
    }
    50% {
        transform: scale(1.2) rotate(-45deg);
    }
}
  • By using div with className(.heart) we have created heart shape
  • By using the div with the className(.heart-container) we have aligned the heart to the center of the page.
  • Heart Shape: We have created the heart by positioning two circular divs (::before and ::after) next to a square div, then rotating the main .heart div by -45deg.
  • Animation: The @keyframes beat animation makes the heart scale up and down, creating a “beating” effect.
  • 0%, 100%: The heart starts and ends at its original scale (1), so it returns to the original size after each beat.
  • 50%: The heart scales up to 1.2 times its original size, creating the “pumping” effect in the middle of each beat cycle.