Why CSS Unit Conversions Matter
Anything well-built mostly runs on consistency. In music, it's beats. In programming, it's logic. And in web design, it's CSS units. They bring consistency and control to size, spacing, and flow. Without them, a layout isn't flexible, it's just code held together with school glue.
When you write a stylesheet, every pixel you enter has a direct impact on the scale, accessibility, and responsiveness of the web page. The wrong unit can make a layout overflow on a small screen, ignore the user's font size setting, or break in print. The right one lets the same design work on any screen and on paper.
So, how do you ensure you always choose the right CSS Unit?
Start by asking yourself: what should this scale with? For example, is it text that respects user preference, or the elements tied to a parent container, or layout tied to viewport size, or are you dealing with fixed, precise elements like borders, icons, etc. Theoretically, there is no single 'correct' unit; it all depends on what you want it to respond to.
Most mistakes come from using a unit that responds to the wrong reference. Furthermore, because CSS offers
so many distinct length units, converting between them manually could lead to errors and a tedious
workflow. That is exactly why a reliable CSS Unit Converter like the
Syntax Toolbox CSS Unit Converter that supports all
11 of the most commonly used CSS units - px, rem, em, %,
vw, vh, pt, pc, in, cm, and
mm - with configurable base font sizes, PPI/DPI settings, and viewport dimensions is worth
keeping open while you work.
Understanding CSS Units at a Glance
Relative CSS Units
| Unit | Sign used | Description |
|---|---|---|
| Root Em | rem | Relative to the root element's (html) font size. |
| Em | em | Relative to the parent's font size on font-size, and to the element's own font size on other properties. |
| Percentage | % | Relative to the parent element's size on most properties. |
| Viewport Width | vw | 1% of the browser viewport's width. |
| Viewport Height | vh | 1% of the browser viewport's height. |
Absolute CSS Units
| Unit | Sign used | Description |
|---|---|---|
| Pixels | px | Logical CSS pixel. 1px = 1/96 of an inch. |
| Points | pt | 1pt = 1/72 of an inch. |
| Picas | pc | 1pc = 12 points (1/6 of an inch). |
| Inches | in | 1in = 96px = 72pt |
| Centimeters | cm | Metric unit. 1cm ≈ 37.8px; 100cm = 1 meter. |
| Millimeters | mm | Metric unit. 1mm ≈ 3.78px; 1000mm = 1 meter. |
How to Use the Syntax Toolbox CSS Unit Converter
Syntax Toolbox CSS Unit Converter supports all 11 units listed above. Want to use our Converter? Here's how:
- Step 1: Select a CSS Unit to convert from (Relative or Absolute Unit).
- Step 2: Select another Unit (Relative or Absolute).
- Step 3: Adjust the reference value, such as Base Font/REM size, if the conversion needs one.
- Step 4: Enter a value to convert and check results in real-time.
Results appear as you type, with no extra buttons to press.
To simplify the process, the Syntax Toolbox CSS Unit Converter comes with default reference values, such as Base Font/REM Size. Here's the full list:
| Sr. No. | Conversions between | Default Size |
|---|---|---|
| 1 | Pixels to rem | Base Font/REM Size defaults to 16 |
| 2 | Pixels to em | Parent Font/EM Size defaults to 16 |
| 3 | Pixels to Percentage | Parent Element Size defaults to 1000 |
| 4 | Pixels to Viewport Width | Viewport Width defaults to the user's browser window width |
| 5 | Pixels to Viewport Height | Viewport Height defaults to the user's browser window height |
| 6 | Pixels to (pt, pc, in, cm, mm) | PPI/DPI (Resolution) field defaults to 96 |
Common CSS Unit Conversions and Formulas
Below are the exact formulas the Syntax Toolbox CSS Unit Converter uses. Unless stated otherwise, the
examples assume the browser default of 16px for the root font size, 16px for the
parent font size, and a 1920×1080px viewport. In the tool, the viewport fields start with your own browser
window size, and you can change any of these values.
px to rem converter
rem = px ÷ base font size
Example: 24px ÷ 16 = 1.5rem
rem to px converter
px = rem × base font size
Example: 1.5rem × 16 = 24px
px to em converter
em = px ÷ parent font size
Example: 20px ÷ 16 = 1.25em
em to px converter
px = em × parent font size
Example: 1.25em × 16 = 20px
px to % converter
% = (px ÷ parent element size) × 100
Example: 480px ÷ 1200px × 100 = 40%
px to vw converter
vw = (px ÷ viewport width) × 100
Example: 960px ÷ 1920px × 100 = 50vw
px to vh converter
vh = (px ÷ viewport height) × 100
Example: 540px ÷ 1080px × 100 = 50vh
px to pt converter
pt = px × (72 ÷ 96) = px × 0.75
Example: 16px × 0.75 = 12pt
pt to px converter
px = pt × (96 ÷ 72) ≈ pt × 1.333
Example: 12pt × (96 ÷ 72) = 16px
px to in converter
in = px ÷ 96
Example: 192px ÷ 96 = 2in
px to cm converter
cm = px ÷ 37.795
Example: 96px ÷ 37.795 ≈ 2.54cm
px to mm converter
mm = px ÷ 3.7795
Example: 38px ÷ 3.7795 ≈ 10.05mm
Reverse and Cross-Unit Formulas
These formulas cover the reverse conversions and conversions between relative units. When you
convert between em and rem, pixels are always the middle step, which is why both font
sizes show up in the formula.
| Conversion | Formula | Example |
|---|---|---|
| % to px | px = % × parent size ÷ 100 | 40% of 1200px = 480px |
| vw to px | px = vw × viewport width ÷ 100 | 10vw at 390px = 39px |
| vh to px | px = vh × viewport height ÷ 100 | 50vh at 1080px = 540px |
| in to px | px = in × 96 | 2in = 192px |
| cm to px | px = cm × 96 ÷ 2.54 | 5.08cm = 192px |
| mm to px | px = mm × 96 ÷ 25.4 | 10mm ≈ 37.8px |
| pc to px | px = pc × 16 | 3pc = 48px |
| px to pc | pc = px ÷ 16 | 80px = 5pc |
| em to rem | rem = em × parent font size ÷ root font size | 1.5em (20px parent, 16px root) = 1.875rem |
| rem to em | em = rem × root font size ÷ parent font size | 2rem (16px root, 20px parent) = 1.6em |
| rem to vw | vw = rem × root font size ÷ viewport width × 100 | 2rem at 1440px ≈ 2.2222vw |
px to rem, em, and pt Conversion Chart (16px Base)
Here are the sizes you'll reach for most often, already converted. The rem column uses the default
16px root, and the em column assumes a 16px font size on whatever the
em refers to. If your project uses the
62.5% root trick, read the last column
instead.
| px | rem (16px root) | em (16px) | pt | rem (62.5% root) |
|---|---|---|---|---|
| 4px | 0.25rem | 0.25em | 3pt | 0.4rem |
| 8px | 0.5rem | 0.5em | 6pt | 0.8rem |
| 10px | 0.625rem | 0.625em | 7.5pt | 1rem |
| 12px | 0.75rem | 0.75em | 9pt | 1.2rem |
| 14px | 0.875rem | 0.875em | 10.5pt | 1.4rem |
| 16px | 1rem | 1em | 12pt | 1.6rem |
| 18px | 1.125rem | 1.125em | 13.5pt | 1.8rem |
| 20px | 1.25rem | 1.25em | 15pt | 2rem |
| 24px | 1.5rem | 1.5em | 18pt | 2.4rem |
| 28px | 1.75rem | 1.75em | 21pt | 2.8rem |
| 32px | 2rem | 2em | 24pt | 3.2rem |
| 36px | 2.25rem | 2.25em | 27pt | 3.6rem |
| 40px | 2.5rem | 2.5em | 30pt | 4rem |
| 48px | 3rem | 3em | 36pt | 4.8rem |
| 56px | 3.5rem | 3.5em | 42pt | 5.6rem |
| 64px | 4rem | 4em | 48pt | 6.4rem |
| 72px | 4.5rem | 4.5em | 54pt | 7.2rem |
| 80px | 5rem | 5em | 60pt | 8rem |
| 96px | 6rem | 6em | 72pt | 9.6rem |
| 100px | 6.25rem | 6.25em | 75pt | 10rem |
px to vw Chart by Viewport Width
A vw value means nothing without a screen width next to it. The same 24px is
6.4vw on a 375px phone but only 1.25vw on a 1920px monitor,
so match the column to the width of your design file. For reference, 1vw is 3.75px,
14.4px, and 19.2px at these three widths.
| px | 375px wide | 1440px wide | 1920px wide |
|---|---|---|---|
| 8px | 2.1333vw | 0.5556vw | 0.4167vw |
| 16px | 4.2667vw | 1.1111vw | 0.8333vw |
| 24px | 6.4vw | 1.6667vw | 1.25vw |
| 32px | 8.5333vw | 2.2222vw | 1.6667vw |
| 48px | 12.8vw | 3.3333vw | 2.5vw |
| 64px | 17.0667vw | 4.4444vw | 3.3333vw |
| 96px | 25.6vw | 6.6667vw | 5vw |
| 100px | 26.6667vw | 6.9444vw | 5.2083vw |
CSS Units in Real-World Web Layouts
Now that you know the basics of how CSS units work, here is how each one behaves in real-world layouts. Each CSS unit has its own scaling logic, strengths, and edge cases, and knowing them helps you build layouts that stay responsive.
Relative CSS Units Explained
Relative units are what make a web design responsive and accessible, because they scale with user preferences and viewport dimensions. This is what makes it possible for a single stylesheet to look consistent across a 320px-wide phone as well as a 4K monitor.
Let's have a look at all the Relative Units supported by the Syntax Toolbox CSS Unit Converter:
rem - Root EM
1rem equals the font size declared on the html element. By default, browsers set this to
16px (unless changed), so the standard conversion becomes:
1rem = 16px
Because rem is tied to the root font size, it respects user browser settings and improves
accessibility. Adjusting the root value allows you to scale the entire interface proportionally.
html {
font-size: 100%; /* 16px by default */
}
.title {
font-size: 2rem; /* 2 x 16px = 32px */
}
.card {
padding: 1.5rem; /* 1.5 x 16px = 24px */
} - The html element's
font-sizeis100%, which is16pxby default, so1remequals16pxeverywhere in the document. -
Therefore, the title's
font-sizeis32px(2 ×16px) and the card's padding is24px(1.5 ×16px), no matter how deeply either element is nested.
For mixed contexts where element-specific scaling is required, use em instead.
em - Element Em
The em unit is relative to a font size, and which one depends on the property. On
font-size, 1em is the parent element's computed font size. On every other
property, such as padding or margin, it is the element's own font size. If no font size is explicitly set,
the element inherits the value from its parent, ultimately tracing back to the root.
Unlike rem, em is context-based and can compound in nested structures, since each
element calculates its font-size from its immediate parent rather than the root.
.notice {
font-size: 20px;
padding: 0.75em 1em; /* 15px 20px - tied to this block's own text */
}
.notice__tag {
font-size: 0.8em; /* 0.8 x 20px = 16px */
}
.notice__tag sup {
font-size: 0.8em; /* 0.8 x 16px = 12.8px, not 16px */
}
Notice what happens on the last rule. The tag already shrank to 16px, so its own child starts
measuring from 16px instead of the original 20px:
0.8 × 16px = 12.8px
This cascading behavior means values can grow or shrink quickly across deeply nested components. Because
of this, em must be used carefully in nested layouts where unintended scaling can occur due to
repeated multiplication.
For cases where sizing needs to be relative to the parent element's dimensions rather than font size, use
the % unit.
% - Percentage
On most layout properties, the % unit is relative to the size of the parent element. Unlike
em and rem, it is not based on a font size there, and it can apply to width,
height, padding, margins, and positioning.
The behavior of % depends on the property it is applied to, making it flexible for layout
control but context-dependent in calculation.
.shell {
display: flex;
width: 960px;
}
.shell__aside {
width: 25%; /* 25% of 960px = 240px */
padding: 2%; /* 19.2px on every side - resolved from width */
box-sizing: border-box; /* padding stays inside the 240px */
}
.shell__main {
width: 75%; /* 75% of 960px = 720px */
} - Widths resolve against the parent's width, so the two columns add up to the
960pxshell. - Percentage padding and margin always resolve against the parent's width, even for the top and bottom sides. That is what makes the classic padding-top aspect-ratio trick work.
- On
font-size, a percentage behaves exactly likeem:150%is 1.5 times the parent's font size.
% behaves similarly to em for font-size but is generally clearer for
layout dimensions.
For viewport-based sizing that responds to the browser window instead of the parent element, use
vw.
vw - Viewport Width
1vw equals 1% of the browser's current viewport width. For example, an element set
to width: 50vw will always occupy 50% of the viewport's horizontal space,
regardless of screen size.
Unlike %, which is relative to the parent element, vw is relative to the viewport
itself, making it useful for true screen-based scaling. For example, a phone has a smaller vw
than an HD TV.
.banner {
/* break a section out of a centered, max-width page */
width: 100vw;
margin-inline: calc(50% - 50vw);
}
.banner__inner {
width: 50vw; /* 1440px viewport = 720px */
padding-inline: 4vw; /* gutters grow with the screen */
}
One thing to keep in mind: vw counts the scrollbar width on desktop browsers, so an element set
to 100vw can overflow a page that already has a vertical scrollbar. Use 100% when
you only need the container's width.
vw allows for width-based scaling. For height-based viewport scaling, use vh.
vh - Viewport Height
1vh equals 1% of the browser's current viewport height. For example, an element
set to height: 25vh will always occupy 25% of the viewport's vertical space,
regardless of screen size.
.slide {
min-height: 100vh; /* one screen per slide */
scroll-snap-align: start;
}
.slide__figure {
height: 60vh; /* 1080px viewport = 648px */
}
.sticky-nav {
position: sticky;
top: 0;
max-height: 25vh; /* never eats more than a quarter of the screen */
overflow-y: auto;
}
On mobile browsers, 100vh can behave inconsistently due to dynamic browser UI (address bar,
toolbars). In such cases, newer units like svh, dvh, and lvh provide
more precise control over viewport behavior. Our CSS Unit Converter focuses on the classic units supported
across all browsers.
Absolute CSS Units Explained
Absolute units represent fixed physical (or logical) sizes. They do not respond to user preferences or viewport changes, making them predictable but less suitable for responsive interfaces. Absolute units shine in print stylesheets and designs where exact physical dimensions are required.
Let's explore the Absolute Units supported by the Syntax Toolbox CSS Unit Converter:
px - Pixels
CSS pixels are device-independent (logical) units, not physical screen pixels. One CSS pixel is defined as 1/96th of a logical inch.
On high-DPI (Retina) displays, the OS scales content so that multiple physical pixels are used to render one
CSS pixel. As a result, px remains visually consistent regardless of the device's actual pixel
density.
Although pixels are considered an absolute unit in CSS, they represent logical measurements instead of fixed
physical dimensions. That predictability is why px still earns its place in fine visual details
such as borders, dividers, and shadows, the parts of an interface that should not grow just because the text
around them did.
.card {
border: 1px solid #d0d7de; /* stays 1px whatever the font size */
border-radius: 8px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}
.card__divider {
height: 1px; /* a relative unit here could round down to 0 and vanish */
background: #d0d7de;
} pt - Points
Points are a typographic unit traditionally used in print design where one point equals 1/72 of an inch.
In web development, pt is rarely used for screen layouts, but it becomes relevant when matching
typography to print specifications or design files exported from applications such as Adobe InDesign or
Illustrator.
@media print {
body {
font-size: 11pt; /* 11 x 1.333 = 14.67px */
line-height: 1.4;
}
h1 {
font-size: 24pt; /* matches the 24pt heading in the print spec */
}
} pc - Picas
Picas are a typographic unit popularly used in professional publishing and grid-based print layouts. One pica equals 12 points, or 1/6 of an inch.
The difference between pt and pc is simple:
- pt = micro (type sizing).
- pc = macro (layout structure).
@media print {
.invoice {
width: 45pc; /* 45 x 12pt = 540pt = 7.5in text column */
margin: 0 auto;
}
.invoice__section + .invoice__section {
margin-top: 2pc; /* 24pt of vertical rhythm between blocks */
}
} in - Inches
Inches are a physical measurement unit commonly used in print and hardware specifications. One inch is
defined as 96px in web contexts.
Unlike typographic units such as pt and pc, inches represent real-world physical
length, which makes them useful for print layout systems, packaging dimensions, and print-preview accuracy.
On screen, an inch is always 96px, so its physical size varies from one display to another and rarely matches a ruler.
@page {
size: 8.5in 11in; /* US Letter */
margin: 0.75in;
}
.certificate {
width: 7in; /* 8.5in page minus both 0.75in margins */
padding: 0.5in;
box-sizing: border-box; /* padding stays inside the 7in */
} cm - Centimeters
Centimeters are a metric physical unit used to represent real-world length. In CSS, 1cm is
defined as 96px ÷ 2.54, or approximately 37.795px.
Like inches and points, centimeters are primarily a print unit. They are useful when your design references metric measurements or when generating PDFs and print layouts through the browser.
@page {
size: 21cm 29.7cm; /* A4 */
margin: 1.5cm;
}
.label-sheet__item {
width: 6.4cm;
height: 3.4cm; /* standard address label */
padding: 0.3cm;
box-sizing: border-box; /* keeps the label 6.4cm x 3.4cm */
} mm - Millimeters
Millimeters are another physical metric unit, where 1mm is defined as approximately
3.78px in CSS.
Compared to cm, millimeters are used for finer detail and tighter measurement control in
print-based layouts, especially when precision matters in spacing, margins, or small-scale design elements.
In web development, mm is the finer of the two metric units covered here, and it is useful for print layouts and PDFs generated in the browser.
@page {
size: 210mm 297mm; /* the same A4 sheet, in millimeters */
margin: 12mm;
}
.business-card {
width: 85mm;
height: 55mm;
border: 0.2mm dashed #999; /* fine cut guide */
} Relative vs. Absolute CSS Units: Key Differences
| Key Difference | Relative Units | Absolute Units |
|---|---|---|
| Responsiveness | Highly responsive; values adapt based on surrounding context like parent, root, or viewport. | Not responsive; values are fixed and do not adjust based on screen size or resolution. |
| Dependency | Dependent on relative factors like font size, container, or viewport dimensions. | Independent of parent elements or viewport for calculation. |
| Scalability | Automatically adjusts to different screen sizes. | Less adaptable across different screen sizes. |
| Flexibility | Size may vary depending on parent or viewport, allowing for more fluid and dynamic layouts. | Provides consistent and exact sizing regardless of environment. |
| Accessibility | Adapt to user settings such as base font size, improving accessibility. | Do not respond to the user's browser font size setting, hindering accessibility. |
| Computation | Need a reference value, such as a font size or viewport width, to resolve to pixels. | Resolve to a fixed number of CSS pixels without a reference value. |
Choosing the Right CSS Unit for Your Layout
Instead of thinking in terms of individual units, think in terms of what the value should respond to: the root, the parent, or the viewport. Once you know that, the unit is easy to pick.
Font sizes
- Use
remfor font sizes. It scales with the user's browser font preference and provides consistent sizing across the entire document. - Avoid
pxfor font sizes, as it ignores the user's default font size setting (browser zoom still works).
Padding and Margin
- Use
remfor global spacing to keep it proportional to typography. - Use
emwhen you want spacing to scale relative to the component's own font size. Ideal for buttons and badges. - Avoid
pxfor layout spacing unless you need strict pixel precision.
Layout Widths and Columns
- Use
%for fluid columns inside flex or grid containers. - Use
vwfor full-viewport-width elements.
Heights
- Use
vhfor full-screen sections (e.g., hero banners, landing screens). - Use auto for content-driven height (recommended default).
- Use fixed units (
remorpx) only when height must be strictly controlled (e.g., toolbars, inputs, buttons).
Borders and Box Shadows
- Use
pxfor borders and shadows. These are fine visual elements that should remain consistent regardless of text or layout scaling. - Avoid relative units here, as they can make UI elements appear inconsistent across components.
Media Query Breakpoints
- Prefer
emorremoverpx. These units respond to userfont-sizesettings, making layouts more accessible and adaptable. - Convert breakpoints with
16as the base, even if your stylesheet uses the 62.5% trick. Inside a media query,emandremignore the root size you set in CSS and go by the browser's own font size instead. - The usual ones are
640px=40em,768px=48em,1024px=64em,1280px=80em, and1536px=96em. The numbers are the same inrem.
Print Stylesheets
- Use physical units like
pt,pc,cm,mm. These map directly to real-world print dimensions and ensure accurate printed output.
Icons and SVG
- Use
emwhen icons should scale with surrounding text (best for inline UI icons). - Use
pxwhen precise, fixed-size control is required (e.g., logos or tightly aligned UI elements).
Practical Use Cases of CSS Unit Conversions
Let's understand the above examples with practical use cases of CSS unit conversions because knowing the theory is one thing but actually knowing when to apply them in a real project is another.
Translating a Figma design to code
Most design tools, including Figma, use pixels as their default unit. So if a design specifies a heading
size of 32px, but your project uses a rem-based typography system with a root font size of
16px, you simply divide 32 by 16 to get 2rem.
Converting your typography this way keeps your design consistent while allowing text to scale according to the user's browser settings automatically.
Scaling spacing from a pixel-based system
Suppose your design system defines a base spacing unit of 8px. To convert each value to
rem, divide it by the root font size (typically 16px).
8px → 0.5rem
16px → 1rem
24px → 1.5rem
32px → 2rem
Using rem for spacing ensures that padding and margin scale proportionally if the root font size
is ever adjusted. For instance, if you use the popular 62.5% root trick (making
1rem = 10px), the same 8px becomes 0.8rem.
Converting pixel widths to viewport units
Say you want a hero banner to fill exactly half the screen width on all devices. Your reference viewport is
a 1920px wide artboard, and the hero section is 960px wide. To convert that width
into vw, use the following calculation:
vw = (960 ÷ 1920) × 100 = 50vw
This means the hero section will always occupy 50% of the viewport width. Whether the user is
viewing your site on a 375px phone or a 2560px desktop monitor, the element will
continue to take up half of the available screen width.
Matching web typography to print specs
When creating a print stylesheet or generating a browser-based PDF, your designer may hand you type sizes in
points (as is standard in tools like Adobe InDesign). For example, if the body text is specified as
12pt, you can convert it to pixels for comparison with your web styles:
px = 12 × (96 ÷ 72) = 16px
This demonstrates that 12pt is exactly equal to 16px, which is also the browser's
default body text size. Knowing this relationship makes it easier to translate print typography into web
styles while keeping text sizes consistent across both mediums.
Component spacing with em
Buttons, badges, and form controls often need padding that grows and shrinks as the text size changes.
Suppose a button has a font-size of 14px and you want 10px of
horizontal padding. You can convert the padding to em using:
em = 10 ÷ 14 ≈ 0.71em
Now if the button's font size changes (e.g., in a compact variant), the padding will adjust automatically, eliminating the need for separate overrides.
CSS Unit Tips, Tricks, and Best Practices
Let's move beyond the serious theory and practicals to explore a handful of small tricks that can save you significant conversion work. Each one works in all current browsers, and every value used here can be cross-checked with the Syntax Toolbox CSS Unit Converter.
The 62.5% root font size trick
There's a simple CSS trick for making px → rem conversions almost effortless. Just
set the root font size to 62.5%.
Now 24px becomes 2.4rem, 8px becomes 0.8rem, and you can
convert most values at a glance without reaching for a calculator.
html {
font-size: 62.5%; /* 16px x 0.625 = 10px */
}
body {
font-size: 1.6rem; /* back to 16px */
}
The trick is to use a percentage rather than hard-coding 10px. With
html { font-size: 62.5%; }, the root size remains relative to the user's browser
settings, so you get the convenient 10px baseline without unnecessarily overriding
accessibility preferences.
One small catch: reset your body text to 1.6rem (or whatever base size you want). Otherwise,
your default text will end up at 10px.
Fluid typography with clamp()
Tired of writing three media queries for one heading?
There's an easier way to make your typography scale smoothly. clamp() takes a minimum, a
preferred value, and a maximum, then adjusts the font size fluidly between those limits as the viewport
changes.
h1 {
/* never below 28px, never above 56px */
font-size: clamp(1.75rem, 1.25rem + 2.5vw, 3.5rem);
}
The trick is to keep a rem value in the middle expression. A preferred value made entirely of
vw, such as 5vw, can stop text from growing on zoom, because zooming in shrinks
the viewport in CSS pixels and the vw value shrinks with it. Mixing rem with
vw gives you fluid sizing while still allowing the text to scale properly when users zoom the
page.
Use ch to control line length
Long paragraphs can become surprisingly difficult to read when they stretch across the full width of a large
screen. To keep them under control, try a CSS unit called ch.
1ch equals the width of the '0' character in the current font. Since a comfortable line length
is usually around 60 to 75 characters, something like max-width: 65ch gives you a solid
starting point.
.article-body {
max-width: 65ch;
}
The trick is that ch is tied to the element's font. If you change the font family or size
later, the measure adjusts with it. A fixed max-width in px would stay the same and
might need manual retuning.
Fix the mobile 100vh problem with dvh
If you've ever built a full-screen section that looks perfect on desktop but gets cut off by the address bar
on mobile, 100vh is usually the reason.
On mobile browsers, 100vh can be based on the largest possible viewport rather than the area
currently visible on screen. 100dvh tracks the dynamic viewport instead, so the height adjusts
as the browser's UI appears and disappears. That change can shift content while the user scrolls, so use
svh if you need a steady height.
.hero {
height: 100vh; /* fallback for older browsers */
height: 100dvh; /* wins wherever it is supported */
}
The trick is to declare both values: use 100vh as the fallback, then override it with
100dvh. Browsers that don't support dvh will ignore the second declaration and
keep using the vh value.
Mix unit systems with calc()
Sometimes a layout needs to combine relative and absolute units, and that's where calc() comes
in. You can mix units like %, vw, rem, and px in a
single expression and eliminate guesswork from layout maths.
.main {
/* full width minus a 20rem sidebar */
width: calc(100% - 20rem);
}
.page {
/* full screen minus a 4rem sticky header */
min-height: calc(100vh - 4rem);
}
One small detail you must adhere to: keep spaces around the + and - operators. For example,
calc(100%-20rem) is invalid, so the browser drops the entire declaration. The proper expression
would be calc(100% - 20rem).