Bootstrap div alignment issue - css

I'm having issue trying to align my div in Bootstrap 3.
Here's what I am trying to accomplish:
I have worked with pull and push but I guess I'm not good enough with it.
Code:
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3 blue">
blue
</div>
<div class="col-md-3 col-md-pull-9 red">
red
</div>
<div class="col-md-9 col-md-push-3 orange">
orange
</div>
<div class="col-md-3 col-md-pull-9 green">
green
</div>
<div class="col-md-3 col-md-pull-9 purple">
purple
</div>
</div>
</div>
http://jsfiddle.net/bva5z74w/1/

I was watching this to see if anyone would come answer the obvious: don't use bootstrap push and pull for this. Use float right and no floats. Blue must be taller than red at the min-width, other than that I think this will work smoothly.
DEMO: http://jsbin.com/degeju/1/
CSS:
.blue {
background: blue
}
.red {
background: red
}
.orange {
background: orange
}
.green {
background: green
}
.purple {
background: purple
}
.red,
.blue,
.green,
.orange,
.purple {
height: 50px
}
#media (min-width:992px) {
.blue {
height: 600px;
float: right;
}
.red {
height: 300px;
float: none;
}
.orange {
height: 400px;
float: right;
}
.green {
height: 200px;
float: none;
}
.purple {
height: 200px;
float: none;
}
}
HTML
<div class="container">
<div class="row">
<div class="col-md-9 blue">
blue
</div>
<div class="col-md-3 red">
red
</div>
<div class="col-md-9 orange">
orange
</div>
<div class="col-md-3 green">
green
</div>
<div class="col-md-3 purple">
purple
</div>
</div>
</div>

The HTML you have will work perfectly, just remove the pull on the purple block:
HTML:
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3 blue">
blue
</div>
<div class="col-md-3 col-md-pull-9 red">
red
</div>
<div class="col-md-9 col-md-push-3 orange">
orange
</div>
<div class="col-md-3 col-md-pull-9 green">
green
</div>
<div class="col-md-3 purple">
purple
</div>
</div>
</div>
http://www.bootply.com/dMNG75tSf0
Edit:
The float on the left elements is causing them to collapse to fit their contents. However, with media queries you can set the height to be the same height as the adjacent right element.
#media (max-width:1199px) {
.red {
height: 360px;
}
}
#media (min-width:1200px) {
.red {
height: 300px;
}
}
The obvious drawback is this limits the fix to browsers that support media queries. Therefore, Christina's answer is better, assuming it works consistently across browsers.

div has a display type "block", by default this means that each div will be shown below another div.
this means that someway you've changed some of the css, specifically with floats or display:inline.
if you're not sure how to find and change this you can always make sure each div has width:100%;
that will make sure the div catches the whole width of it's parent container.

Related

Sticky left cell in flexbox table on 'category' change

