Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 58 ยท Utilities

Interactions, Z-index and Object Fit Utilities

Control selection, clicks and stacking with Bootstrap 5.3 utilities: user-select, pe-none, z-index layers and object-fit, with live examples.

Some utilities do not change how an element looks at rest. They change how it behaves: whether text can be selected, whether a click reaches it, which element sits on top when two overlap, and how a picture fills its box. Bootstrap 5.3 wraps these CSS properties in short, responsive classes so you can adjust them straight in the HTML.

This lesson covers four groups: interaction utilities (user-select-*, pe-*), z-index utilities (z-*), object-fit utilities (object-fit-*) and the closely related overflow and opacity helpers. Every demo loads Bootstrap 5.3.3 from the CDN. For link-specific utilities such as link-opacity-* and link-underline-*, see Helpers.

Text selection: user-select-*

Normally users can highlight any text. Sometimes that is exactly what you do not want (a clickable label that highlights on double-click), and sometimes you want the opposite (a code that should select with one click).

Class CSS Use for
user-select-all user-select: all One click selects the whole element: coupons, tokens, share links
user-select-auto user-select: auto Browser default
user-select-none user-select: none Buttons, tabs, drag handles, decorative labels
Select all in one click

Use user-select-none on interface chrome only. Never put it on article text or error messages that someone may need to copy, and remember that it does not protect content from being copied: anyone can still read the page source.

Pointer events: pe-none and pe-auto

Class CSS Effect
pe-none pointer-events: none Clicks, hovers and taps pass through to whatever lies beneath
pe-auto pointer-events: auto Restores normal behavior (useful inside a pe-none parent)

Typical uses are decorative overlays that must not block the content below, and "pass-through" containers whose children should still respond.

An overlay that lets clicks through

The left overlay swallows every click, so the counter never moves. The right overlay has pe-none, so the click goes through to the button.

pe-none does not disable keyboard access. A link or button with pe-none can still be focused with Tab and activated with Enter. To disable a control properly, use the disabled attribute on buttons and form fields. On a link, Bootstrap's .disabled class combines pe-none with aria-disabled="true" and tabindex="-1".

Stacking order: z-*

When two positioned elements overlap, the one with the higher z-index is drawn on top. Bootstrap offers a small scale so you do not invent numbers such as 9999:

Class z-index
z-n1 -1 (behind siblings)
z-0 0
z-1 1
z-2 2
z-3 3

These are small utilities for ordering items within one component, such as stacking cards or layered decoration. Bootstrap's own components use a separate, much higher scale (dropdowns 1000, sticky 1020, fixed 1030, offcanvas 1045, modal 1055, popover 1070, tooltip 1080, toasts 1090), so a z-3 element will never cover a modal.

An important rule: z-index only works on positioned elements (any position except static) and on children of flex or grid containers. Add position-relative to the element first.

Layering cards

Each coloured box overlaps the next, and the numbers decide who wins. Remember stacking contexts: an element with z-index creates a context for its children. A child with z-3 inside a parent with z-0 can never rise above a sibling of that parent with z-1. If a layer refuses to move, look for a parent that creates a context (position with z-index, opacity below 1, transform, and others).

Fitting images: object-fit-*

Images (and videos) keep their natural shape unless you give them a fixed box, and then they stretch. object-fit decides how the content fits into the box, and Bootstrap provides classes for it:

Class Behavior
object-fit-contain Fit entirely inside the box, keeping the shape, leaving empty bands
object-fit-cover Fill the whole box, keeping the shape and cropping the overflow
object-fit-fill Stretch to the box (the default, distorts)
object-fit-scale Whichever of none or contain is smaller
object-fit-none Natural size, cropped by the box

Each also has responsive forms such as object-fit-sm-cover and object-fit-lg-contain. Combine them with fixed heights and the sizing helpers from Sizing Utilities.

Five ways to fit one picture

cover is the everyday choice for photo grids: every thumbnail is the same size and never distorted, at the cost of cropping. contain is right when nothing may be cut off, such as logos. Avoid fill unless you want stretching.

To choose which part stays visible when cropping, there is no Bootstrap class for object-position, so use an inline style or your own CSS: style="object-position: top" keeps the top of a portrait photo (usually faces).

Overflow and opacity

Two more behavior-related groups round out the set.

Overflow controls what happens when content is bigger than its box:

Class Effect
overflow-auto Scrollbars appear when needed
overflow-hidden Clips anything that sticks out (also contains floats and rounds clip with rounded)
overflow-visible Lets content spill (default)
overflow-scroll Always shows scrollbars
overflow-x-*, overflow-y-* Per axis

Give scrolling regions a fixed height and tabindex="0" so keyboard users can scroll them.

Opacity sets transparency with opacity-0, opacity-25, opacity-50, opacity-75 and opacity-100. opacity-0 hides an element visually but leaves it in the layout and in the accessibility tree, so it is not a substitute for d-none or visually-hidden (see Display and Visibility).

HTML
<div class="overflow-auto border rounded p-2" style="height: 6rem;" tabindex="0">
  <p>Long content that scrolls inside this box...</p>
</div>

Putting it together: a safe image card with a decorative overlay

A pattern that uses most of the lesson: a thumbnail that fills its frame with object-fit-cover, a decorative gradient layer with pe-none and a low z-* so it sits above the picture but below the caption, and a caption that stays selectable.

HTML
<div class="position-relative rounded overflow-hidden" style="width: 16rem; height: 10rem;">
  <img class="w-100 h-100 object-fit-cover" src="..." alt="Harbor at dusk">
  <div class="position-absolute bottom-0 start-0 w-100 h-50 pe-none z-1"
       style="background: linear-gradient(transparent, rgba(0,0,0,.65));"></div>
  <p class="position-absolute bottom-0 start-0 m-2 mb-1 text-white z-2">Harbor at dusk</p>
</div>

The overlay never blocks clicks on the image, the caption sits on top of it, and overflow-hidden with rounded clips everything to the rounded corners.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens when you click an element covered by an overlay with pe-none?

    pointer-events none makes the overlay invisible to the pointer, so clicks reach the content beneath it.
  2. 2. Which statement about pe-none is correct?

    Keyboard access is unaffected. Use the disabled attribute (or aria-disabled with tabindex -1 on links) to truly disable something.
  3. 3. Why might z-3 not lift an element above its neighbor?

    z-index needs a positioned element (or a flex or grid child), and children cannot escape their parent's stacking context.
  4. 4. Which object-fit class gives equal-size photo thumbnails without distortion, cropping the overflow?

    cover scales the image to fill the whole box while keeping its proportions, cutting what does not fit.
  5. 5. Why is user-select-all useful?

    user-select: all makes a single click select the entire contents of the element.

Summary

  • user-select-all|auto|none controls highlighting: use none on UI chrome only, and all for things people copy.
  • pe-none lets pointer input pass through (decorative overlays); pe-auto restores it; neither changes keyboard access.
  • z-n1 to z-3 order positioned items inside a component; Bootstrap's own components use a separate, higher scale.
  • object-fit-cover, contain, fill, scale and none decide how images fill fixed boxes; object-position needs your own CSS.
  • overflow-* and opacity-* complete the behavior toolbox, but opacity-0 is not a way to hide content for everyone.

Next lesson: The Utility API, where you create your own utilities.