Vertically-align images in a 4-column layout using Bootstrap 3 - html

Yes folks, it's another vertical-alignment question! I'm trying to do something pretty simple with Bootstrap 3, but hitting a wall on it. I'm building on the answer to this related question, but hitting the problem described below.
Here's the markup:
<div class="container">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://www.vectortemplates.com/raster/batman-logo-big.gif">
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://www.vectortemplates.com/raster/superman-logo-012.png">
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://2.bp.blogspot.com/-91OnmwoX5t0/T8reMB25ReI/AAAAAAAACQQ/D_jlmi6vWTw/s1600/GREEN+LANTERN+LOGO.png">
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://ecx.images-amazon.com/images/I/41biZJowGyL._SY300_.jpg">
</div>
</div>
</div>
And the CSS:
.vcenter {
display: inline-block;
vertical-align: middle;
float: none;
}
img {
max-width:200px;
}
When "float:none;" is included on .vcenter, the images are vertically-aligned as desired. However, the fourth column wraps underneath the other three.
When "float:none;" is commented out, the 4-column layout is achieved, but the images aren't vertically-aligned.
Bootply example so you can see what I mean.
Any idea how to get a four-column layout and vertically-aligned images?

The issue is that display:inline-block takes the white space into account. If you stack the column divs right next to each with no new paragraph spaces it will line up correctly. So it would end up like this:
<div class="container">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://www.vectortemplates.com/raster/batman-logo-big.gif">
</div><div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://www.vectortemplates.com/raster/superman-logo-012.png">
</div><div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://2.bp.blogspot.com/-91OnmwoX5t0/T8reMB25ReI/AAAAAAAACQQ/D_jlmi6vWTw/s1600/GREEN+LANTERN+LOGO.png">
</div><div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 vcenter">
<img src="http://ecx.images-amazon.com/images/I/41biZJowGyL._SY300_.jpg">
</div>
</div>
</div>
For more info see:
Stackoverflow: css - two inline-block, width 50% elements don't stack
CSS-Tricks: Fighting the Space Between Inline Block Elements

The float on columns in Bootstrap 3 helps to account for the few pixels too large they are. To fix this you just need to play around with the margin a little:
.vcenter .col-xs-3, .vcenter .col-sm-3, .vcenter .col-md-3, .vcenter .col-lg-3 {
margin: 0px -2px;
}
Hope this helps!

Related

Bootstrap, how to align all image on the center? [duplicate]

This question already has answers here:
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
Currently my grid image were aligned like this
But my goal is to make the grid image like this, notice the "img 1" were aligned on the center. How to achieve this?
HTML
<div class="container">
<div class="row imagetiles">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
<img src=https://i.imgur.com/nXSiWbw.jpg class="img-responsive">
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
<img src=https://i.imgur.com/0dXOdvZ.jpg class="img-responsive">
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
<img src=https://i.imgur.com/nXSiWbw.jpg class="img-responsive">
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
<img src=https://i.imgur.com/0dXOdvZ.jpg class="img-responsive">
</div>
</div>
</div>
CSS
div.imagetiles div.col-lg-3.col-md-3.col-sm-3.col-xs-6{
padding: 0px;
}
jsfiddle can be view here
Here is the Bootstrap 4 way to do so.
You can just add d-flex align-items-center class to the row:
<div class="container">
<div class="row imagetiles d-flex align-items-center">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6 p-0">
<img src=https://i.imgur.com/nXSiWbw.jpg class="img-fluid">
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6 p-0">
<img src=https://i.imgur.com/0dXOdvZ.jpg class="img-fluid">
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6 p-0">
<img src=https://i.imgur.com/nXSiWbw.jpg class="img-fluid">
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6 p-0">
<img src=https://i.imgur.com/0dXOdvZ.jpg class="img-fluid">
</div>
</div>
</div>
Also, you used an incorrect version of Bootstrap 4 in your initial JSFiddle. Updated to the latest version at this moment. Also, the jQuery version used is also incorrect (you should notice an error in Developer Console).
To set a particular DIV to have zero padding, you can use Bootstrap layout help class p-0; you don't need to set it specifically in CSS.
Last, img-responsive is for Bootstrap 3, img-fluid is for Bootstrap 4. Don't mix them up.
JSFiddle: https://jsfiddle.net/shivanraptor/bq8covg5/7/
Change your CSS to:
div.imagetiles div.col-lg-3.col-md-3.col-sm-3.col-xs-6{
padding: 0px;
align-self: center;
}
div.imagetiles{
display: flex;
}
here's the jsfiddle
Simply add this to your CSS:
.imagetiles {
align-items: center;
display: flex;
}
jsfiddle here: https://jsfiddle.net/7snfxL4d/
You can use different approaches but the flex one is really easy like this :
.imagetiles{
display: inline-flex;
align-items: baseline;
}
Try to add class my-auto to your img tag
If 1st dont work try to make the whole row a flex-wrap with classes d-flex align-content-center flex-wrap
You can find more here.

How can I fill an empty div partitioned using bootstrap grid system with a specific color?

