Skip to content

Commit 634344c

Browse files
authored
v4/v5: Add responsive containers (twbs#29095)
* create responsive containers provide more flexibility and allow the user to determine when containers switch from fluid to fixed width. * fix the base container code this commit fixes the non-media portion of the generated CSS. I learned about the `@extends` directive and was able to put it to good use. I create a new temporary map that contains all the main `$container-max-widths` and join it to our 2 special cases of 'xs' and 'fluid'. Then we loop through that and, with the appropriate infixes, extend our placeholder * formatting for style forgot to run my tests before the last push, i think these are better. * finish incomplete comment * fix the responsive containers using the `@extend` directive I was able to clean up this code * fix responsive containers in the navbar mostly we just look through all of our breakpoints so we can include all of the responsive container classes in the tweaks we have to do for the navbar (redeclaring flex properties, don't double up on padding, etc) * Simplify container extends * Simplify navbar containers * Rearrange, add comments, ensure everything is nested in $enable-grid-classes * Reduce new CSS by using attribute selector We avoid using `@extend` whenever possible, and this is more readable * Update _grid.scss * Update _navbar.scss * Add docs for responsive containers, redesign the container layout page * Add to the Grid example
1 parent 57154a6 commit 634344c

6 files changed

Lines changed: 77 additions & 54 deletions

File tree

scss/_grid.scss

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@
33
// Set the container width, and override it for fixed navbars in media queries.
44

55
@if $enable-grid-classes {
6+
// Single container class with breakpoint max-widths
67
.container {
78
@include make-container();
89
@include make-container-max-widths();
910
}
10-
}
11-
12-
// Fluid container
13-
//
14-
// Utilizes the mixin meant for fixed width containers, but with 100% width for
15-
// fluid, full width layouts.
1611

17-
@if $enable-grid-classes {
12+
// 100% wide container at all breakpoints
1813
.container-fluid {
1914
@include make-container();
2015
}
16+
17+
// Responsive containers that are 100% wide until a breakpoint
18+
@each $breakpoint, $container-max-width in $container-max-widths {
19+
.container-#{$breakpoint} {
20+
@include make-container();
21+
}
22+
23+
@include media-breakpoint-up($breakpoint, $grid-breakpoints) {
24+
.container-#{$breakpoint} {
25+
max-width: $container-max-width;
26+
}
27+
}
28+
}
2129
}
2230

31+
2332
// Row
2433
//
2534
// Rows contain and clear the floats of your columns.

