Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 58 ยท Utilities

Helpers: Stretched Link, Vertical Rule, Icon Link and More

Learn Bootstrap 5.3 helpers: stretched-link for clickable cards, vr dividers, icon-link, focus-ring, colored links and text-bg, plus ratio and clearfix.

Utilities such as mt-3 or d-flex each change one CSS property. Helpers are slightly bigger: a handful of classes that package a small, reusable behavior, often a few CSS rules working together, so you do not have to write them yourself. You have already met some of them (ratio in Sizing Utilities, clearfix in Images and Figures, visually-hidden in Accessibility in Bootstrap).

This lesson gathers the full set and goes deep on the helpers that have not had their own spotlight: stretched-link, the vertical rule vr, icon-link, focus-ring, the colored link helpers and text-bg-*. At the end there is a short reference for the ones you already know. All examples load Bootstrap 5.3.3 from the CDN.

A common pattern: a card with a title, a picture and some text where the entire card should open the article. Wrapping everything in an <a> creates messy markup, and screen readers read the whole card as the link text. The stretched-link class does it properly.

It adds an invisible ::after pseudo-element to the link that covers its nearest positioned ancestor (an element with position: relative, absolute, fixed or sticky). The link keeps its small, descriptive text, but a click anywhere on the card activates it.

A fully clickable card

Rules to remember:

  • A card, list group item or position-relative wrapper must be the containing block. If the stretched area covers the whole page, you forgot the positioned parent.
  • Only one stretched link per container. Two would overlap and the later one wins.
  • Other interactive elements inside the stretched area, such as buttons or secondary links, sit under the invisible overlay and become unclickable. Give them position-relative (and a z-index if needed) so they rise above it:
HTML
<a href="/post" class="stretched-link">Read more</a>
<a href="/tag/css" class="position-relative">#css</a>   <!-- still clickable -->
  • Keep the link text meaningful (the post title is better than "click here"), because assistive technology announces that text for the whole card.

vr: the vertical rule

<hr> draws a horizontal line. The vr helper draws its vertical cousin: a 1 px wide line that stretches to the height of its container. It shines inside flex rows, between toolbar groups, breadcrumbs and stats.

HTML
<div class="vr"></div>

It is an inline-block with min-height: 1em, align-self: stretch behavior in flex containers, and background-color: currentcolor at 25% opacity. In a flex container it fills the available height. Elsewhere it needs a height, set with a utility such as style="height: 2rem" or the h-* classes.

Dividers in a toolbar and a stats row

The last divider is red because vr uses currentcolor: setting a text color class such as text-danger changes it. Dividers between flex children replace border-end utilities and do not add width to the items.

Bootstrap 5.3 added icon-link, an inline-flex link that aligns an icon (an inline SVG or an icon-font glyph) with its text, with a consistent gap. A companion icon-link-hover class nudges the icon sideways on hover.

HTML
<a class="icon-link icon-link-hover" href="#">
  Read the docs
  <svg class="bi" aria-hidden="true" width="16" height="16"><use xlink:href="#arrow-right"/></svg>
</a>

You can set the gap and the hover shift with CSS variables: --bs-icon-link-gap and --bs-icon-link-transform. Combine it with the link color helpers described below. Add aria-hidden="true" to purely decorative icons so screen readers do not announce them.

Icon links

focus-ring: a visible keyboard focus

Keyboard users rely on the focus outline to see where they are. Bootstrap's focus-ring helper replaces the browser default with a soft ring that you can recolor. It appears on :focus-visible (keyboard focus, not mouse clicks).

Apply it to any focusable element and tweak the look with CSS variables or color classes:

Class or variable Effect
focus-ring Turns on the ring for :focus-visible
focus-ring-primary, -success, -danger... Ring color
--bs-focus-ring-width Thickness (default .25rem)
--bs-focus-ring-opacity Opacity
--bs-focus-ring-color Any custom color
Press Tab to see the rings

Never remove focus indicators without replacing them. See Accessibility in Bootstrap.

The text-* classes color text, but a link has hover and focus states. The link-* helpers (link-primary, link-secondary, link-success, link-danger, link-warning, link-info, link-light, link-dark, link-body-emphasis) color links and darken them on hover, so a normal text color class cannot mimic their hover behavior.

Version 5.3 adds fine control of the underline and its opacity: link-underline-* for the color, link-offset-* for the distance (1, 2 or 3), link-underline-opacity-* and link-opacity-* (with -hover variants) and link-underline-opacity-0 to hide the underline until hover.

HTML
<a class="link-primary" href="#">Primary link</a>
<a class="link-danger link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover" href="#">Subtle underline</a>
<a class="link-body-emphasis link-opacity-50-hover" href="#">Fades on hover</a>

Use them for colored links in the body copy, and use text-decoration-none only where the link is obviously interactive from context (such as in navigation).

text-bg-*: background and readable text together

text-bg-primary sets a background color and automatically picks a contrasting text color for it (white or black), via the color-contrast() Sass function. It is the quickest way to make badges and callouts that are readable without choosing a text color manually.

HTML
<span class="badge text-bg-warning">Beta</span>
<div class="p-3 text-bg-dark rounded">Dark box, light text</div>