I have a list of items that I am presenting in a flexbox table. My actual display has more columns, but in this question, I'll use just 2 - the 'category' and 'item' names. The data is presented in category order. Where items repeat for a category, only the first item for the category should have the category listed, for subsequent ones I want the category cell empty. As this can scroll off the top of the screen, I'd like to make this cell sticky, as I do for the headings.
However, as the cell is a child of the row, not the container of the table, it scrolls up with the row. You can see this in the first table in the example below, the '.first_item' CSS class does not hold the caption in place.
I concluded that the only way to achieve this is to insert a div before the row to present the category and make this sticky - class cat_head in the 2nd table in the example below. This works, however, it knocks the first item down and does not align where I want it to. I therefore set its height to 0px, which brings the item row back into alignment, but then have to put the text within an absolute positioned div within this so that I can set a background colour to stop text for the categories from overlaying each other.
It works really well and is exactly how I'd like it to operate - except that because the height is set to 0px, the absolute div extends below the bottom of the table as it is scrolled off the top of the screen and overwrites what is below it - you can see in the example it overwrites "Text below".
I also have to manually add an 'alt' class name on alternate rows to get background colouring rather than relying on ':nth-child(even)' - but I can live with this.
Has anyone got any suggestions for stopping it falling out of the bottom of the container, or indeed a better way of doing this? Thanks.
.pad {
height: 50px;
background-color: yellow;
}
.fill {
height: 120vh;
background-color: yellow;
}
.container {
background-color:#ffffff;
}
.row {
display: flex;
flex-flow:row wrap;
}
.head {
position: sticky;
top: 0px;
background-color: #e0e0e0;
font-weight: bold;
border-bottom: 1px solid #000000;
height: 24px;
z-index: 3;
}
.cell {
flex: 0 0 100px;
padding: 5px;
}
.first_item {
position: sticky;
top: 25px;
z-index: 2;
}
.table_1 .row:nth-child(even),
.table_2 .alt {
background-color: #f0f0f0;
}
.cat_head {
position: sticky;
top: 25px;
height:0px;
overflow: visible;
z-index: 2;
}
.cat_text {
position: absolute;
width: 80px;
padding: 0;
background-color: rgba(8,156,213,1);
color: #ffffff;
border: 1px solid #000000;
border-radius:5px;
margin:2px 0 0 4px;
text-align:center;
}
<div class="pad"></div>
<div class="container table_1">
<div class="row head">
<div class="cell">Category</div>
<div class="cell">Item</div>
</div>
<div class="row cat">
<div class="cell first_item">Category 1</div>
<div class="cell">Item 1-1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-2</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-3</div>
</div>
<div class="row cat">
<div class="cell first_item">Category 2</div>
<div class="cell">Item 2-1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 2-2</div>
</div>
</div>
<div class="pad">Text below</div>
<div class="container table_2">
<div class="row head">
<div class="cell">Category</div>
<div class="cell">Item</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 1</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-2</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-3</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 2</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 2-1</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 2-2</div>
</div>
</div>
<div class="fill">Text below</div>
OK. So I went to sleep on it and realised that the sticky positioning when scrolling off the top of the viewport is based on the bottom of its div. Therefore, I need to shift the div below the row it is on - as its 0px high and that's where its 'bottom' should be.
I also need to move the contained absolute div above it rather than below so its bottom aligns to it. I initially put a -24px top margin on the absolute div, but this stopped the sticky positioning on its parent for some reason. I therefore put a -24px top margin on the sticky div, which worked. However, it also dragged the next row up, so I also added a 24px bottom margin which resolved that.
I also added a containing div around each category's rows so that the sticky heading scrolls off screen when the next category gets to the top.
All working fine.
.pad {
height: 50px;
background-color: yellow;
}
.fill {
height: 120vh;
background-color: yellow;
}
.container {
background-color:#ffffff;
}
.row {
display: flex;
flex-flow:row wrap;
}
.head {
position: sticky;
top: 0px;
background-color: #e0e0e0;
font-weight: bold;
border-bottom: 1px solid #000000;
height: 24px;
z-index: 3;
}
.cell {
flex: 0 0 100px;
padding: 5px;
}
.alt {
background-color: #f0f0f0;
}
.cat_head {
position: sticky;
top: 27px;
height:0px;
margin: -24px 0 24px 4px;
overflow: visible;
z-index: 2;
}
.cat_text {
position: absolute;
width: 80px;
padding: 0;
background-color: rgba(8,156,213,1);
color: #ffffff;
border: 1px solid #000000;
border-radius:5px;
text-align:center;
}
<div class="pad"></div>
<div class="container">
<div class="row head">
<div class="cell">Category</div>
<div class="cell">Item</div>
</div>
<div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-1</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-2</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-3</div>
</div>
</div>
<div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 2-1</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 2</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 2-2</div>
</div>
</div>
</div>
<div class="fill">Text below</div>

Bootstrap complex column position ordering

Is it possible to pull/push divs of a column from another column in Bootstrap grid system? For example:
At desktop view: it's like this way:
Parent Column 1 | Parent Column 2
First Content | Second Content
Third Content | Fourth Content
At mobile view, it's like this way:
Parent Column 1
First Content
Second Content
Parent Column 2
Third Content
Fourth Content
Please take a look on this fiddle to understand what I am trying to mean.Thanks in advance.
You can achieve it by using bootstrap's grid.
.first {
background: yellow;
height: 300px;
}
.second {
background: red;
height: 100px;
color: white;
}
.third {
background: green;
height: 300px;
color: white;
}
.fourth {
background: blue;
height: 100px;
color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 col-xs-12 first">
FIRST
</div>
<div class="col-sm-6 col-xs-12 second">
SECOND
</div>
<div class="col-sm-6 col-xs-12 third">
THIRD
</div>
<div class="col-sm-6 col-xs-12 fourth">
FOURTH
</div>
</div>
</div>
Or you can use Masonry JS:
var $container = $('.masonry-container');
$container.masonry({
columnWidth: '.item',
itemSelector: '.item'
});
.first {
background: yellow;
height: 300px;
}
.second {
background: red;
height: 100px;
color: white;
}
.third {
background: green;
height: 300px;
color: white;
}
.fourth {
background: blue;
height: 100px;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.1.1/masonry.pkgd.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row masonry-container">
<div class="col-sm-6 col-xs-12 first item">
FIRST
</div>
<div class="col-sm-6 col-xs-12 second item">
SECOND
</div>
<div class="col-sm-6 col-xs-12 third item">
THIRD
</div>
<div class="col-sm-6 col-xs-12 fourth item">
FOURTH
</div>
</div>
</div>
I think the issue is more of a float problem because your columns have variable height. Create a special class to pull the 2nd and 3rd columnd right on larger screens..
#media (min-width: 992px) {
.pull-md-right {
float:right;
}
}
And, use it like this..
<div class="row">
<div class="col-md-6">
<div class="first">First</div>
</div>
<div class="col-md-6 pull-md-right">
<div class="second">Second</div>
</div>
<div class="col-md-6 pull-md-right">
<div class="third">Third</div>
</div>
<div class="col-md-6">
<div class="fourth">Fourth</div>
</div>
</div>
http://codeply.com/go/CYteNdheKS
In Bootstrap 4, responsive floats are included so you won't need the extra CSS.

Bottom container stacking ontop of middle container

I currently have two containers. One of the containers has two divs stacking ontop of each other (a img and text). That is fine. But it seems the bottom container is stacking ontop of the 1st container.
<div class="container">
<div id='map'>
<div id='image'>
<img src="/static/usa.png">
</div>
<div id='states'>
<div class="col-md-12">
<center>
<h2>Select Your State</h2>
Select your state from the list below to find information specific to your area.
</center>
</div>
<div class="col-md-2">
state
</div>
<div class="col-md-2">
state
</div>
<div class="col-md-2">
state
</div>
<div class="col-md-2">
state
</div>
<div class="col-md-2">
state
</div>
<div class="col-md-2">
state
</div>
</div>
</div>
</div>
<div class="container">
<center>
<h1>Information on ...</h1>
This site is a beta framework
</center>
</div>
and my css file is
img {
width:100%;
opacity: 0.4;
}
#map { position: relative; }
#image {position: absolute;}
#states {
position: absolute;
}
span {
display: inline-block;
vertical-align: middle;}
a {
color: black;
}
try adding clear:both; to the bottom container
I answered it by instead of trying to stack the images just setting it to a background image static.
body {
background-image: url("/static/usa.png");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}

