Skip to content

Commit 65e9979

Browse files
committed
Add container queries to bootstrap
This change is designed to be backward compatible with the existing media query implementation, while also enabling support for container queries.
1 parent c985f82 commit 65e9979

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

scss/mixins/_breakpoints.scss

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
// Breakpoint viewport sizes and media queries.
1+
// Breakpoint viewport sizes and container queries.
22
//
33
// Breakpoints are defined as a map of (name: minimum width), order from small to large:
44
//
55
// (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px, xxl: 1400px)
66
//
77
// The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.
88

9+
// Set the document root element as the default container to maximize the available viewport width.
10+
// This should allow in most cases to keep backwards compatibility with the previous Media Query implementation.
11+
html {
12+
container-type: inline-size;
13+
}
14+
15+
// Applying this class to an element makes Bootstrap's responsive utilities reference the container's size rather than the viewport's.
16+
.breakpoint-container {
17+
container-type: inline-size;
18+
}
19+
920
// Name of the next breakpoint, or null for the last breakpoint.
1021
//
1122
// >> breakpoint-next(sm)
@@ -56,72 +67,97 @@
5667
@return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
5768
}
5869

59-
// Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
70+
// Container query of at least the minimum breakpoint width. No query for the smallest breakpoint.
6071
// Makes the @content apply to the given breakpoint and wider.
61-
@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
72+
@mixin container-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
6273
$min: breakpoint-min($name, $breakpoints);
6374
@if $min {
64-
@media (min-width: $min) {
75+
@container (min-width: #{$min}) {
6576
@content;
6677
}
6778
} @else {
6879
@content;
6980
}
7081
}
7182

72-
// Media of at most the maximum breakpoint width. No query for the largest breakpoint.
83+
// Container query of at most the maximum breakpoint width. No query for the largest breakpoint.
7384
// Makes the @content apply to the given breakpoint and narrower.
74-
@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
85+
@mixin container-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
7586
$max: breakpoint-max($name, $breakpoints);
7687
@if $max {
77-
@media (max-width: $max) {
88+
@container (max-width: #{$max}) {
7889
@content;
7990
}
8091
} @else {
8192
@content;
8293
}
8394
}
8495

85-
// Media that spans multiple breakpoint widths.
96+
// Container query that spans multiple breakpoint widths.
8697
// Makes the @content apply between the min and max breakpoints
87-
@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
98+
@mixin container-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
8899
$min: breakpoint-min($lower, $breakpoints);
89100
$max: breakpoint-max($upper, $breakpoints);
90101

91102
@if $min != null and $max != null {
92-
@media (min-width: $min) and (max-width: $max) {
103+
@container (min-width: #{$min}) and (max-width: #{$max}) {
93104
@content;
94105
}
95106
} @else if $max == null {
96-
@include media-breakpoint-up($lower, $breakpoints) {
107+
@include container-breakpoint-up($lower, $breakpoints) {
97108
@content;
98109
}
99110
} @else if $min == null {
100-
@include media-breakpoint-down($upper, $breakpoints) {
111+
@include container-breakpoint-down($upper, $breakpoints) {
101112
@content;
102113
}
103114
}
104115
}
105116

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

114125
@if $min != null and $max != null {
115-
@media (min-width: $min) and (max-width: $max) {
126+
@container (min-width: #{$min}) and (max-width: #{$max}) {
116127
@content;
117128
}
118129
} @else if $max == null {
119-
@include media-breakpoint-up($name, $breakpoints) {
130+
@include container-breakpoint-up($name, $breakpoints) {
120131
@content;
121132
}
122133
} @else if $min == null {
123-
@include media-breakpoint-down($next, $breakpoints) {
134+
@include container-breakpoint-down($next, $breakpoints) {
124135
@content;
125136
}
126137
}
127138
}
139+
140+
// Aliases for the previous mixin, for backwards compatibility.
141+
@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
142+
@include container-breakpoint-up($name, $breakpoints) {
143+
@content;
144+
}
145+
}
146+
147+
@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
148+
@include container-breakpoint-down($name, $breakpoints) {
149+
@content;
150+
}
151+
}
152+
153+
@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
154+
@include container-breakpoint-between($lower, $upper, $breakpoints) {
155+
@content;
156+
}
157+
}
158+
159+
@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
160+
@include container-breakpoint-only($name, $breakpoints) {
161+
@content;
162+
}
163+
}

0 commit comments

Comments
 (0)