Display non equal height element with bootstrap - css

I want to make a page with posts that have a non equal height, but same width like two posts on a row without the use of rows I did give every div a col-md-6 class which gave me two divs in a row but there's a gap between the elements since they are with different height.
like this:
and the gap becomes bigger if the show comments button is clicked:
here's a working plunker with this example.
I tried to pull the odd element to a side and the even element to the other, I thought maybe when elements are floated left they will stay left, but that was not correct.
first it looked like it worked:
but again once the show comments button is clicked the gap reappear and if I play a little with the padding the elements will be unaligned:
here's a working example in this plunker.
And in all this examples if an element is long enough it will push the element under it to the other side, which is the other behaviour that I don't want.
So the question is how could I display non equal height elements in a way that there's no gaps and that when one element is too long (or the show comments button is clicked) it won't push the one under it to the other side?
Note: I'm looking for a CSS solution.
update:
As #gorostas and #dsuess answered the use of two containers to separate the elements is the original design and it work as a visual organisation but when it come to ordering the posts that will be a problem since the loop wont run in two separate container and throwing one element in each every loop so for such a solution to work I need to write a function that splits the posts and organize them in a way that the post number 1 will be in the left, 2 on the right and 3 on the left and so on, even though that's doable it's rather unnecessary so going back to my original question I'm looking for a CSS solution I already have 2 other JavaScript solutions I don't like to use I rather found a simple and easy to implement one, that reduces the amount of HTML and JavaScript code written.

How about using 2 columns with each containing boxes:
$('div').on('click', function () {
$(this).toggleClass('expanded')
});
.column{
width: 48%;
float: left;
margin: 0 1%;
}
.box {
margin: 5px 1%;
width: 100%;
height: 40px;
border: 1px solid black;
background: #c3c3c3;
}
.expanded {
height: 120px;
background: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>click the boxes</h2>
<div class="column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>
<div class="column">
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>

What you think abaout using Masonry?
Here is working fiddle
http://jsfiddle.net/52VtD/9313/
$(document).ready(function () {
var $container = $('.masonry');
$container.function () {
$container.masonry({
itemSelector: '.post-box',
columnWidth: '.post-box',
transitionDuration: 0
});
});
});
UPDATE FINAL
<div class="row">
<div class="col-xs-6 col-md-6 col-sm-6">
1
</div>
<div class="col-xs-6 col-md-6 col-sm-6">
2
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-6 col-sm-6">
3
</div>
<div class="col-xs-6 col-md-6 col-sm-6">
4
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-6 col-sm-6">
5
</div>
<div class="col-xs-6 col-md-6 col-sm-6">
6
</div>
</div>
Working fiddle
http://jsfiddle.net/52VtD/9325/

Related

Why negative margin in .row?