Floating divs being pushed down

I'm so unsure of why this is happening, I'm having a lot of trouble finding a solution, so here it is plain and simple
HTML
<div class="thing"></div>
<div id="big" class="thing"></div>
<div class="thing"></div>
<div class="thing"></div>
<div class="thing"></div>
<div id="big" class="thing"></div>
<div class="thing"></div>
CSS
.thing {
background-color: blue;
height: 100px;
width: 80px;
margin: 20px 0 0 20px;
display: inline-block;
float: left;
}
#big {
height: 140px;
}
JSFiddle: https://jsfiddle.net/sdmq155g/1/
The blue divs on the bottom shouldn't be pushed down unless there is a big div above them. They should all nestle right up underneath the div above them. But for some reason, all their positions are based off of the bigger divs in the top row, even though they're not all directly below one.
The box model that all browsers implement behaves in exactly the way your jsFiddle demonstrates. The blue divs won't nestle up underneath each other, but in fact they will start on the next line based on the tallest item on the row above.
You could overcome this by placing the divs you'd like to stack in a column, then floating these divs.
Example here:
.col {
float: left;
width: 80px;
}
.thing {
background-color: blue;
border: solid 1px red;
height: 100px;
}
#big {
height: 140px;
}
<div class="col">
<div class="thing"></div>
<div id="big" class="thing"></div>
</div>
<div class="col">
<div id="big" class="thing"></div>
<div class="thing"></div>
</div>
<div class="col">
<div class="thing"></div>
<div id="big" class="thing"></div>
</div>
<div class="col">
<div class="thing"></div>
<div id="big" class="thing"></div>
</div>
<div class="col">
<div class="thing"></div>
<div id="big" class="thing"></div>
</div>

Twitter Bootstrap Banner - How to render

This is probably a pretty basic question, but I have a banner with an image on the left and text on the right. Under the banner is just a block of color. When the page gets smaller, my expectation is that the bits in the banner would stack (maintaining the background color for both) and the block of color (class="blue-line") would fall beneath them.
Here is the mark-up:
<section>
<div class="row header">
<div class="col-sm-6">
<img src="../images/logo.png" height="100px" />
</div>
<div class="col-sm-6 title">
<h2>Some Title Text</h2>
</div>
</div>
<div class="row">
<div class="col-sm-12 blue-line"></div>
</div>
</section>
and the css
.header {
background-color: #F2EBCC;
border: 10px solid #F2EBCC;
height: 120px;
}
.row > .title {
text-align: right;
top: 45%;
}
Thanks in advance!
JSFiddle: http://jsfiddle.net/3n6Kd/
try this:
<section>
<div class="row header">
<div class="col-sm-6">
<img src="../images/logo.png" height="100px" />
</div>
<div class="col-sm-6 title">
<h2>Some Title Text</h2>
</div>
</div>
<div class="row">
<div class="col-sm-12 blue-line"></div>
</div>
and:
.header {
background-color: #F2EBCC;
height: 120px;
}
.title {
text-align: right;
display: block;
padding: 10px;
}
.blue-line {
margin-top:10px;
height: 15px;
background-color: blue;
}
the text go under the first column not the blue-line, but it seems to appear above the blue-line so try it in your computer because some time jsfiddle.net don't show code correctly.
hope it will help you.

Resources