Consider this case
<div class="row">
<div class="col-md-3 col-sm-3 col-xs-3"></div>
<div class="col-md-6 col-sm-6 col-xs-6"></div>
<div class="col-md-3 col-sm-3 col-xs-3"></div>
</div>
In this case, suppose if I need to fill the first div (first column) within the parent div with a background of #FF0000 how can I do it? If I can do it in a regular way, that is by specifying the style="background-color:red", the background changes only for the content written within it. Not if I create another div inside it and set its height and width to 100% nothing happens. Why is it so? And how can I do it the correct way?
Use a
<div class="col-md-3 col-sm-3 col-xs-3"> </div>
And set the background color as you wish.
Simple and elegant.
Empty div has no height. Use height, min-height, padding-top or padding-bottom to make empty div visible. You may do it
by class for column
by class for row with :first-child
Bootstrap uses col-xs- for screens of any width if there are no other conditions. Therefore col-md-3 col-sm-3 col-xs-3 is equivalent to col-xs-3.
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
.make-first-div-red > div:first-child,
.make-it-red {
background-color: #f00;
min-height: 30px;
margin-bottom: 10px;
)
<div class="row make-first-div-red">
<div class="col-xs-3"></div>
<div class="col-xs-6"></div>
<div class="col-xs-3"></div>
</div>
<div class="row">
<div class="col-xs-3 make-it-red"></div>
<div class="col-xs-6"></div>
<div class="col-xs-3"></div>
</div>
<div class="row">
<div class="bg col-md-3 col-sm-3 col-xs-3">adadadadada</div>
<div class="col-md-6 col-sm-6 col-xs-6"></div>
<div class="col-md-3 col-sm-3 col-xs-3"></div>
</div>
and use this css
.bg{background:#FF0000}
and it will work

Using borders on column divs in Bootstrap

I have a row with a few columns. I want each div to have a 1px border around it, but wherever two divs touch, there shouldn't be overlapping borders (2px). I want it like a grid, but without using tables as this has to be responsive. In the picture below, I want the first effect, not the second. I've tried borders with border-collapse but it did nothing, even when adding "display:table-cell".
<div class="container">
<div class="row">
<div class="col-xs-6 col-sm-3 col-lg-2"></div>
<div class="col-xs-6 col-sm-3 col-lg-2"></div>
<div class="col-xs-6 col-sm-3 col-lg-2"></div>
<div class="col-xs-6 col-sm-3 col-lg-2"></div>
<div class="col-xs-6 col-sm-3 col-lg-2"></div>
<div class="col-xs-6 col-sm-3 col-lg-2"></div>
</div>
</div>
it's probably not the best solution (may be the easiest) but you could just add
div {
margin-top:-1px;
margin-left: -1px;
}
so then the borders overlap and it looks like a 1px border all around

not getting any kind of margin in bootstrap grid

I was creating a template today using bootstrap. suddenly My grids are not getting any margin between them.
my markup are pretty simple:
<div class="container" div style="margin-top:50px;margin-bottom:50px">
<div class="row">
<div class="col-lg-3 col-md-3 col-xs-3 col-sm-3 box">
<p>flacon</p>
</div>
<div class="col-lg-3 col-md-3 col-xs-3 col-sm-3 box">
<p>flacon</p>
</div>
<div class="col-lg-3 col-md-3 col-xs-3 col-sm-3 box">
<p>flacon</p>
</div>
<div class="col-lg-3 col-md-3 col-xs-3 col-sm-3 box">
<p>flacon</p>
</div>
</div>
</div>
I didn't face anything like it before. please help me.
There is no margins between the columns in bootstrap but there is padding. When you set the background-color to white, that creates a background for the padding too which is why it looks continuous and not separated.
Add this:
<div class="col-sm-3">
<div class="white-box">
...
</div>
</div>
With .white-box whatever you want such as background-color: white;
The Bootstrap grid-system does not add any margins - instead it adds a padding inside the columns. You could build something like this:
<div class="container" div style="margin-top:50px;margin-bottom:50px">
<div class="row">
<div class="col-xs-3">
<div class="col-xs-12 box">
<p>flacon</p>
</div>
</div>
<div class="col-xs-3">
<div class="col-xs-12 box">
<p>flacon</p>
</div>
</div>
<div class="col-xs-3">
<div class="col-xs-12 box">
<p>flacon</p>
</div>
</div>
<div class="col-xs-3">
<div class="col-xs-12 box">
<p>flacon</p>
</div>
</div>
</div>
</div>
I also removed your many col-XX-3 classes as stated in my comment. For mor details see Bootstrap documentation
Grid classes apply to devices with screen widths greater than or equal to the breakpoint sizes, and override grid classes targeted at smaller devices. Therefore, applying any .col-md- class to an element will not only affect its styling on medium devices but also on large devices if a .col-lg- class is not present.

Make a div overflow horizontally if max height is reached?

I am using Bootstrap and trying to achieve the following with thumbnails.
I the div #demo reaches its max height then make anything after this height overflow horizontally and enable a scrollbar horizontally. I tried overflow-x:auto and other tricks but the div overflows vertically. Any ideas on how to do this?
<div class="row">
<a class="col-md-6 col-xs-12" href="http://i1.ytimg.com/vi/Tm0FAcMQdzg/mqdefault.jpg">
<div class="thumbnail">
<img src="http://i1.ytimg.com/vi/Tm0FAcMQdzg/mqdefault.jpg" alt="...">
</div>
</a>
<div id="demo" style="overflow-y: auto; max-height: 430px;">
<a class="col-md-4 col-xs-4" href="http://i1.ytimg.com/vi/Tm0FAcMQdzg/mqdefault.jpg">
<div class="thumbnail">
<img src="http://i1.ytimg.com/vi/Tm0FAcMQdzg/mqdefault.jpg" alt="...">
</div>
</a>
.....
</div>
</div>
Have a look here: Create horizontally scrolling List Item view using Bootstrap Columns
And also look this sample: http://jsfiddle.net/V5zWT/10/
<div class="DocumentList">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 DocumentItem">aaa</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 DocumentItem">aaa</div>
</div>
</div>