Bootstrap vertical aligned without breaking the grid - html

I been trying all the different options that I found to vertically align a grid. Because of the layout that I have none of them worked.
This is what I have to do: for COL-MD and COL-LG the layout should be:
DIV1-IMG DIV2-TEXT
DIV3-TEXT DIV4-IMG
In this case the DIV-TEXT has to be vertically aligned.
For COL-SM and under, the layout should be:
DIV1-IMG
DIV2-TEXT
DIV4-IMG (if it is complicated, we can stick to first show DIV3-TEXT and then DIV-4-IMG)
DIV3-TEXT
The problem is that if I set the div to display:table and its variants, the grid breaks and it doesn't resize to col-xs.
Here I set up the example in case you can see what I am trying to accomplish: http://www.bootply.com/nCIExg8DkM
Any ideas on how one can align the text vertically without breaking the grid?
Thanks!

Use media queries to reduce the coverage area of your style: #media (min-width: 992px) {}.
Use .col-md-push-6 and .col-md-pull-6 modifier classes to change the order of your columns.
Simplify classes:
col-xs-12 col-lg-6 col-md-6 is equal to col-md-6.
col-lg-offset-2 col-lg-8 col-md-offset-1 col-md-10 col-xs-offset-1 col-xs-10 is equal to col-lg-offset-2 col-lg-8 col-xs-offset-1 col-xs-10.
Use special helpful classes:
The .img-responsive class scales the image nicely to the parent element.
The .center-block class aligns the block via margin.
Check the result: http://www.bootply.com/WEf5cQlDog
#media (min-width: 992px) {
.vertical-middle > div {
display: table-cell;
float: none;
vertical-align: middle;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-xs-offset-1 col-xs-10 col-lg-offset-2 col-lg-8">
<div class="row vertical-middle">
<div class="col-md-6">
<img src="http://www.hercampus.com/sites/default/files/2013/02/27/topic-1350661050.jpg" class="center-block img-responsive">
</div>
<div class="col-md-6 text-center">
<h2>Title 1</h2>
<p>Text to be centered</p>
</div>
</div>
<div class="row vertical-middle top-buffer">
<div class="col-md-6 col-md-push-6">
<img src="http://www.hercampus.com/sites/default/files/2013/02/27/topic-1350661050.jpg" class="center-block img-responsive">
</div>
<div class="col-md-6 col-md-pull-6 text-center">
<h2>Title 2</h2>
<p>Text to be centered</p>
</div>
</div>
</div>
</div>
</div>

Related

My Bootstrap row is extending beyond the page width when I resize it?

I'm very new to Bootstrap and have been working through some tutorials. I'm currently trying to rebuild Google's homepage and have run into some difficulty with the responsiveness of the grid system.
I've created a very basic layout of the top bar on Google's homepage and it more or less looks fine as it is fullscreen; however, when I resize the window, the text on the right hand side spills over the width of the window.
<body>
<div class="container-fluid" id="topbar">
<div class="row">
<div class="col-1 justify-content-start aboutlink">
About
</div>
<div class="col-1 justify-content-start">
Store
</div>
<div class="col-8">
</div>
<div class="col-1 justify-content-end gmaillink">
Gmail
</div>
<div class="col-1 justify-content-end">
Images
</div>
</div>
</div>
Here's an image of the issue:
The classes "aboutlink" and "gmaillink" are simply aligning the text to the right and the topbar id has a 15px margin and sets the font size.
I've had a read through the responsive breakpoints and grid system documentation, but can't seem to fix this issue. Would be grateful if anyone could share some advice?
Thank you.
What is going wrong?
If we add a border to the columns and allow the word to wrap if it doesn't fit, we can see better what is happening.
Take a look at this example, and you will see that on smaller screens the words are not fitting into the col-1 divs, and because words don't wrap by default it is causing the col to grow bigger than it should be to accommodate the size of the text:
.col-1 {
overflow-wrap: break-word; border: 1px solid lightgray;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="container-fluid" id="topbar">
<div class="row">
<div class="col-1 aboutlink">
About
</div>
<div class="col-1">
Store
</div>
<div class="col-8">
</div>
<div class="col-1 gmaillink">
Gmail
</div>
<div class="col-1">
Images
</div>
</div>
</div>
1. Breakpoints and padding classes
Bootstrap's grid classes to allow you to set the breakpoints for the cols. So for example these classes mean: give the column 6/12 of the space on screens up to the md breakpoint (768px), and 8/12 of the space from 768px and up:
<div class="col-6 col-md-8">
Bootstrap also has spacing classes that can be used to change the padding of the columns. The px-* classes set the padding for the left and right padding. The default is px-3, so we can use px-1 to make the padding smaller and so the same size columns can fit in more content.
Example using col-sm-* and px-*:
.row div {border:1px solid lightgray;}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="container-fluid" id="topbar">
<div class="row">
<div class="col-2 col-sm-1 aboutlink px-1">
About
</div>
<div class="col-2 col-sm-1 px-1">
Store
</div>
<div class="col-4 col-sm-8">
</div>
<div class="col-2 col-sm-1 gmaillink px-1">
Gmail
</div>
<div class="col-2 col-sm-1 px-1">
Images
</div>
</div>
</div>
2. Bootstrap Auto classes
A better option in this case (as you don't need a defined structure) might be to use the col-auto Bootstrap classes that will use only the space needed to fit the content - this can overcome the problem of having to set the cols to a specific proportion of the width, such as 1/12 or 2/12.
In the example below, we set the width of the first 2 and last 2 columns to col-auto so they will resize to fit the text inside them, and then give the middle column the col class to take the rest of the available space:
.col-auto{ border:1px solid lightgray;}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="container-fluid" id="topbar">
<div class="row">
<div class="col-auto aboutlink px-1">
Abouttttt
</div>
<div class="col-auto px-1">
Store
</div>
<div class="col">
</div>
<div class="col-auto gmaillink px-1">
Gmail
</div>
<div class="col-auto px-1">
Images
</div>
</div>
</div>
FYI: the justify-content-* classes are for flexbox layouts & don't work with the grid classes, so I have removed them from the examples.

how to change div align in mobile version

I have 3 div in my html. div1, div2,div3. Now in mobile version I want to change the alignment of those div. I have given in below images.
HTML Code
<div class="row">
<div class="col-md-12">
Div1
</div>
<div class="col-md-9" style="float:right;">
Div3
</div>
<div class="col-md-3" style="float:left;">
Div2
/div>
</div>
But didn't solved.
How to do that?
Anybody help please ?
Looks like you are using bootstrap. If you re using Bootstrap 4 you can use the ordering feature to easily achieve this.
<div class="row">
<div class="col-md-12 order-md-1 order-sm-1">
Div1
</div>
<div class="col-md-3 order-md-2 order-sm-3">
Div2
</div>
<div class="col-md-9 order-md-3 order-sm-2">
Div3
</div>
</div>
Fiddle
EDIT
Since you are using bootstrap 3, reordering col-12 are not possible (e.g. col- * -push / col- * -pull). As alternative you can use flex-box or grid to achieve this easily. See example below.
<div class="row reordered">
<div class="col-md-12">
Div1
</div>
<div class="col-md-3">
Div2
</div>
<div class="col-md-9">
Div3
</div>
</div>
CSS - Reordered using grid
#media screen and (max-width: 992px) { //992 is the breakpoint of col-md
.reordered {
display: grid;
}
.col-md-3 {
order: 2;
}
.col-md-9 {
order: 1;
}
}
Fiddle
<div class="row">
<div class="col-md-12 col-12">
Div1
</div>
<div class="col-md-12 col-12">
<div class="row flex-row-reverse flex-column-reverse">
<div class="col-md-3 col-12">
Div2
</div>
<div class="col-md-9 col-12">
Div3
</div>
</div>
</div>
</div>
If I understand correctly, you're using the Bootstrap framework in which case you can use the "responsive utility classes" which are part of the framework's grid system to achieve what you require. To control the ordering of elements on the mobile version, you will also need to add some additional CSS.
The following changes to your HTML, along with the added CSS of .order-row etc, should achieve this for you:
#media (max-width: 768px) {
.order-row {
display: flex;
flex-direction: column;
}
.order-0 {
order: 0;
}
.order-1 {
order: 1;
}
}
<link href="https://cdn.usebootstrap.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12">
Div1
</div>
</div>
<div class="row order-row">
<div class="col-md-3 col-sm-12 order-1">
Div2
</div>
<div class="col-md-9 col-sm-12 order-0">
Div3
</div>
</div>
</div>
For more information on how to use bootstraps responsive grid classes, see this documentation
Div1
Div2
Div3
if you use bootstrap 4, you can use order
<div class="row">
<div class="col-md-12">
Div1
</div>
<div class="col-md-9 order-md-0 order-1">
Div2
</div>
<div class="col-md-3 order-md-1 order-0">
Div3
/div>
</div>
Explanation
the Div2 will show on the left on viewport md and bigger, and it will shown under Div3 if the viewport is smaller than md

How to center content in a Bootstrap column?

I am trying to center column's content. Does not look like it works for me. Here is my HTML:
<div class="row">
<div class="col-xs-1 center-block">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
JSFiddle Demo
Use:
<!-- unsupported by HTML5 -->
<div class="col-xs-1" align="center">
instead of
<div class="col-xs-1 center-block">
You can also use bootstrap 3 css:
<!-- recommended method -->
<div class="col-xs-1 text-center">
Bootstrap 4 now has flex classes that will center the content:
<div class="d-flex justify-content-center">
<div>some content</div>
</div>
Note that by default it will be x-axis unless flex-direction is column
[Updated Apr 2022]: Tested and Included the 5.1 version of Bootstrap.
I know this question is old. And the question did not mention which version of Bootstrap he was using. So I'll assume the answer to this question is resolved.
If any of you (like me) stumbled upon this question and looking for answers using the current bootstrap 5.0 (2020) and 4.5 (2019) framework, then here's the solution.
Bootstrap 4.5, 5.0, and 5.1
Use d-flex justify-content-center on your column div. This will center everything inside that column.
<div class="row">
<div class="col-4 d-flex justify-content-center">
// Image
</div>
</div>
If you want to align the text inside the col just use text-center
<div class="row">
<div class="col-4 text-center">
// text only
</div>
</div>
If you have text and image inside the column, you need to use d-flex justify-content-center and text-center.
<div class="row">
<div class="col-4 d-flex justify-content-center text-center">
// for image and text
</div>
</div>
Bootstrap naming conventions carry styles of their own, col-XS-1 refers to a column being 8.33% of the containing element wide. Your text, would most likely expand far beyond the specified width, and couldn't possible be centered within it. If you wanted it to constrain to the div, you could use something like css word-break.
For centering the content within an element large enough to expand beyond the text, you have two options.
Option 1: HTML Center Tag
<div class="row">
<div class="col-xs-1 center-block">
<center>
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</center>
</div>
</div>
Option 2: CSS Text-Align
<div class="row">
<div class="col-xs-1 center-block" style="text-align:center;">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
If you wanted everything to constrain to the width of the column
<div class="row">
<div class="col-xs-1 center-block" style="text-align:center;word-break:break-all;">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
UPDATE - Using Bootstrap's text-center class
<div class="row">
<div class="col-xs-1 center-block text-center">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
FlexBox Method
<div class="row">
<div class="flexBox" style="
display: flex;
flex-flow: row wrap;
justify-content: center;">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
http://jsfiddle.net/usth0kd2/13/
Bootstrap 4/Bootstrap 5, horizontal and vertical align contents using flex box
<div class="page-hero d-flex align-items-center justify-content-center">
<h1>Some text </h1>
</div>
Use bootstrap class align-items-center and justify-content-center
.page-hero {
height: 200px;
background-color: grey;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="page-hero d-flex align-items-center justify-content-center">
<h1>Some text </h1>
</div>
No need to complicate things. With Bootstrap 4, you can simply align items horizontally inside a column using the margin auto class my-auto
<div class="col-md-6 my-auto">
<h3>Lorem ipsum.</h3>
</div>
Use text-center instead of center-block.
Or use center-block on the span element (I did the column wider so you can see the alignment better):
<div class="row">
<div class="col-xs-10" style="background-color:#123;">
<span class="center-block" style="width:100px; background-color:#ccc;">abc</span>
</div>
</div>
This is a simple way.
<div class="row">
<div class="col-md-6 mx-auto">
<p>My text</p>
</div>
</div>
The number 6 controls the width of the column.
//add this to your css
.myClass{
margin 0 auto;
}
// add the class to the span tag( could add it to the div and not using a span
// at all
<div class="row">
<div class="col-xs-1 center-block">
<span class="myClass">aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
col-lg-4 col-md-6 col-sm-8 col-11 mx-auto
1. col-lg-4 ≥ 1200px (popular 1366, 1600, 1920+)
2. col-md-6 ≥ 970px (popular 1024, 1200)
3. col-sm-8 ≥ 768px (popular 800, 768)
4. col-11 set default smaller devices for gutter (popular 600,480,414,375,360,312)
5. mx-auto = always block center
You can add float:none; margin:auto; styling to centerize column content.
If none of the above work (like in my case trying to center an input), I used Boostrap 4 offset:
<div class="row">
<div class="col-6 offset-3">
<input class="form-control" id="myInput" type="text" placeholder="Search..">
</div>
</div>

Bootstrap grid div being pushed out at certain sizes

I wanted to create a layout with one image on the left taking up 6 columns and 2 'rows' and 4 squares on the right taking up the other half, i.e/ each one taking up 3 columns, 2 on top and 2 below.
This all works fine, except that 1 image gets pushed down. When the screen is made smaller, it pops back up, and then drops back down again at other smaller sizes.
Here it is on bootply
and here is my code:
<div class="container">
<div class="container-fluid">
<div class="row-fluid">
<div class="box1 col-md-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/Sheep-002-Wm.jpg">
</div>
<div class="box1 col-md-3 col-sm-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/roseveal.jpg">
</div>
<div class="box1 col-md-3 col-sm-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/roseveal5.jpg">
</div>
<div class="box1 col-md-3 col-sm-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/Sheep-002-Wm.jpg">
</div>
<div class="box1 col-md-3 col-sm-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/tentego_wagen.jpg">
</div>
</div>
</div>
</div>
My CSS is:
.box1 img {
width:100%;
}
.container {
width:100%;
padding:0px;
max-width: 1265px;
overflow: auto;
}
.row {
margin-left: 0px;
margin-right: 0px;
}
.col-md-3, .col-md-6, .col-sm-6 {
padding-left: 0px;
padding-right: 0px;
}
Please excuse the images - I'm meant to be designing a sort of farming website!
Any help or insight would be so much appreciated.
I have determined that it has to do with your images not being consistent.
Basically, your images are all different sizes, and you make no effort to enforce a standard upon them. You do put them in bootstrap columns, which is keeping them the proper widths, but the heights are still messing up and differing just slightly, which sometimes kicks your last image down to the next row.
I changed this css by adding a height:auto, which helped.
.box1 img{
width:100%;
height: auto;
}
I then tried making all four images on the right the same image, and the problem went away completely. I forked your bootply over here if you want to see the whole thing.
I have to say, It's looking pretty neat, you should be proud of yourself. It flows really well other than this hiccup. You might consider setting the max-height, or setting height to auto if you make sure that your pictures are all identical aspect ratios. They are already close to the same aspect ratio, but it's giving you these subtle difference that were difficult to detect visually, but are screwing up your layout.
Rufus, Try doing it like this.
Here is the full size Fiddle.
Using 3 sets of rows looks to help what you are looking to to here.
The extra classes also help keep the layout that you are looking for too.
<div class="container-fluid col-lg-12">
<div class="row-fluid col-sm-6 col-md-6 col-lg-6">
<div class="box1 col-sm-12 col-md-12 col-lg-12">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/Sheep-002-Wm.jpg">
</div>
</div>
<div class="row-fluid col-sm-6 col-md-6 col-lg-6">
<div class="box2 col-xs-6 col-sm-6 col-md-6 col-lg-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/roseveal.jpg">
</div>
<div class="box2 col-xs-6 col-sm-6 col-md-6 col-lg-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/roseveal5.jpg">
</div>
</div>
<div class="row-fluid col-sm-6 col-md-6 col-lg-6">
<div class="box2 col-xs-6 col-sm-6 col-md-6 col-lg-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/Sheep-002-Wm.jpg">
</div>
<div class="box2 col-xs-6 col-sm-6 col-md-6 col-lg-6">
<img src="http://www2.buit.ie/wp-content/uploads/2015/06/tentego_wagen.jpg">
</div>
</div>
</div>

Twitter-bootstrap-3 | Thumbnail Centering?

Hey Guys!
I'm currently working on a website using twitter bootstrap 3 but I've got some issues. I'm trying to center the 3 thumbnails I've got. The text is centered, image is centered but I can't seem to get the thumbnails centered. Here is my code:
<div class="row">
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="img/test.png">
<div class="caption">
<p>test</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="img/test.png">
<div class="caption">
<p>test</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="img/test.png>
<div class="caption">
<p>test</p>
</div>
</div>
</div>
CSS:
.thumbnail {
background-color: #cecece;
border: 1px solid #666;
text-align:center;
}
to center your content on the page, wrap the row in a containter ()
Notice that Bootstrap uses a 12 column grid. To fill your row with tree thumbs for the medium (md) grid, consider to use the col-md-4 class (3 x 4 = 12) class for your columns. ON the small (sm) grid you now have 3 columns of 50% by using three times the col-sm-6 class. Consider to use only one class, only the col-md-4 class will give you 3 columns for a screen width of 992px or wider and only the col-sm-4 class does the same but for screen widths of 768px and wider. Below the 992 or 768 the columns will stack.
The text can be centered by applying the text-center class, also see http://getbootstrap.com/css/#type-alignment
Also see: http://www.bootply.com/WAFUzlJs8s