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

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.

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

Bootstrap - resize specific column

I don't know how to make this kind of col 3 and 6 size.
Middle column has no padding, but it is not enough.
I was trying to make different sizes of col.
#media (min-width:992px){
.col-md-6 { width: 52,5641%;}
.col-md-3 { width: 23,7179%;}
}
but no success.
With Bootstrap you dont need to add media queries or your own width, just use the BS grid system (you can read more here) and let it handle all the tough work. Based on your picture a 3 column layout would use something like this:
<div class="row">
<div class="col-md-3">.col-md-3</div>
<div class="col-md-6">.col-md-6</div>
<div class="col-md-3">.col-md-3</div>
</div>
Make sure you columns total 12 like above (3+6+3) If you need extra padding in between columns just add a nested DIV and apply the spacing you want to those.
<div class="row">
<div class="col-md-3">
<div class="myclass">
this will have extra padding
</div>
</div>
<div class="col-md-6">.col-md-6</div>
<div class="col-md-3">.col-md-3</div>
</div>
.myclass {
padding: 20px;
}
Updated
Based on your comment if you want column 6 to be slightly larger than it is you will either need to expand that column and "shrink" the outer 2 columns to something like this:
<div class="row">
<div class="col-md-2">.col-md-2</div>
<div class="col-md-8">.col-md-8</div>
<div class="col-md-2">.col-md-2</div>
</div>
If that's not what you are going for then you can create your own table within bootstrap.
<div class="row">
<div class="custom-col">My custom left side</div>
<div class="custom-main">my main column</div>
<div class="custom-col">My custom right side</div>
</div>
Sizing each of the column as you need.
Maybe Bootstrap is not the best option for your problem. It works if only you can divide the screen in 12 equal parts. Rewrite this rule could break other stuff.
What about using flexboxes or other CSS framework more flexible?

Display non equal height element with bootstrap

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/

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
}
}

Twitter Bootstrap: make a 2x2 grid

I have the following:
<div class="container-fluid">
<div class="row-fluid">
<div class="span3"></div>
<div class="span3"></div>
<div class="span3"></div>
<div class="span3"></div>
</div>
</div>
By default, this div.span* spans the entire width of the screen, like this:
[x][x][x][x]
At a certain screen width, I want this to appear in a 2x2 grid, like this:
[x][x]
[x][x]
How do I do this?
Sorry about my earlier attempts, I did not fully understand your question:
The thing which you are trying with bootstrap is not really possible unless you go for your own #media selectors. There is a library called Neat. I think this is the example you are looking for.
EARLIER ATTEMPTS:
Try this, from here:
<div class="container-fluid">
<div class="row-fluid">
<div class="span6">
<div class="row-fluid">
<div class="span6">A</div>
<div class="span6">B</div>
</div>
<div class="row-fluid">
<div class="span6">C</div>
<div class="span6">D</div>
</div>
</div>
</div>
</div>
This should give you the following result:
[A][B]
[C][D]
Well that's a lot of divs. Not really sure if this can be made lighter.
The original question appears to be for an older edition of bootstrap.
Here's what solves the issue neatly in Bootstrap 3 markup. The key element is the clearfix div that affects xs and sm viewports [typical use case]. (sm not included in example below).
<div class="row">
<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
<!-- Add the extra clearfix for only the required viewport -->
<div class="clearfix visible-xs-block"></div>
<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
</div>
via getbootstrap.com
Here are 2 options that are responsive without the need for media queries. Resize the windows to see how they react.
CSS Columns:
http://jsfiddle.net/88t4L/
.row-fluid {
columns: 2 8em;
}
Here, the columns must be at least 8em wide, but if there's room for all of them to appear in a row, it will do so.
http://caniuse.com/#search=columns
CSS Flexbox:
http://jsfiddle.net/88t4L/1/
.row-fluid {
display: flex;
flex-flow: row wrap;
}
.row-fluid .span3 {
flex: 1 0 8em; /* grow equally, don't shrink, preferred width of 8em */
}
http://caniuse.com/#search=flexbox

Resources