liveBook · Manning

Width

As explained in Web Design Playground, Second Edition

The CSS width property specifies the horizontal size of an element's content box or replaced content.

Width controls an element’s horizontal size. Browsers normally make a block-level element as wide as its container—often the browser window—and let its content determine its height. Setting width is how you take the wheel instead of leaving every line to roam free. section 9.2

Think in boxes: an element has content, padding, a border, and a margin. Width and height set its dimensions, while padding, borders, and margins add the surrounding space and structure. section 9.7 section 9.0

Use width with a value that suits the job:

  • A measurement unit such as px, em, rem, vw, or vh. section 9.2
  • A percentage, when the width should relate to its container. section 9.2
  • auto, the default, when the browser should calculate the width. section 9.2
  • A block-level element such as div or p; ordinary inline elements such as span and a ignore width. Use display: inline-block when an inline element needs block-like sizing without leaving the text flow. section 9.2

Give text room to breathe

A body width of 550px can keep page text from stretching into exhausting, hard-to-scan lines. For screen reading, aim for roughly 65–75 characters per line; changing font size changes that line length too. Tiny CSS change, noticeably happier eyeballs. section 5.5

div {
    box-sizing: border-box;        #1
    width: 630px;                  #2
}

This rule makes a div 630px wide and uses border-box sizing, pairing an explicit width with the box-model setting shown alongside it. section 9.2

Width can also shorten a horizontal rule so it does not run across its entire container. For text boxes, prefer setting width rather than a fixed height: content can grow, and fixed heights can leave text with nowhere sensible to go. section 3.6 section 9.2

How Web Design Playground, Second Edition covers it

How deep it goes
Explains browser defaults, block-versus-inline behavior, units, percentages, auto, and box-model context; defers layout in earnest.
Examples
Demonstrates a 550px body, a 630px div, and a shortened horizontal rule.
What it assumes
Introduces default sizing from zero while distinguishing block-level and inline elements.
What you can do after
Apply it via CSS to set block widths and tune line length.
How it connects
Connects width with height, the CSS box model, padding, borders, margins, display, and typography line length.

Also explained in

Compare how 3 books explain width →

Related concepts

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