Sizing utilities help you quickly set the width and height of elements without writing custom CSS.
Sizing utilities (width, height) in Bootsrap
w-{value} // for width
h-{value} // for height
// {value} can be:
// 25, 50, 75, 100 (percentages)
// autoUse w- classes to set width and h- classes to set height.
Values like 25, 50, 75, and 100 represent percentages.
<div class="w-50">This div is 50% wide</div>
<img src="image.jpg" class="h-25" alt="Sample image">
<div class="w-100 h-100">Full width and height</div>
<div class="w-auto">Width adjusts to content</div>
This example shows boxes with different width and height percentages using Bootstrap sizing utilities. The boxes have a blue background and white text for clear visibility.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Sizing Utilities Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <style> .box { background-color: #0d6efd; color: white; display: flex; align-items: center; justify-content: center; margin-bottom: 1rem; border-radius: 0.5rem; } </style> </head> <body> <main class="container mt-4" style="height: 100vh;"> <h1>Sizing Utilities (width, height)</h1> <div class="box w-25 h-25">25% width & height</div> <div class="box w-50 h-50">50% width & height</div> <div class="box w-75 h-75">75% width & height</div> <div style="height: 200px;"> <div class="box w-100 h-100">100% width & height (200px height set on parent)</div> </div> <div class="box w-auto" style="height: 50px;">Width auto, height 50px</div> </main> </body> </html>
Remember that height percentages depend on the parent's height being set.
Use w-auto or h-auto to let the size adjust to content.
Combine sizing utilities with responsive classes like w-sm-50 for different screen sizes.
Sizing utilities let you quickly set width and height using simple classes.
Use w- for width and h- for height with percentage or viewport values.
They help keep your design consistent and responsive without writing CSS.
Practice
Solution
Step 1: Understand width and height prefixes
Bootstrap usesw-for width andh-for height classes.Step 2: Identify the class for 50% width
The classw-50sets width to 50%, whileh-50sets height to 50%.Final Answer:
<code>w-50</code> -> Option AQuick Check:
Width 50% = w-50 [OK]
- Confusing width (w-) with height (h-)
- Using w-100 instead of w-50
- Assuming h-50 sets width
Solution
Step 1: Recall Bootstrap height classes
Bootstrap usesh-100to set height to 100% of the parent container.Step 2: Viewport height in Bootstrap
Bootstrap does not have a classvh-100to set height to 100vh. To set height to viewport height, custom CSS or utility classes are needed.Final Answer:
<code>h-100</code> -> Option BQuick Check:
Height 100% of parent = h-100 [OK]
- Using h-vh-100 or h-100vh which are invalid
- Assuming vh-100 is a Bootstrap class
- Confusing viewport height with parent height
<div class='w-75'></div> element relative to its parent container?Solution
Step 1: Understand the meaning of w-75
The classw-75sets the element's width to 75% of its parent container.Step 2: Compare options with the class effect
Only 75% of the parent container correctly states 75% width relative to the parent.Final Answer:
75% of the parent container -> Option DQuick Check:
w-75 means width 75% [OK]
- Mixing up 75% with 25%
- Thinking w-75 means height
- Assuming w-75 means 100% width
<div class='w-25'></div> does not work as expected. What is the error?Solution
Step 1: Identify the class used
The classw-25sets width to 25%, not height.Step 2: Correct the class for height
To set height to 25%, useh-25instead ofw-25.Final Answer:
Using width classw-25instead of height classh-25-> Option AQuick Check:
Height needs h-25, not w-25 [OK]
- Confusing width and height classes
- Assuming w-25 affects height
- Thinking Bootstrap lacks height utilities
Solution
Step 1: Understand responsive sizing classes
Bootstrap uses breakpoint prefixes likemdto apply styles at medium screen sizes and above.Step 2: Choose classes for width at different breakpoints
w-100sets width 100% on all screens, butw-md-50overrides width to 50% on medium and larger screens.Step 3: Verify other options
Options B, C, and D either reverse the sizes or mix height and width incorrectly.Final Answer:
<code>w-100 w-md-50</code> -> Option CQuick Check:
Responsive width: w-100 then w-md-50 [OK]
- Mixing height and width classes
- Reversing breakpoint order
- Not using breakpoint prefixes for responsiveness
Ready to prove it?
Pass the test and this pattern moves unknown
