Bootstrap container-fluid on xs devices only
Asked Answered
B

5

12

I want to use the full width on xs and sm devices (container-fluid) but just the container class for all other devices What's the best way to put this in place? I've tried jasnys bootstrap which has a container-smooth class but it doesn't centre the content when the screen gets over a certain size...

Bourque answered 6/4, 2015 at 12:57 Comment(3)
Have you noticed a need for this? container loses the "boxed" layout on xs devices naturally.Deckhouse
It doesn't quite use the full screen width on xs devices I've tested it, the container-fluid behaviour isn't seen on xsBourque
The easiest way would be to create a custom @media query and customize the .container class.Deckhouse
A
12

Overwrite the container class in your CSS and your done:

/* XS styling */
@media (max-width: @screen-xs-max) {
  .container {
    width: inherit;
  }
}

/* SM styling */
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) {
  .container {
    width: inherit;
  }
}

Just replace the Less variables with your corresponding px-values.

Adriatic answered 27/8, 2015 at 15:14 Comment(0)
H
5

Or you can do sth like this, and it works too:

// Extra small devices (portrait phones, less than 576px)
@media (max-width: 575.98px) {
    .container {
        min-width: 100%;
    }
}
Hydrous answered 26/3, 2018 at 14:34 Comment(0)
D
3

For bootstrap 4.4 & onwards

You can specify different container classes based on the device resolution. please have a look at the below example.

<div class="container-sm">
   /* Do your stuff here */
</div>

For more customization

enter image description here

Reference: https://getbootstrap.com/docs/4.4/layout/overview/#containers

Dispraise answered 25/4, 2022 at 4:44 Comment(3)
Just using the container-md class would work (md because they want 100% on both xs and sm). Using both classes will make one take priority over the other.Keon
@SiddharthBhansali but in that case, you have to write custom CSS for padding. As container-fluid comes with its own feature so it's better to use which is the official recommendation.Dispraise
You didn't understand my comment. I am saying you cannot use container-sm and container-fluid together. One will take precedence. OP wants 100% on xs and sm, but default container on other screens. Even by the picture you uploaded from the docs container-md does exactly that.Keon
C
0

.container sets a max-width at each responsive breakpoint and width: 100% on viewports thats less 576px wide. To let container take all space on small viewport you must specify <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> in the head section. Otherwise there will be magins on left and right.

Cataract answered 18/6, 2024 at 14:48 Comment(0)
C
-4

you can add a copy of the same container and configure it visible only in the sizes you want with the classes hidden-xx and visible-xx like this:

<div class="container-fluid hidden-md hidden-lg">
   your content here
</div>

and this for the normal container:

<div class="container hidden-xs hidden-sm">
       your content here
    </div>
Concoct answered 6/4, 2015 at 14:50 Comment(2)
you'd then need to duplicate your content though to accommodate both formats (ie xs, sm vs md, lg)Killam
Unnecessary duplication of content.Sym

© 2022 - 2025 — McMap. All rights reserved.