Understanding REM Units
The REM (root em) unit is a relative measurement that differs from its cousin EM in a crucial way: while EM references the parent element's font size, REM always references the root element (the <html> tag).
html {
font-size: 16px;
}
.card {
width: 10rem; /* equals 160px */
height: 10rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.text {
/* EM depends on parent */
font-size: 2em;
/* REM depends on root */
padding: 2rem;
}
The key advantage of REM is scalability: modifying the root font size automatically adjusts all REM-based dimensions throughout the entire page. This makes it ideal for responsive design where you need proportional scaling across different screen sizes.
Media Queries in CSS3
Overview
Media queries enable you to apply styles conditionally based on device characteristics. This CSS3 feature is essential for building layouts that adapt to smartphones, tablets, and desktop monitors.
Syntax Structure
@media mediatype and (media feature) {
/* CSS rules */
}
Media Types
| Type | Purpose |
|---|---|
all |
Targets every device |
print |
Targets print layouts and previews |
screen |
Targets computer monitors, tablets, and phones |
Logical Operators
and— Combines multiple conditions (both must be true)not— Negates a conditiononly— Restricts to specific media types
Common Media Features
| Feature | Description |
|---|---|
width |
Exact viewport width |
min-width |
Minimum viewport width |
max-width |
Maximum viewport width |
Practical Example
/* Medium screens */
@media screen and (max-width: 768px) {
.container {
padding: 16px;
}
.grid {
grid-template-columns: 1fr;
}
}
/* Small screens */
@media screen and (max-width: 480px) {
.container {
padding: 12px;
}
.sidebar {
display: none;
}
}
Responsive Strategy with REM
Combining media queries with REM units creates a powerful adaptive system. When you change the root font size via media queries, all REM-based elements scale proportionally. This means buttons, containers, typography, and spacing all resize together maintaining visual harmony.
Resource-Based Media Queries
You can conditionally load stylesheets based on device characteristics:
<link rel="stylesheet" href="mobile.css" media="screen and (max-width: 767px)">
<link rel="stylesheet" href="desktop.css" media="screen and (min-width: 768px)">
This approach keeps stylesheets organized by device category rather than mixing all conditions in one file.
CSS Preprocessing with Less
The Problem with Raw CSS
CSS lacks fundamental programming features—no variables, no functions, no scope isolation. This leads to repetitive declarations, difficult maintainance, and no way to perform calculations dynamically.
What Less Offers
Less extends CSS with programming capabilities while keeping CSS-compatible syntax. It introduces variables, mixins, functions, and operations that dramatically improve code organization and maintainability.
Reference: https://lesscss.org/
Variable Declaration
@primary-color: #3498db;
@font-base: 16px;
@spacing-unit: 8px;
.button {
color: @primary-color;
font-size: @font-base;
padding: @spacing-unit * 2;
}
Naming rules:
- Prefix with
@ - Avoid special characters
- Cannot start with numbers
- Case-sensitive
Compilation Process
Less code requires compilation to standard CSS before browsers can interpret it. VS Code users can install the "Easy LESS" extension, which automatically generates CSS files up on save.
Selector Nesting
Less allows hierarchical rule definitions that mirror HTML structure:
.nav {
background: #2c3e50;
&-item {
display: inline-block;
padding: 12px;
&:hover {
background: lighten(#2c3e50, 10%);
}
&.active {
border-bottom: 3px solid #3498db;
}
}
}
The & symbol references the parent selector, enabling pseudo-classes and modifiers without repetition.
Arithmetic Operations
Less supports mathematical operations on any value:
@container-width: 1200px;
@columns: 12;
.column {
width: @container-width / @columns;
padding: 10px + 5px;
margin: (24px / 2);
}
Operations respect unit compatibility—when mixing units, the first operand's unit takes precedence.
Implementing REM-Based Responsive Layouts
Core Concept
The goal is proportional scaling: when viewport dimensions change, every element adjusts maintaining design ratios. By setting the root font size proportionally to screen width and using REM for all dimensions, elements scale uniformly.
Implementation Approaches
Approach 1: Media Queries with Less
This method manually defines breakpoints and scales.
Standard viewport widths:
| Device Category | Typical Widths |
|---|---|
| Older phones | 320px, 360px |
| Modern phones | 375px, 414px |
| Tablets | 768px, 1024px |
| Desktops | 1920px+ |
Division strategy:
- Divide the design width into equal porsions (commonly 15 or 10)
- Use that portion size as your root font size
- Convert all pixel values using the formula:
REM Value = Original Pixel / Root Font Size
Example calculation for 750px design:
@design-width: 750px;
@parts: 15;
@font-size: @design-width / @parts; // 50px
html {
font-size: @font-size;
}
.banner {
width: 750rem / @parts; // converts to 750px at design size
height: 400rem / @parts;
}
At 320px viewport, the root font becomes ~21.33px (320 ÷ 15), so the banner becomes ~106.67px × 56.89px—scaled proportionally.
Approach 2: Flexible.js Library
This library (developed by the Taobao mobile team) automates the calculation process. It divides the viewport into 10 equal segments regardless of actual pixel density.
To use it:
- Include the library in your project
- Set your root font size based on design dimensions divided by 10
For a 750px design: root font size = 75px
All element dimensions become: element pixels ÷ 75
The library handles all breakpoint calculations internally, eliminating manual media query management.
Module Importing in Less
Organize stylesheets into reusable modules:
// base.less
@base-font: 16px;
@spacing: 8px;
// components.less
@import "base";
.button {
font-size: @base-font;
padding: @spacing * 2;
}
The import statement pulls external Less files into the current file, combining all variables and rules into one compiled output.