How Does Twitter Bootstrap Achieve Automatic Column Stacking As Width Decreases? - css

In Bootstrap 3, you can use the following code to build a row with 3 columns that collapse onto one another as the screen width decreases:
<div class = 'row'>
<div class = 'col-md-4'>
Some text
</div>
<div class = 'col-md-4'>
Some text
</div>
<div class = 'col-md-4'>
Some text
</div>
</div>
I am working on a project right now where I would like to achieve the same effect, but without having to load Bootstrap.
How does Bootstrap achieve the 'stacking' effect that you would get from the above?

The basic idea is that the width of the columns work on percentages. So in your example the width of each column might be 33.333% and then you use a media query to say at a certain resolution the width of items change and they stop floating or whatever else you want to make them do on a smaller resolution.
Here's an example of a media query where any device with a resolution with a maximum width of 767px will be affected.
#media (max-width: 767px) {
.col-md-4 {
width: 100%;
float: none;
}
}
Here's a great article by Ethan Marcotte who is pretty much credited with creating what we now know as responsive web design:
http://alistapart.com/article/responsive-web-design/

Related

How can I change column widths in bootstrap when I only have access to CSS (no way for me to change the HTML)?

<div class="col-12 col-md-7">column md-7</div>
<div class="col-12 col-md-1">spacer md-1</div>
<div class="col-12 col-md-4">column md-4</div>
I've volunteered to do some work on a page for a conference about accessibility. The organisers have used a "drag and drop" page builder but it has no editing ability for sub pages. The page they need to change has a small amount of content in the md-7 column but most of the text of the speaker bios is in the md-4 column and looks much too narrow.
They have asked me to use the "Add custom CSS functionality" to adjust the width of the columns.
So my question is how to use ONLY CSS to increase the width of the third column.
I have tried the following:
.col-12.col-md-7 { max-width:30%; }
.col-12.col-md-4 { flex-grow:5 !important; }
.col-12.col-md-4 { max-width:70% !important; }
and it expands the third column nicely on a regular screen but falls apart on mobile as the 30% width is too small for the first column.
..and it expands the third column nicely on a regular screen but falls
apart on mobile as the 30% width is too small for the first column.
Perhaps I misunderstand the problem, but can you not just use media queries? BS4 use among other media queries these definitions (your CSS included) :
#media (min-width: 1200px) {
.col-12.col-md-7 { max-width:30%; }
.col-12.col-md-4 { flex-grow:5 !important; }
.col-12.col-md-4 { max-width:70% !important; }
}
#media (min-width: 576px) {
.col-12.col-md-7 { max-width: 60%; } /* perhaps 60% fits better */
.col-12.col-md-4 { flex-grow:5 !important; } /* change other classes accordingly */
.col-12.col-md-4 { max-width:70% !important; }
}
if you don't want to use inline css, you can define a class for all columns separately. You can then select and replace any column with css. This method is a bit laborious. If you want it to change in mobile view, you have to use #media
so you have to use this method and arrange each one individually in whatever size you want it to look like.
Html:
<div class="col-12 col-md-7 my-col1">column md-7</div>
<div class="col-12 col-md-1 my-col2">spacer md-1</div>
<div class="col-12 col-md-4 my-col3">column md-4</div>
Css:
.my-col3 {
// what if you want to
}
If you want to solve the size problem using only bootstrap other than css, you need to look at the column properties in more detail. You can specify how much space to take up in what size, so you don't have to add css properties. When you do this, you can also adjust the mobile and tablet appearance by taking advantage of the bootstrap responsive features.
in short, it would be a more logical move to adjust the responsive according to the column dimensions.
You can wrap the whole thing inside a container-fluid and then define a new class for every column. Then change the width of the column in css and use #media query to define the transformation in media breakpoints.

Force Bootstrap 4 breakpoint (for preview) by class

Bootstrap 4 CSS uses media queries to get current screen size and display elements in the style of according device (screen width).
But what if I want to show preview of mobile markup on wide screen?
I want something like adding some class "preview-sm":
<div class="preview-sm">
<div class="container">
<div class="row">
...
</div>
</div>
</div>
so all Bootstrap 4 media queries will be overrided by breakpoint I specify in this class.
Is this possible?
Or maybe there is another way to show preview in Bootstrap?
You can break styles by class like that:
.preview-sm .container {
width: 1000px !important;
}
But #media always based on screen size.

Twitter's Bootstrap 3 grid, changing breakpoint and removing padding

