Discover how a few simple classes can save you hours of CSS headaches!
Why Display utilities in Bootsrap? - Purpose & Use Cases
Imagine you want to show or hide parts of your webpage depending on the screen size or user action. You try to write custom CSS for each element to control when it appears or disappears.
Writing custom CSS for every element is slow and confusing. You might forget to update styles for different screen sizes, causing your page to look broken or inconsistent. It's hard to keep track of all these rules.
Display utilities in Bootstrap let you quickly show, hide, or change how elements appear using simple classes. You don't write custom CSS; just add a class and the element behaves as you want on different devices.
/* CSS */ .hide-on-mobile { display: none; } @media (min-width: 768px) { .hide-on-mobile { display: block; } }<div class="d-none d-md-block">Content</div>
You can easily control element visibility across devices with simple class names, making your site responsive and user-friendly without extra CSS.
On a product page, you want to hide a large image on small phones but show it on tablets and desktops. Using display utilities, you add classes to the image to handle this automatically.
Manual CSS for showing/hiding elements is slow and error-prone.
Bootstrap display utilities use simple classes to control visibility.
This makes responsive design easier and faster to build.
Practice
d-none do to an element?Solution
Step 1: Understand the
Thed-noneclass purposed-noneclass in Bootstrap sets the CSS propertydisplay: none;which hides the element completely.Step 2: Confirm effect on all screen sizes
Since no breakpoint is specified,d-noneapplies to all screen sizes, hiding the element everywhere.Final Answer:
It hides the element on all screen sizes. -> Option AQuick Check:
d-none= hide element always [OK]
- Thinking d-none only hides on small screens
- Confusing d-none with color or interaction changes
- Assuming it disables the element
Solution
Step 1: Understand breakpoint classes for visibility
d-nonehides the element on all screens, andd-md-blockshows it as block starting from medium screens and up.Step 2: Combine classes to show only on medium and larger
Usingd-none d-md-blockhides the element on small and extra small screens, then shows it on medium and larger.Final Answer:
d-none d-md-block -> Option AQuick Check:
Hide all + show md+ = d-none d-md-block [OK]
- Using d-md-none which hides on medium screens
- Confusing d-block and d-none order
- Using wrong breakpoints like d-sm-block d-md-none
<div class="d-none d-lg-block">Hello</div>
Solution
Step 1: Analyze the classes
d-noneandd-lg-blockd-nonehides the element on all screen sizes by default.d-lg-blockoverrides this to show the element as block starting at large screens (lg) and above.Step 2: Determine visibility on screen sizes
From extra small to medium screens, the element is hidden. From large screens and up, it becomes visible as a block element.Final Answer:
Visible only on large screens and above -> Option CQuick Check:
d-none + d-lg-block = show lg+ only [OK]
- Thinking it is visible on all screens
- Confusing d-lg-block as hiding on large screens
- Ignoring the effect of d-none
Solution
Step 1: Understand the goal and class effects
To hide only on small screens, the element should be visible by default and hidden on small screens only.Step 2: Analyze each option's effect
d-none d-sm-blockhides the element on all screens by default, then shows it from small screens and up. This means it is hidden only on extra small screens, not small screens.Final Answer:
d-none d-sm-block -> Option DQuick Check:
Hide all then show sm+ hides xs only, not sm [OK]
- Mixing up small (sm) and extra small (xs) breakpoints
- Using d-none without proper show class
- Assuming d-sm-block hides on small screens
Solution
Step 1: Break down the requirements by screen size
Extra small (xs) and small (sm): hidden
Medium (md) and large (lg): visible as block
Extra large (xl): visible as inlineStep 2: Match classes to requirements
d-nonehides on all by default (xs and sm included).d-md-blockshows block from medium and up (md, lg, xl).d-xl-inlineoverrides to inline only on extra large screens.Step 3: Verify other options
Other options either hide wrong breakpoints or use incorrect display types at breakpoints.Final Answer:
d-none d-md-block d-xl-inline -> Option BQuick Check:
Hide all, show block md+, inline xl = d-none d-md-block d-xl-inline [OK]
- Using d-sm-none hides only small, not extra small
- Mixing inline and block classes at wrong breakpoints
- Forgetting to hide on extra small screens
Ready to prove it?
Pass the test and this pattern moves unknown
