Skip to content
Terence Eden’s Blog·

📐CSS Pixels Aren't Perfect Squares Anymore

Why your CSS lengths might be off on HD screens

TL;DR

CSS pixel sizes can vary based on display resolution, making absolute measures unreliable. Use relative units like ch and lh for more consistent layouts.

Pixels in CSS don't exist as perfect squares on monitors; they can span multiple physical pixels depending on the screen's resolution. This means that setting lengths in cm or even px might not give you the exact dimensions you expect, especially on HD displays. Developers should opt for relative units like ch (character width) and lh (line height) to ensure consistent layouts across different devices. For instance, using min(75ch, 100%) as a main width can adapt better to varying screen sizes and resolutions.

CSS Pixels Aren't Perfect Squares Anymore — Terence Eden’s Blog

Key Points

1

CSS cm lengths don't always equal one hundredth of a meter, especially on large TV screens where 1cm can be longer than expected.

2

The ch unit represents the width of the '0' character in the font and is useful for setting text width based on character size.

3

Padding and margins can also use relative units like ch or lh to adapt better to different screen resolutions.

4

System fonts eliminate potential issues with combining ch units and web fonts, ensuring consistent layout across devices.

5

The ex unit represents the height of a lowercase 'x' in the font, useful for setting line heights and vertical spacing.

Why It Matters

If you're designing responsive layouts, using relative CSS measures like ch or lh can save hours of debugging. For example, setting padding to 1.5lh ensures consistent line height across devices, while min(75ch, 100%) for main width adapts better to varying screen sizes. This approach simplifies layout management and enhances user experience.

cssresponsive-designlayoutweb-development

Frequently Asked Questions

Why does this matter?

If you're designing responsive layouts, using relative CSS measures like ch or lh can save hours of debugging. For example, setting padding to 1.5lh ensures consistent line height across devices, while min(75ch, 100%) for main width adapts better to varying screen sizes. This approach simplifies layout management and enhances user experience.

What happened?

CSS pixel sizes can vary based on display resolution, making absolute measures unreliable. Use relative units like ch and lh for more consistent layouts.

Comments

Subscribe to join the conversation...

Be the first to comment

Enjoyed this article?

Get it daily. 7am. Free. Reads in 5 minutes.

Join 3,303 builders reading daily.