I m trying to move to the new boostrap 3 grid and I m facing some difficulties.
How to have the grid to stack below 480px but not between 480px and 768px?
above 768px the padding left of the first and the padding right of the last are outside the container, but below 768px the padding is inside the container so the look is different because the content is no more aligned with a container that could be above.
when the grid stack the padding remain but to me it should be at 0.
Any help would be welcome I m using the code below with bootstrap 3 RC1
<div class="container" style="background-color: purple;">
container
</div>
<div class="container">
<div class="row">
<div style="background-color: red" class="col-4 col-sm-4 col-lg-4"><img data-src="holder.js/100%x200"></div>
<div style="background-color: blue" class="col-4 col-sm-4 col-lg-4"><img data-src="holder.js/100%x200"></div>
<div style="background-color: green" class="col-4 col-sm-4 col-lg-4"><img data-src="holder.js/100%x200"></div>
</div>
</div>
update jan 2014
See also: https://github.com/twbs/bootstrap/issues/10203
update 21 aug 2013
Since Twitter Bootstrap 3 RC2 the col-* mentioned below has been renamed to xs-col-*
There are four grid classes now: xs-col-* (mobile, never stacks), col-sm-* (tablet, stacks below 768px), col-md-* (laptops,stacks below 992 px) and col-lg-* (desktop, stacks below 1200px).
update
In my previous answer i use this table from the recent docs:
[old image removed]
When i test this values if found something different:
"col-xs-*" will be applied always (never stacks)
"col-sm-*" will be applied between 768 and higher (992px) (stacks at 767)
"col-lg-*" will be applied between 992 and higher (stacks at 991)
In variables.less you will find:
// Media queries breakpoints
// --------------------------------------------------
// Tiny screen / phone
#screen-tiny: 480px;
#screen-phone: #screen-tiny;
// Small screen / tablet
#screen-small: 768px;
#screen-tablet: #screen-small;
// Medium screen / desktop
#screen-medium: 992px;
#screen-desktop: #screen-medium;
But there doesn't seem to be a breakpoint at 480px (or as #fred_ says the grid is missing the col-ts-* (tiny to small) set of classes). See also: https://github.com/twbs/bootstrap/issues/9746
To set the stacking point at 480px you will have to recompile yours css. Set #screen-small to 480px; and define your cols with:
<div style="background-color: red" class="col-sm-4"> after that.
Note this will change #grid-float-breakpoint also cause it is defined as #grid-float-breakpoint: #screen-tablet;.
When adding a row to the container i don't find problems with padding.
Or try: http://www.bootply.com/70212 it will stack below 480px by adding a media query (the javascript is used for illustration only)
previous answer
From now Twitter’s Bootstrap defines three grids: Tiny grid for Phones (<480px), Small grid for Tablets (<768px) and the Medium-large grid for Destkops (>768px). The row class prefixes for these grid are “.col-”, “.col-sm-” and “.col-lg-”. The Medium-large grid will stack below 768 pixels screen width. So does the Small grid below 480 pixels and the tiny grid never stacks.
With your "col-4" prefix the grid will never stack. So remove "col-4" to let your grid stack below the 480px. This also will remove padding cause is stacks now.
See also: http://bassjobsen.weblogs.fm/migrate-your-templates-from-twitter-bootstrap-2-x-to-twitter-bootstrap-3/ and Writing Twitter's Bootstrap with upgrading to v3 in mind
Use class .col-ts-12 for all 100% divs under 480px and put this code at the end of bootstrap.css:
#media (max-width: 480px) {
.col-ts-12 { float: none; }
}
To implement any changes for tiny to small devices you will need to include your own media queries to add your own additional breakpoints.
For example:
#media (max-width: 480px) {
.no-float {
display:block;
float:none;
padding:0;
}
}
I am not quite sure what you are trying to achieve with this but this is how you would apply styles where the screen width is below 480px.

Twitter's Bootstrap mobile: more columns

