Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 58 ยท Utilities

Display and Visibility

Learn Bootstrap 5.3 display utilities: d-* classes, showing and hiding by breakpoint, print display, visibility, overflow, z-index and position helpers.

Responsive design often comes down to two questions: what shows at this screen size, and how are things stacked? Bootstrap answers the first with the d-* display utilities and the second with position-*, z-* and overflow-* classes. They map almost one to one onto CSS properties, so what you learn here also deepens your understanding of plain CSS.

In this lesson you will change an element's display, show and hide content at different breakpoints, hide content for print, tell visible apart from d-none, control overflow and stacking, and use the position helpers.

The d-* classes

The d- classes set the CSS display value. The most useful ones:

Class CSS
d-none display: none
d-block display: block
d-inline display: inline
d-inline-block display: inline-block
d-flex / d-inline-flex display: flex / inline-flex
d-grid display: grid
d-table, d-table-row, d-table-cell table display types

Each can carry a breakpoint: d-md-flex applies display: flex from 768 px up. Bootstrap marks these rules !important, so they win over component styles. That is convenient, but it also means you cannot override them later without !important yourself.

Changing display

Showing and hiding by breakpoint

Hiding is just d-none. To hide something only at some sizes, combine d-none with a d-{breakpoint}-* class that brings it back. Remember that the classes are mobile-first min-width rules, so think "hidden from the smallest size up to X".

  • Hide on xs only (show from sm up): d-none d-sm-block
  • Show on xs only: d-block d-sm-none
  • Show only on md: d-none d-md-block d-lg-none
  • Hide only on lg and up: d-lg-none
flowchart LR
  A["xs<br/>under 576"] --> B["sm<br/>576"] --> C["md<br/>768"] --> D["lg<br/>992"] --> E["xl<br/>1200"] --> F["xxl<br/>1400"]

Each breakpoint class applies from its width until another class replaces it.

A reference chart for the typical case, "only visible on one size", helps you avoid mistakes:

To show only on Classes
xs d-block d-sm-none
sm d-none d-sm-block d-md-none
md d-none d-md-block d-lg-none
lg d-none d-lg-block d-xl-none
xl d-none d-xl-block d-xxl-none
xxl d-none d-xxl-block
One message per breakpoint

Resize your window and exactly one message appears at a time.

Hidden content is still downloaded. d-none only hides an element; its images and markup are still sent to the browser. Do not use it to switch between two heavy versions of the same content. Reserve it for small things such as a short label versus a long one, and use <picture> or srcset for images (Images and Figures).

Printing

Print versions get their own set: d-print-none hides an element when printing, and d-print-block, d-print-inline and the rest force a display type when printing. Typical use: hide navigation and buttons, show a "page printed from..." note.

HTML
<nav class="d-print-none">...site menu...</nav>
<p class="d-none d-print-block">Printed from example.com</p>

You can try this on your own pages with the browser's print preview. The embedded editor cannot open a print dialog, so this example is read-only.

Visibility: invisible versus d-none

d-none removes the element from the layout, so neighbors move into its place. .invisible keeps the space but makes the element transparent and unreachable, and .visible forces it to show. The difference matters when you want a layout to stay put:

d-none versus invisible

An invisible element is also removed from the accessibility tree and cannot be focused, just like d-none. If you want content that is hidden visually but still read by screen readers, use visually-hidden (and visually-hidden-focusable for skip links). See Accessibility in Bootstrap.

Overflow

Overflow classes control what happens when content is bigger than its box: overflow-auto (scrollbars when needed), overflow-hidden (clip), overflow-visible and overflow-scroll (always show scrollbars). Bootstrap 5.3 adds per-axis versions: overflow-x-auto, overflow-y-hidden and so on. Give the box a height first, or there is nothing to overflow.

Position and z-index

The position helpers set position and offsets:

  • position-static, position-relative, position-absolute, position-fixed, position-sticky.
  • Offsets top-0, top-50, top-100, bottom-*, start-*, end-* (values 0, 50 and 100 percent).
  • translate-middle, translate-middle-x, translate-middle-y shift the element back by half its own size, which centers it on an offset.
  • Shortcuts: fixed-top, fixed-bottom, sticky-top, sticky-bottom.

The stacking order of positioned elements uses z-n1, z-0, z-1, z-2 and z-3 (5.3). Higher numbers sit on top, and z-n1 goes behind the siblings.

Badge on a corner, and stacking

The badge on the button works because the button is position-relative, so the absolutely positioned badge measures from the button's corner. top-0 start-100 puts the badge's top-left corner at the button's top-right corner, and translate-middle pulls it back by half its own size. Notice the visually-hidden text, which tells screen reader users what the number means.

Positioned children need a positioned ancestor to anchor to. If an absolute badge flies to the corner of the page, you forgot position-relative on its parent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which classes show an element only on medium screens (768 to 991 px)?

    Hide it by default, show it from md, and hide it again from lg.
  2. 2. What is the difference between d-none and invisible?

    d-none sets display: none, so neighbors close the gap. invisible only sets visibility: hidden, which keeps the box.
  3. 3. What is d-print-none for?

    d-print-none applies display: none in print media, which is handy for navigation and buttons.
  4. 4. An absolutely positioned badge appears at the corner of the whole page instead of its button. What is the likely fix?

    An absolute element measures from the nearest positioned ancestor. Without one it uses the page.

Summary

  • d-* classes set display; add a breakpoint (d-md-flex) to apply them from that width up.
  • Show only at one size by combining d-none with one d-{bp}-block and the next d-{bp}-none.
  • Hidden elements are still downloaded; use d-print-none / d-print-block to tune printing.
  • invisible keeps the space, d-none does not, and visually-hidden stays available to screen readers.
  • overflow-*, position-*, offset classes, translate-middle and z-* give you CSS positioning without custom CSS.

Next lesson: Flex Utilities.