Use it instead of pairing bg-* with text-white or text-dark by hand.

Stacks: hstack and vstack

hstack and vstack are tiny flexbox helpers. vstack lays children out in a vertical column, hstack in a horizontal row with the items centered vertically. Both come with no gap by default, so you add one with the gap utilities (gap-3). They are shorthand for d-flex flex-column and d-flex align-items-center, with a useful trick for pushing items apart: an ms-auto or me-auto margin takes all the free space.

hstack, vstack and auto margins

me-auto on the Blog link pushes the two buttons to the right edge, the same trick as with d-flex. Stacks are for quick one-dimensional layouts; for anything with rows and columns, use the grid.

Clearfix and ratio, in a little more detail

clearfix fixes the old problem of floats: a container whose children are all floated collapses to zero height, so the content that follows slides up beside them. Add clearfix to the parent and it contains the floats again. You will meet it in older code that uses float-start and float-end for images beside text; modern layouts use flexbox and rarely need it.

ratio keeps an element at a fixed proportion while the width changes. Wrap an iframe, video or any child in ratio ratio-16x9 (or 1x1, 4x3, 21x9), and Bootstrap makes the child fill the box. For a custom shape, set the CSS variable on the wrapper: style="--bs-aspect-ratio: 50%;" is a 2:1 box (the percentage is height divided by width).

Responsive floats, sticky headers and the ratio map

Three helpers have breakpoint versions or Sass settings that are easy to miss.

Responsive floats. float-start, float-end and float-none all take a breakpoint infix, in the same style as the other responsive classes: float-md-end, float-lg-none, and so on. The rule applies from that breakpoint upward. A common pattern is an image that sits on its own line on a phone, and floats beside the text on larger screens: float-md-end ms-md-3 mb-3. When floated children could collapse their parent, add clearfix to the parent (see above).

Responsive sticky. sticky-top pins an element to the top of the viewport while you scroll its container. sticky-bottom does the same at the bottom. Both have breakpoint versions, sticky-sm-top through sticky-xxl-top and the matching sticky-*-bottom, which only stick from that width upward. That is handy for a sidebar: on a phone it flows with the page, and from md it stays in view. A sticky element needs a scrolling ancestor that is taller than it, and none of its ancestors may have overflow: hidden, or it will not stick.

float-md-end and sticky-md-top

Resize the preview or open the page on a phone to compare the two states. At 768px and wider the line reports float: right and position: sticky; below that it reports float: none and position: static.

The ratio map. The built-in ratio-1x1, ratio-4x3, ratio-16x9 and ratio-21x9 classes are generated from a Sass map. When you compile Bootstrap yourself, add your own with map-merge and they will exist as classes, with no hand-written CSS:

SCSS
$aspect-ratios: (
  "1x1": 100%,
  "4x3": calc(3 / 4 * 100%),
  "16x9": calc(9 / 16 * 100%),
  "21x9": calc(9 / 21 * 100%),
  "2x1": 50%,
  "3x2": calc(2 / 3 * 100%)
);

The value is the height as a percentage of the width, which is why 16:9 is 9 / 16 * 100%. Without a build step, use the runtime route from above: style="--bs-aspect-ratio: 50%;" on a ratio wrapper.

Helpers you already know

Helper What it does Where to read more
clearfix Makes a container wrap its floated children Images and Figures
ratio ratio-16x9 Keeps embeds in a fixed aspect ratio Sizing Utilities
fixed-top, fixed-bottom, sticky-top, sticky-bottom Pin elements to the viewport or while scrolling Display and Visibility
visually-hidden, visually-hidden-focusable Hide content visually but keep it for assistive tech; show it on focus (skip links) Accessibility in Bootstrap
text-truncate One line with an ellipsis Text Utilities
position-*, top-*, translate-middle Offset and center positioned elements (badges on buttons) Display and Visibility

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does stretched-link stretch over?

    stretched-link adds an overlay with ::after that covers the nearest ancestor with a position other than static, making the whole container clickable.
  2. 2. A card has a stretched link plus a separate tag link, and the tag link no longer responds to clicks. What is the fix?

    The overlay covers other content. Giving the secondary link a position raises it above the overlay so it receives clicks.
  3. 3. Why can a vr element take its color from text-danger?

    The helper uses currentcolor at partial opacity, so text color classes change it.
  4. 4. When does the focus-ring helper show its ring?

    focus-ring is tied to :focus-visible so mouse users do not see rings on clicks, while keyboard users do.
  5. 5. What is the advantage of text-bg-primary over bg-primary text-white?

    text-bg-* uses a Sass contrast function to choose white or dark text that is readable on the chosen background.

Summary

  • Helpers package small behaviors: stretched-link, vr, icon-link, focus-ring, link-*, text-bg-*, clearfix, ratio, position helpers and visually-hidden.
  • stretched-link turns a positioned container (like a card) into one big click target; lift secondary links with position-relative.
  • vr is a vertical divider that follows currentcolor and fills the height of a flex row.
  • icon-link aligns an icon with link text; focus-ring gives keyboard users a clear, recolorable indicator.
  • text-bg-* pairs a background with readable text automatically.

Next lesson: Borders, Radius and Shadows, where you add edges and depth to elements.