Width, Height, and Box Sizing

width and height set the the width and height of an element, respectively.

| LIVE PREVIEW
Hi!

Width and Height Properties

Working with Inline Elements

Setting width and/or height (including min-height, etc.) does not work with inline elements. Try using display: inline-block instead.

width

Set's the element's width.

Possible values:

  • length(e.g. 100px) sets the width to this length
  • percentage(e.g. 50%) sets the width to this percentage of the parent's width
  • autosets the width automatically
  • max-contentsets the width to be as large as the content needs
  • min-contentsets the width to be as little as possible, without causing an overflow
  • fit-contentsets the width to be as large as the content needs, but never exceeds the parent's content area width.
Illustration of max-content, min-content, and fit-content

height

Set the element's height.

Possible values:

  • length(e.g. 100px) sets the height to this length
  • percentage(e.g. 50%) sets the height to this percentage of the parent's height
  • autosets the height automatically

min-width

Ensures the element's width does not go lower than this value.

(See width for the possible values.)

min-height

Ensures the element's height does not go lower than this value.

(See height for the possible values.)

max-width

Ensures the element's width does not go higher than this value.

(See width for the possible values.)

max-height

Ensures the element's height does not go higher than this value.

(See height for the possible values.)

Inline and Block Size Properties

inline-size

Sets the size in the inline direction.

(Which direction this is depends on writing-mode. Normally refers to width when you write horizontally.)

block-size

Sets the size in the block direction.

(Which direction this is depends on writing-mode. Normally refers to height when you write horizontally.)

min-inline-size

Ensures the element's inline size does not go lower than this value.

min-block-size

Ensures the element's block size does not go lower than this value.

max-inline-size

Ensures the element's inline size does not go higher than this value.

max-block-size

Ensures the element's block size does not go higher than this value.

Box Sizing Property

box-sizing

Specifies what to be sized when setting width and height.

Possible values:

  • content-boxaccounts only the element's content area to match width and height (default)
  • border-boxaccounts everything (including the element's border and padding area) to match width and height
Illustration of CSS Box model
Illustration of CSS Box model

References