In the Flexboxgrid framework I see a margin of -1rem on the .row class. In small viewports this creates a small horizontal scroll of the container.
Since I've seen this negative margin on other frameworks, what is its purpose? Inner columns have a padding of the same qty, reversed.
In the picture, red line is .container, dashed line is .row. Btw the margin is visible only on the right.
Because you're supposed to use them in combination with columns.
Columns generally have a padding to push the contents of them away from the border, in order to make it look nicer. However, when you are nesting columns within columns, the content keeps getting pushed inwards, which is mostly not a desired effect. To keep this from happening the rows have a negative margin, which pulls the columns back. In your case, it looks like you need to add a col-xs-12 around the column groups within the rows . This will prevent the content from being pulled too far.
Take a look here for a nicely explained introduction.
Here's a demonstration of how the .row class works:
.col1 {
background: red;
}
.col2 {
background: green;
}
body {
font-family: sans-serif;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/flexboxgrid/6.3.1/flexboxgrid.min.css" type="text/css">
<div class="row">
<div class="col-xs-12
col1">
<div class="col-xs-12
col2">
<div class="box">Without a row</div>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-12
col1">
<div class="row">
<div class="col-xs-12
col2">
<div class="box">With a row</div>
</div>
</div>
</div>
</div>
In general row is placed in container. container has padding of 15 and row has margin of -15

How to rearrange order of divs with 12 bootstrap columns width?

I have two divs being one above the other.
When the window width goes below 992 pixels, I want b to be above a, so I want to rearrange them vertically.
I am trying to do that with bootstrap. The result becomes like this:
Why?
html:
<div class="parent">
<div class="row">
<div class="a col-xs-12 col-xs-offset-12 col-sm-offset-12 col-md-offset-0">
a
</div>
<div class="b col-xs-12">
b
</div>
</div>
</div>
css:
.parent
{
width: 100px;
}
.a
{
background: #faa;
}
.b
{
background: #afa;
}
https://jsfiddle.net/24tt6xgL/5/
Other things I have tried:
pushing the a-div 12 and pulling the b-div 12 columns with col-md-push-12 and col-md-pull-12.
Only pushing the a-div, not pulling the b-div.
pushing the a-div 6 columns, pulling the b-div 6 columns
One solution for this would be to use the classes like hidden-*. In this way, you would have duplicate code, but it would quickly achieve what you would like to do and stay within Bootstrap.
<div class="parent">
<div class="row">
<div class="b col-xs-12 visible-xs visible-sm">
b
</div>
<div class="a col-xs-12">
a
</div>
<div class="b col-xs-12 hidden-xs hidden-sm">
b
</div>
</div>
</div>
As you can see here, the .b div has been duplicated, but never appears twice: For md and lg screens, the top .b div would be hidden and the bottom one would be shown. For xs and sm screens the opposite is true.
Of course, this solution is not perfect, the maintenance of the code is more of an effort, so if these divs are much longer than in the example code, it is probably worth trying to play with the CSS, but if the code is short, it would be a good solution.
It's also worth noting that duplicating the .a div above and below the .b div instead is an equivalent solution.

Centering columns which are not multiple of 2

The offset cannot be of any use for centering columns which aren't multiples of 2 such as col-md-3. But let's say one needs to have a single one of such columns that also has to be centered, how can it be properly done with causing minimum impact on the bootstrap behavior?
I found that by using margins the bootstrap behavior gets messed up. (a backend developer asking this question, I'm not a UI expert)
HTML:
<div class="container">
<div class="row">
<div class="col-md-3 centercol" style="border:1px solid black;">
This is in the middle.
</div>
</div>
</div>
CSS:
.centercol {
margin:0 auto;
float:none;
}
See it in action: http://www.bootply.com/l7AArBXdlZ

dynamically created bootstrap columns with different heights, float to left

Consider an unknown number of divs being created dynamically and styled using the bootstrap grid system. In this example, I'm using col-sm-4 so after every third block, we move to a new row. The blocks (divs) can be different heights, which is determined by the content within.
This is where I run into the layout problem. When moving to a new row, I want the fourth block to float to the left. This only happens when the left most div in the row above is also the shortest. I have pictures to illustrate.
Real Life:
The Dream:
The "correct" way to do this would be to wrap every three in a row class I beleive, but I'm not sure how to do this with dynamic content (could probably hack it) or if there's an easy css solution.
HTML:
<div class="row">
<div class="col-sm-12">
<div class="col-sm-4 block">
<div class="inner-block"></div>
</div>
<div class="col-sm-4 block">
<div class="inner-block"></div>
</div>
<div class="col-sm-4 block">
<div class="inner-block" style="height:150px"></div>
</div>
<div class="col-sm-4 block">
<div class="inner-block"></div>
</div>
</div>
</div>
CSS:
.block {
padding: 5px;
}
.inner-block {
height: 200px;
background-color: blue;
}
Plunker Example (expand preview to proper size)
If your system is unable to add first/last classes on every nth div, then you can use the nth-child css pseudo selector.
#media (min-width: 768px) {// For medium displays and above
.col-sm-4:nth-child(3n+1) { // We target every 3rd div but offset the count by 1 so that that 1st, 4th 7th etc divs are cleared
clear:both; // Clear the float
}
}

How Can i Make a column to fill all the way to the sides in bootstrap

I may sound stupid, but this is all new to me.
I'm guessing I have overlooked something.I have no ideea how to fill the white spaces between my columns(end-to-end)
This is my code:
<div class="container" id="cfoot">
<div class="col-lg-3">
<h3>despre noi</h3>
<p>Pensiunea Delia</p>
<p>Echipa Noastra</p>
</div>
<div class="col-lg-3">
<h3>link-uri utile</h3>
<p>Intrebari frecvente</p>
<p>Serviciile noastre</p>
<p>Contact</p>
</div>
<div class="col-lg-3">
<h3>ultimele postari</h3>
<p>Titlul postare blog vine aici</p>
<p>Titlul postare blog vine aici</p>
<p>Titlul postare blog vine aici</p>
</div>
<div class="col-lg-3">
<img src="imgs/logodelia.png" alt="logobottom">
<p># 2014 Pensiunea Delia. Designed by Kinkara Web</p>
</div>
CSS:
#cfoot.container{
background-color:#f4f4f4;
color:#6c6c6c;
background-image:none;
}
Can anyone help please?
When I use developer tools to look at the markup, I'm seeing this applied by the browser:
body {
display: block;
margin: 8px;
}
If you simply add
body {
margin: 0;
}
.container {
margin: 10px; // adjust as needed
}
I think you'll be on your way.
note: you're also missing the Bootstrap row
<div class="row">
fiddle: http://jsfiddle.net/XEF8v/1/
I'm not quite clear on your question. I think you are asking us how you can use bootstrap to achieve the layout of four columns, like in the second image that you have posted.
You can get most of the way there by using Bootstraps built-in grid system.
Overview of Bootstrap's grid system: http://getbootstrap.com/css/#grid
<div class="container-fluid">
<div class="row">
<div class="col-md-1" id="col-1"><!-- empty space on left --></div>
<div class="col-md-2" id="col-2"><!-- despre noi column --></div>
<div class="col-md-2" id="col-3"><!-- link-uri-title column --></div>
<div class="col-md-2" id="col-4"><!-- ultimele column --></div>
<div class="col-md-4" id="col-5"><!-- delea logo column --></div>
<div class="col-md-1" id="col-6"><!-- empty space on right --></div>
</div>
</div>
The col-md-<#> class determines the horizontal width of a column. Per Bootstrap's documentation, these numbers should add up to 12.

Resources