liveBook · Manning

Pixel

As explained in CSS in Depth, Second Edition

A CSS pixel unit represents a length in CSS pixels, an abstract unit used for rendering dimensions.

A CSS pixel (px) is a CSS length unit used for dimensions and viewport thresholds. It carries design measurements directly into CSS, while em and rem can make measurements respond to font sizing. section 7.2 section 12.1.1

Choose between pixels and relative units according to how the spacing should behave:

  • Use px for larger gaps or spacing between boxes when resizing with text is not needed. section 12.1.1
  • Consider em or rem for small measurements around text and buttons when they should respond to the user’s font-size setting. section 12.1.1
  • Use px or em for media-query breakpoints; em is based on the browser’s default font size, which can vary with user settings. section 7.2

Convert a design measurement

For a 10 px navbar gap with a 16 px base font size, use 0.625em. Larger layout gaps can remain in px, such as a 40 px margin below a hero image or a 25 px gap between columns. section 12.1.1 section 12.1.1

Listing 7.7 Adding a breakpoint to the page title styles
.title > h1 {
  color: #333;
  text-transform: uppercase;
  font-size: 1.5rem;
  margin-block: 0.2em;
}

@media (min-width: 560px) {     #1
  .title > h1 {
    font-size: 2.25rem;     #2
  }
}

A 560px viewport breakpoint increases the title’s font size on wider screens. section 7.2

How CSS in Depth, Second Edition covers it

How deep it goes
Compares px with em and rem for breakpoints and spacing, leaving unit choice to case-by-case judgment.
Examples
Shows a 560px title breakpoint, 10px navbar spacing converted to 0.625em, and 40px and 25px layout gaps.
What you can do after
Apply px to viewport breakpoints and fixed design gaps, choosing relative units when text-adjacent spacing should scale with font settings.
How it connects
Connects pixel values to media queries, viewport width, em and rem, and responsive design.

Also explained in

Compare how 10 books explain pixel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.