Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 58 ยท Layout

Sizing Utilities

Set widths and heights with Bootstrap w-*, h-* and mw-100, use viewport sizes, keep video and map shapes with the ratio helper, crop images with object-fit.

The grid decides how wide the big regions of a page are. But plenty of smaller sizing questions come up every day: make this button full width, make this card as tall as its neighbour, keep this video in a 16:9 box, stop this image from sticking out of its column. Bootstrap answers them with a small family of sizing utilities, plus the ratio helper and the object-fit classes.

This lesson covers w-* and h-*, mw-100 and mh-100, viewport-based sizes (vw-100, vh-100), the ratio helper, object-fit-* for images, and a checklist for fixing overflow. You can combine all of them with the columns from Responsive Columns.

Width and height in percentages

The core classes set a size as a percentage of the parent:

Class CSS
w-25 width: 25%
w-50 width: 50%
w-75 width: 75%
w-100 width: 100%
w-auto width: auto
h-25, h-50, h-75, h-100, h-auto the same values for height

Notice that percentages are relative to the parent box. w-50 inside a 400 px container is 200 px; inside a narrow column it is half of that column.

Widths relative to the parent

Heights need one extra thought. A percentage height only works when the parent has a definite height, otherwise it has nothing to be a percentage of and the browser ignores it. In the next example the parent has a fixed height, so h-50 and h-100 make sense:

Percentage heights need a tall parent

A common real-world use of h-100 is equal-height cards. The columns in a row already stretch to the same height, so putting h-100 on the card inside each column makes the cards fill them. You will see this in Cards.

Maximum sizes

mw-100 sets max-width: 100% and mh-100 sets max-height: 100%. They do not make an element bigger; they only stop it from growing beyond its parent. That makes them the safe choice for things with their own natural size, such as images, videos and iframes. An image that is 1,200 px wide stays at its natural size inside a 300 px column unless you cap it:

HTML
<img src="photo.jpg" class="mw-100 h-auto" alt="...">

For images you can use the dedicated img-fluid class, which does the same thing; see Images and Figures. Note h-auto: when only the width is capped, the height must follow the image's own proportions instead of keeping a fixed height attribute.

Viewport sizes

Four classes size an element relative to the window rather than the parent:

Class Meaning
vw-100 width: 100vw, the full window width
vh-100 height: 100vh, the full window height
min-vw-100 min-width: 100vw
min-vh-100 min-height: 100vh

The classic use is a hero or login page that fills the screen: min-vh-100 guarantees at least the window height but lets the content grow taller if needed, which is nearly always better than a rigid vh-100.

A section that fills the window height

Scroll inside the preview, or change the window height, and the section follows. The d-flex align-items-center classes center the text vertically; they are covered in Flex Utilities.

Be careful with vw-100. The window width includes the vertical scrollbar on many systems, so vw-100 can be wider than the visible page and cause a horizontal scrollbar. To fill a parent's width, use w-100; keep vw-100 for elements that must break out of their container on purpose.

The ratio helper

Embedded content such as videos, maps and slideshows should keep its shape as the page gets narrower. The ratio helper creates a box with a fixed aspect ratio and makes its child fill the box. Wrap the content, add ratio plus one ratio class:

Class Aspect ratio
ratio-1x1 square
ratio-4x3 4:3
ratio-16x9 16:9 widescreen
ratio-21x9 21:9 ultrawide

The direct child of a .ratio element is stretched to fill it (position: absolute with width and height 100%), so you can place an iframe, video, embed or a plain div there.

For a ratio of your own, set the CSS variable --bs-aspect-ratio to the height as a percentage of the width. A box that is 2 wide and 1 tall has a ratio of 50%.

Ratio helper

Resize the window and every box keeps its proportions. A real embed would look like <div class="ratio ratio-16x9"><iframe src="..." title="..."></iframe></div>. Always give embeds a title for screen readers. Embedding third-party pages inside this tutorial's sandbox is blocked, so the demo uses plain boxes.

flowchart LR
  A["div.ratio.ratio-16x9<br/>width from the parent"] --> B["height from the ratio<br/>(width x 9 / 16)"]
  B --> C["child fills the box<br/>iframe, video or div"]

The width comes from the layout; the ratio class works out the height.

Fitting images into boxes with object-fit

Sometimes you need pictures of different shapes to fill identical boxes, for example a card grid with thumbnails. If you force both width and height, a normal image is squashed. The object-fit-* classes (added in Bootstrap 5.3) decide what happens instead:

  • object-fit-contain shows the whole image inside the box, leaving empty space if the shapes differ.
  • object-fit-cover fills the box completely and crops what does not fit. This is the usual choice for thumbnails.
  • object-fit-fill stretches the image to the box (distorts it).
  • object-fit-scale picks the smaller of none and contain.
  • object-fit-none keeps the natural size and crops.

Each has responsive versions such as object-fit-md-cover.

object-fit comparison

The same wide picture appears four times. In "fill" the circle is squashed into an oval, in "contain" the whole picture fits with gaps above and below, and "cover" fills the box with the circle centered but the sides cropped. For consistent thumbnails, combine object-fit-cover with a fixed height or a ratio box.

Fixing overflow

A horizontal scrollbar on a phone almost always means something is wider than its container. Work through this list:

  1. Images and videos. Add img-fluid or mw-100 h-auto.
  2. Long unbroken text such as URLs or long words: add text-break (see Text Utilities).
  3. Wide tables. Wrap them in .table-responsive so only the table scrolls (Tables).
  4. Code blocks. Give <pre> the class overflow-auto (or overflow-x-auto) so it scrolls inside its own box.
  5. Rows outside containers, or vw-100 inside a container with a scrollbar (see the warning above).
  6. Fixed pixel widths. Replace width: 600px with mw-100 and a percentage width.

Find the culprit quickly. In your browser's developer tools, hover over elements in the Elements panel while the page is narrow. The highlighted box shows which element sticks out past the right edge.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A percentage height such as h-50 does nothing in your layout. What is the most likely reason?

    A percentage height needs a parent with a definite height to be a percentage of. Give the parent a height, or use another approach such as flex or min-vh-100.
  2. 2. Which class creates a section that is at least as tall as the window but may grow with its content?

    min-vh-100 sets a minimum height of 100vh. vh-100 would fix the height and could clip content.
  3. 3. In the ratio helper, which value of --bs-aspect-ratio gives a box twice as wide as it is tall?

    The value is height divided by width, written as a percentage: 1 / 2 = 50%.
  4. 4. You need thumbnails of different shapes to fill identical boxes without distortion. Which class is the usual choice?

    object-fit-cover scales the image to fill the box and crops the overflow, keeping its proportions.

Summary

  • w-25/50/75/100/auto and h-25/50/75/100/auto size an element as a percentage of its parent; percentage heights need a parent with a defined height.
  • mw-100 and mh-100 cap an element at its parent's size, which is how you keep images and embeds inside their columns.
  • vw-100, vh-100, min-vw-100 and min-vh-100 use the window as the reference; prefer min-vh-100 for full-screen sections.
  • ratio plus ratio-1x1, ratio-4x3, ratio-16x9 or ratio-21x9 keeps embeds in proportion; custom ratios use --bs-aspect-ratio.
  • object-fit-cover, -contain, -fill, -scale and -none control how an image fits a fixed box.

Next lesson: Typography.