scss/_navbar.scss

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
// Because flex properties aren't inherited, we need to redeclare these first
2727
// few properties so that content nested within behave properly.
28-
> .container,
29-
> .container-fluid {
28+
> [class^="container"] {
3029
display: flex;
3130
flex-wrap: wrap;
3231
align-items: center;
@@ -140,8 +139,7 @@
140139

141140
&#{$infix} {
142141
@include media-breakpoint-down($breakpoint) {
143-
> .container,
144-
> .container-fluid {
142+
> [class^=".container"] {
145143
padding-right: 0;
146144
padding-left: 0;
147145
}
@@ -165,8 +163,7 @@
165163
}
166164

167165
// For nesting containers, have to redeclare for alignment purposes
168-
> .container,
169-
> .container-fluid {
166+
> [class^=".container"] {
170167
flex-wrap: nowrap;
171168
}
172169

site/content/docs/4.3/examples/grid/grid.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44
background-color: rgba(86, 61, 124, .15);
55
border: 1px solid rgba(86, 61, 124, .2);
66
}
7+
8+
.themed-container {
9+
padding: 15px;
10+
margin-bottom: 30px;
11+
background-color: rgba(0, 123, 255, .15);
12+
border: 1px solid rgba(0, 123, 255, .2);
13+
}

site/content/docs/4.3/examples/grid/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,13 @@ <h2 class="mt-4">Mixed: mobile, tablet, and desktop</h2>
125125
</div>
126126

127127
</div>
128+
129+
<div class="container">
130+
<h2 class="mt-4">Responsive containers</h2>
131+
<p>Additional classes added in Bootstrap v4.4 allow containers that are 100% wide until a particular breakpoint.</p>
132+
</div>
133+
134+
<div class="container-sm themed-container">.container-sm</div>
135+
<div class="container-md themed-container">.container-md</div>
136+
<div class="container-lg themed-container">.container-lg</div>
137+
<div class="container-xl themed-container">.container-xl</div>

site/content/docs/4.3/layout/overview.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ toc: true
99

1010
## Containers
1111

12-
Containers are the most basic layout element in Bootstrap and are **required when using our default grid system**. Choose from a responsive, fixed-width container (meaning its `max-width` changes at each breakpoint) or fluid-width (meaning it's `100%` wide all the time).
12+
Containers are the most basic layout element in Bootstrap and are **required when using our default grid system**. While containers *can* be nested, most layouts do not require a nested container.
1313

14-
While containers *can* be nested, most layouts do not require a nested container.
14+
### All-in-one
15+
16+
Our default `.container` class is a responsive, fixed-width container, meaning its `max-width` changes at each breakpoint.
1517

1618
<div class="bd-example">
17-
<div class="bd-example-container">
18-
<div class="bd-example-container-header"></div>
19-
<div class="bd-example-container-sidebar"></div>
20-
<div class="bd-example-container-body"></div>
19+
<div class="example-container-element col-6 p-3 mx-auto">
20+
.container
2121
</div>
2222
</div>
2323

@@ -27,13 +27,13 @@ While containers *can* be nested, most layouts do not require a nested container
2727
</div>
2828
{{< /highlight >}}
2929

30+
### Fluid
31+
3032
Use `.container-fluid` for a full width container, spanning the entire width of the viewport.
3133

3234
<div class="bd-example">
33-
<div class="bd-example-container bd-example-container-fluid">
34-
<div class="bd-example-container-header"></div>
35-
<div class="bd-example-container-sidebar"></div>
36-
<div class="bd-example-container-body"></div>
35+
<div class="example-container-element p-3">
36+
.container-fluid
3737
</div>
3838
</div>
3939

@@ -43,6 +43,25 @@ Use `.container-fluid` for a full width container, spanning the entire width of
4343
</div>
4444
{{< /highlight >}}
4545

46+
### Responsive
47+
48+
Responsive containers are new in Bootstrap v4.4. They allow you to specify a class that is 100% wide until particular breakpoint is reached and a `max-width` is applied. For example, `.container-sm` is 100% wide to start until the `sm` breakpoint is reached, where it will remain through the higher breakpoints.
49+
50+
<div class="bd-example">
51+
<div class="example-container-element p-3 mb-3">
52+
.container-sm (100% wide until breakpoint)
53+
</div>
54+
<div class="example-container-element col-6 p-3 mx-auto">
55+
.container-sm (With max-width at breakpoint)
56+
</div>
57+
</div>
58+
59+
{{< highlight html >}}
60+
<div class="container-sm">100% wide until small breakpoint</div>
61+
<div class="container-md">100% wide until medium breakpoint</div>
62+
<div class="container-lg">100% wide until large breakpoint</div>
63+
<div class="container-xl">100% wide until extra large breakpoint</div>
64+
{{< /highlight >}}
4665

4766
## Responsive breakpoints
4867

site/static/docs/4.3/assets/scss/_component-examples.scss

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
border: 1px solid rgba($bd-purple, .15);
3131
}
3232

33+
.bd-example-responsive-containers {
34+
[class^="container"] {
35+
padding-top: .75rem;
36+
padding-bottom: .75rem;
37+
background-color: rgba(86, 61, 124, .15);
38+
border: 1px solid rgba(86, 61, 124, .2);
39+
}
40+
}
41+
3342
// Grid mixins
3443
.example-container {
3544
width: 800px;
@@ -69,37 +78,9 @@
6978
// Container illustrations
7079
//
7180

72-
.bd-example-container {
73-
min-width: 16rem;
74-
max-width: 25rem;
75-
margin-right: auto;
76-
margin-left: auto;
77-
}
78-
79-
.bd-example-container-header {
80-
height: 3rem;
81-
margin-bottom: .5rem;
82-
background-color: lighten($blue, 50%);
83-
@include border-radius;
84-
}
85-
86-
.bd-example-container-sidebar {
87-
float: right;
88-
width: 4rem;
89-
height: 8rem;
90-
background-color: lighten($blue, 25%);
91-
@include border-radius;
92-
}
93-
94-
.bd-example-container-body {
95-
height: 8rem;
96-
margin-right: 4.5rem;
97-
background-color: lighten($bd-purple, 25%);
98-
@include border-radius;
99-
}
100-
101-
.bd-example-container-fluid {
102-
max-width: none;
81+
.example-container-element {
82+
background-color: rgba($blue, .25);
83+
border: 1px solid rgba($blue, .25);
10384
}
10485

10586

0 commit comments

Comments
 (0)