Skip to content

Feat: Add container queries to bootstrap #41612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions scss/mixins/_breakpoints.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// Breakpoint viewport sizes and media queries.
// Breakpoint viewport sizes and container queries.
//
// Breakpoints are defined as a map of (name: minimum width), order from small to large:
//
// (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px, xxl: 1400px)
//
// The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.

// Set the document root element as the default container to maximize the available viewport width.
// This should allow in most cases to keep backwards compatibility with the previous Media Query implementation.
html {
container-type: inline-size;
}

// Applying this class to an element makes Bootstrap's responsive utilities reference the container's size rather than the viewport's.
.breakpoint-container {
container-type: inline-size;
}

// Name of the next breakpoint, or null for the last breakpoint.
//
// >> breakpoint-next(sm)
Expand Down Expand Up @@ -56,72 +67,97 @@
@return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
}

// Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
// Container query of at least the minimum breakpoint width. No query for the smallest breakpoint.
// Makes the @content apply to the given breakpoint and wider.
@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
@mixin container-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
$min: breakpoint-min($name, $breakpoints);
@if $min {
@media (min-width: $min) {
@container (min-width: #{$min}) {
@content;
}
} @else {
@content;
}
}

// Media of at most the maximum breakpoint width. No query for the largest breakpoint.
// Container query of at most the maximum breakpoint width. No query for the largest breakpoint.
// Makes the @content apply to the given breakpoint and narrower.
@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
@mixin container-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
$max: breakpoint-max($name, $breakpoints);
@if $max {
@media (max-width: $max) {
@container (max-width: #{$max}) {
@content;
}
} @else {
@content;
}
}

// Media that spans multiple breakpoint widths.
// Container query that spans multiple breakpoint widths.
// Makes the @content apply between the min and max breakpoints
@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
@mixin container-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
$min: breakpoint-min($lower, $breakpoints);
$max: breakpoint-max($upper, $breakpoints);

@if $min != null and $max != null {
@media (min-width: $min) and (max-width: $max) {
@container (min-width: #{$min}) and (max-width: #{$max}) {
@content;
}
} @else if $max == null {
@include media-breakpoint-up($lower, $breakpoints) {
@include container-breakpoint-up($lower, $breakpoints) {
@content;
}
} @else if $min == null {
@include media-breakpoint-down($upper, $breakpoints) {
@include container-breakpoint-down($upper, $breakpoints) {
@content;
}
}
}

// Media between the breakpoint's minimum and maximum widths.
// Container query between the breakpoint's minimum and maximum widths.
// No minimum for the smallest breakpoint, and no maximum for the largest one.
// Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
@mixin container-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
$min: breakpoint-min($name, $breakpoints);
$next: breakpoint-next($name, $breakpoints);
$max: breakpoint-max($next, $breakpoints);

@if $min != null and $max != null {
@media (min-width: $min) and (max-width: $max) {
@container (min-width: #{$min}) and (max-width: #{$max}) {
@content;
}
} @else if $max == null {
@include media-breakpoint-up($name, $breakpoints) {
@include container-breakpoint-up($name, $breakpoints) {
@content;
}
} @else if $min == null {
@include media-breakpoint-down($next, $breakpoints) {
@include container-breakpoint-down($next, $breakpoints) {
@content;
}
}
}

// Aliases for the previous mixin, for backwards compatibility.
@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
@include container-breakpoint-up($name, $breakpoints) {
@content;
}
}

@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
@include container-breakpoint-down($name, $breakpoints) {
@content;
}
}

@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
@include container-breakpoint-between($lower, $upper, $breakpoints) {
@content;
}
}

@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
@include container-breakpoint-only($name, $breakpoints) {
@content;
}
}