Bootstrap - Removing padding or margin when screen size is smaller
The problem here is much more complex than removing the container padding since the grid structure relies on this padding when applying negative margins for the enclosed rows.
Removing the container padding in this case will cause an x-axis overflow caused by all the rows inside of this container class, this is one of the most stupid things about the Bootstrap Grid.
Logically it should be approached by
- Never using the
.container
class for anything other than rows - Make a clone of the
.container
class that has no padding for use with non-grid html - For removing the
.container
padding on mobile you can manually remove it with media queries thenoverflow-x: hidden;
which is not very reliable but works in most cases.
If you are using LESS
the end result will look like this
@media (max-width: @screen-md-max) {
.container{
padding: 0;
overflow-x: hidden;
}
}
Change the media query to whatever size you want to target.
Final thoughts, I would highly recommend using the Foundation Framework
Grid as its way more advanced
The @media query specifically for 'phones' is..
@media (max-width: 480px) { ... }
But, you may want to remove the padding/margin for any smaller screen sizes. By default, Bootstrap adjusts margins/padding to the body, container and navbars at 978px.
Here are some queries that have worked (in most cases) for me:
@media (max-width: 978px) {
.container {
padding:0;
margin:0;
}
body {
padding:0;
}
.navbar-fixed-top, .navbar-fixed-bottom, .navbar-static-top {
margin-left: 0;
margin-right: 0;
margin-bottom:0;
}
}
Demo
Update for Bootstrap 4
Use the new responsive spacing utils which let you set padding/margins for different screen widths (breakpoints): https://stackoverflow.com/a/43208888/171456
The easy solution is to write something like that,
px-lg-1
mb-lg-5
By adding lg, the class will be applied only on large screens