SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
The Box Model and SizingWriting Modes and Logical PropertiesSizing ElementsBorders and ShadowsDisplay and Document FlowNormal FlowFloats and ColumnsPositioning and StackingStacking and Z-Index Measuring boxes and predicting their positions
Box Model and Flow

Sizing Elements

Set dimensions, constraints, and aspect ratios.

A card should fit its container and leave room for its content. Use widths, heights, and size limits to control both.

Start with automatic sizing

For an ordinary block in normal flow, width: auto uses the available horizontal space after accounting for margins, padding, and borders. height: auto usually grows with the content.

.card {
  box-sizing: border-box;
  width: auto;
  height: auto;
  padding: 1rem;
  border: 2px solid green;
}

The card can become taller when its text wraps onto more lines. An explicit height would limit that space and could leave content overflowing.

Choose a width and constrain it

Keep the card’s padding, border, and box sizing. Change its width and add the two constraints below.

.card {
  width: 90%;
  max-width: 40rem;
  margin: 0 auto;
}

Here the card’s containing block is the space inside its parent’s padding. The parent’s content width supplies the reference for the percentage. The card takes 90% of that width, capped at 40rem.

If the root font size is 16 pixels:

  • 400-pixel container: The card is 360 pixels wide.
  • 1000-pixel container: The 900-pixel percentage result is capped at 640 pixels.

Because the card is narrower than its container, the left and right auto margins share the remaining horizontal space and center it. Its border-box setting keeps padding and borders inside these widths.

Set size limits

The sizing properties play different roles:

PropertyRole
width, heightRequest a size.
min-width, min-heightEstablish a lower limit.
max-width, max-heightEstablish an upper limit.

For a welcome panel that should have some height but grow with its text:

.welcome {
  min-height: 12rem;
  padding: 1rem;
}

With automatic height, the panel can expand beyond its minimum. By contrast, max-height alone does not create scrolling when content is taller; overflow handling is a separate decision.

Choose limits that can fit the available space. A min-width: 500px box can overflow a 320-pixel container. If a minimum and maximum conflict, the minimum wins in ordinary sizing.

Give percentages a usable reference

In a normal block layout, a percentage height usually works when the containing block has a known height:

.preview {
  height: 300px;
}

.preview-panel {
  height: 50%;
}

For a panel directly inside this preview, the height is 150 pixels. If the preview instead has an automatic height that depends on its content, the child's height: 50% generally behaves as auto. A percentage is useful only when its reference can be resolved.

Set proportions with aspect-ratio

For a separate, empty preview box:

.preview {
  width: 320px;
  max-width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  background-color: honeydew;
}

At 320 pixels wide, the box aims for a height of 180 pixels. Leave at least one dimension automatic so aspect-ratio can calculate it. Content and size limits can change the final shape.

Compare a smaller container

The card above requests 90% width and is capped at 40rem. If the root font size is 16 pixels and its container is 600 pixels wide, first find 90%: 600 × 0.9 = 540px. Does the 640-pixel cap change that result?

Check the last step

No. 540 pixels is below the 640-pixel maximum, so the card is 540 pixels wide.

Now calculate its width in an 800-pixel container. Explain which constraint wins.

Check the comparison

90% is 720 pixels, so max-width caps the card at 640 pixels. The remaining space is shared by the automatic horizontal margins.

Key Takeaways

  • Automatic sizing: Normal block boxes can fill available width and grow vertically with content.
  • Constraints: Minimum and maximum dimensions limit the requested size.
  • Percentages: A percentage depends on the containing block and the property being sized.
  • Proportions: aspect-ratio supplies a preferred relationship when a dimension is automatic.

Try it yourself

Try it yourself

Constrain a card

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

How is this lesson?

Previous lessonWriting Modes and Logical PropertiesNext lessonBorders and Shadows
Edit on GitHubReport on GitHub
On this page
%

On this page

Start with automatic sizingChoose a width and constrain itSet size limitsGive percentages a usable referenceSet proportions with aspect-ratioCompare a smaller containerKey TakeawaysTry it yourself