Regarding twitter bootstrap, I currently have a design showing pictures in a grid
<div class="row-fluid">
<div class="image span-4"></div>
<div class="image span-4"></div>
<div class="image span-4"></div>
<div class="image span-4"></div>
<div class="image span-4"></div>
....
</div>
This works quite well, showing 3 pictures in a row on desktop and tablet.
On mobile, they only show one under each other.
Do I have the possibility to show 2 columns next to each on mobile?
Thanks for your help
Below the 768 pixel width the (fluid) grid stack the elements. Add a media query below your bootstrap css include:
#media (max-width: 767px) { .row-fluid .image { width:50%; float:left; } }
Note in your example code you use many span-4's in a row. The total span of a row should be max 12.
Cause you use a odd number of images, you will get the last row with one image 50%. To get images of different row together you will have to reset the display:table of your fluid row. Add an extra class to your rows like 'inline' and use the media query to reset like:
#media (max-width: 767px) { .row-fluid .image { width:50%; float:left; } .inline:before,.inline:after {display: inline-block; content:none;} }
Example: http://bootply.com/62893
Twitter Bootstrap 3.0
Twitter’s Bootstrap 3 defines three grids: Tiny grid for Phones (<480px), Small grid for Tablets (<768px) and the Medium-large grid for Destkops (>768px). The row class prefixes for these grid are “.col-”, “.col-sm-” and “.col-lg-”. The Medium-large grid will stack below 768 pixels screen width. So does the Small grid below 480 pixels and the tiny grid never stacks. Except for old phones which always will stack the elements (mobile first design). Tiny grid for Phones (<768px), Small grid for Tablets (>768px) and the Medium-Large grid for Destkops (>992px). The row class prefixes for these grid are “.col-”, “.col-sm-” and “.col-lg-”. The Medium-large grid will stack below 992 pixels screen width. So does the Small grid below 768 pixels and the tiny grid never stacks. Except for old phones which always will stack the elements (mobile first design). (based on TB3 RC1)
For mobile you could use the “.col-” prefixes (tiny grid) but you still got the problem with the odd number of images in a row. To fix this you could try to add 24 for columns in a row instead of 12. Or use the same solution as above for TB2.
See: http://bootply.com/70644
In Twitter Bootstrap 3.0 there will be a grid for small devices too. You can use this by adding an extra class col-small-span-* to your divs. Note span-* is renamed to col-span-*. So you will get:
<div class="image col-span-4 col-small-span-6"><img src="//placehold.it/350x150">/div>
This will give you 3 (12/4) columns of 33% on the default grid and 2 (12/6) columns of 50% on the small grid. See also: http://bassjobsen.weblogs.fm/migrate-your-templates-from-twitter-bootstrap-2-x-to-twitter-bootstrap-3/

Twitter Bootstrap: non-responsive row in responsive layout

I'm using a responsive fluid grid system on my site, and in most cases the responsive Bootstrap behaviour is what I want: on small screens, the grid columns become fluid and stack vertically.
However, using grid nesting, inside a nested row this is not always the desired behaviour. There are some rows that are not supposed to be stacked vertically, regardless how small the screen is. This is exactly the behaviour of the whole grid when I completely disable all responsive CSS code, but obviously this is no alternative if the responsive behaviour is required for the outer rows.
The relevant markup is:
<div class="container-fluid">
<div class="row-fluid">
<div class="span6">This column should be stacked on small devices.</div>
<div class="span6">
<div class="row-fluid">
<div class="span6">Nested row. This column should NOT be stacked on small devices.</div>
<div class="span6">Nested row. This column should NOT be stacked on small devices.</div>
</div>
</div>
</div>
</div>
See this jsfidde for clarification.
How would one best solve this problem? Is there a way to do it with the native bootstrap functions?
[class*="span"] .span6 { display: inline-block; width: 48.61878453038674%}
example:
http://jsfiddle.net/NfTQ7/1/
What I have done to solve issues like this is the following:
<div class="row-fluid">
<div id="remove-mobile" class="span6">Nested row. This column should NOT be stacked on small devices.</div>
<div id="remove-mobile" class="span6">Nested row. This column should NOT be stacked on small devices.</div>
</div>
#media only screen and (max-width: 480px) {
#remove-mobile {
display:none;
}
}
That way, you get rid of that whole mess on smaller devices, and you can add code specifically targeted towards mobile sizes by simply doing the opposite:
<div class="row-fluid">
<div id="show-mobile" class="span6">Your Beautiful Code For Mobile Only</div>
</div>
#media only screen and (min-width: 481px) {
#show-mobile {
display:none;
}
}
#media only screen and (max-width: 480px) {
#show-mobile {
display:block;
}
#remove-mobile {
display:none;
}
}
It's not the simplest of solutions but I've found it suits my needs
https://github.com/twitter/bootstrap/blob/master/less/mixins.less#L572
If you dive into the source for bootstraps grid, it's relatively easy to pull out the less code used to generate the span[1-12] system.
So I just pulled out the basics and put them in my own file with a different selector. So now, when I want to use span's that don't wrap I just use .naps[1-12] (Span spelt backwards).
The responsive CSS looks for .span[1-12] selectors so it ignores my .naps elements.
It's not elegant, and it's not particularly scalable. It does work though :-/

Resources