Bootstrap Container Queries

Version 4 (Bootstrap 6) · Version 3 (Bootstrap 5)

What is it?

Bootstrap's responsive classes react to the viewport. This library adds classes that react to the width of a parent container instead, using CSS container queries. A component behaves the same whether it is in a sidebar or in a full-width page.

Install

npm i bootstrap@next bootstrap-container-queries@next

With CSS, load the file after Bootstrap 6 (it uses Bootstrap's cascade layers):

<link href="bootstrap.min.css" rel="stylesheet" />
<link href="bootstrap-container-queries.min.css" rel="stylesheet" />

With Sass (Dart Sass 1.103+, @import is not supported):

@use "bootstrap-container-queries/scss/bootstrap-container-queries";

Usage

Add Bootstrap's .contains-inline to the parent (or use set-container() in Sass), then use classes prefixed with cq-breakpoint::

<div class="contains-inline">
  <div class="d-none cq-md:d-block">Visible when the container is at least md</div>
</div>

<div class="contains-inline row">
  <div class="cq-sm:col-6 cq-lg:col-3">...</div>
</div>

Without prefix the class is plain Bootstrap and applies at every width. The container must have a width of its own: it cannot size itself from its content.

Reference

Breakpoints: sm 576px, md 768px, lg 1024px, xl 1280px, 2xl 1536px (the Bootstrap $breakpoints).

Feature Classes Example
Grid cq-{bp}:col-{1-12} cq-md:col-4
Display cq-{bp}:d-{value} cq-lg:d-none
Flex direction cq-{bp}:flex-{value} cq-lg:flex-column-reverse
Text align cq-{bp}:text-{start|center|end} cq-md:text-end
Float cq-{bp}:float-{start|end} cq-sm:float-end
Margin, padding cq-{bp}:m{sides}-{n}, cq-{bp}:p{sides}-{n} cq-xl:mt-5, cq-md:px-3

Utilities are generated from Bootstrap's own $utilities, so values (and spacing scale) match Bootstrap 6.

Customize

@use "bootstrap-container-queries/scss/bootstrap-container-queries" with (
  $container-queries-breakpoints: (xs: 0, sm: 30em, md: 48em),
  $container-queries-utilities: ("display", "gap")
);

Good to know

Live demo

Resize the box below (drag its corner) or use the buttons.

cq-sm:col-6 cq-md:col-4 cq-lg:col-3
cq-sm:col-6 cq-md:col-4 cq-lg:col-3
cq-sm:col-6 cq-md:col-4 cq-lg:col-3
hide on sm and wider screens
hide on screens smaller than sm
hide on md and wider screens
hide on screens smaller than md
hide on lg and wider screens
hide on screens smaller than lg
text start < sm < text end
text start < md < text end
text start < lg < text end
float start < sm < float end
float start < md < float end
float start < lg < float end
flex direction column < lg < flex direction column-reverse