Reorder divs on media query with line break - html

Here is a fiddle to essentially what I want to do - reorder divs using floating to float the middle div to the left on smaller screen sizes, and the outer two to the right.
Here's the current order:
<div id='one'></div>
<div id='two'></div>
<div id='three'></div>
In addition to what's already there, I want the outer two divs to sit underneath the middle div on the left. Essentially I want it to look like:
<div id='two'></div>
<div id='one'></div>
<div id='three'></div>
Where div two has display:block, and display one/three have display:inline-block - without changing the html.
Is this possible with just CSS?

This can be done:
<div class="boxes">
<div class="one box">Box One</div>
<div class="two box">Box Two</div>
<div class="three box">Box Three</div>
<div class="four box">Box Four</div>
</div>
CSS:
#media only screen and (max-width:768px) {
.boxes {
display:table;
width:100%;
}
.box {
display:block;
width:100%;
}
.three { display:table-caption; }
.four { display:table-header-group; }
.one { display:table-footer-group; }
}
Source: http://www.iandevlin.com/blog/2013/06/css/css-stacking-with-display-table (check the demo linked)

Related

Bootstrap align rows of different sizes

Ran into a problem when it comes to bootstrap columns being consistent in size, and the way they look on smaller devices: JSFiddle
<div class="well info">
<div class="row row-centered">
<div class="col-sm-2 col-sm-offset-1 col-centered">
<div class="count row-centered">
Column 1
</div>
<div class="row-centered">
Text 1
</div>
</div>
<div class="col-sm-2 col-centered">
<div class="count row-centered">
Column 2
</div>
<div class="row-centered">
Text 1
</div>
</div>
<div class="col-sm-2 col-centered">
<div class="count row-centered">Column 3</div>
<div class="row-centered">Text 1</div>
</div>
<div class="col-sm-2 col-centered">
<div class="count row-centered">Column 4</div>
<div class="row-centered">Text 4</div>
</div>
<div class="col-sm-2 col-centered">
<div class="count row-centered"><b>Column 5</b>
<br>
</div>
<div class="count row-centered">Text 1</div>
<div class="row-centered">Text 3</div>
<div class="row-centered">Text 4</div>
</div>
</div>
</div>
</div>
</div>
CSS
.row-centered {
text-align:center;
}
.col-centered {
display:inline-block;
float:none;
/* reset the text-align */
text-align:left;
/* inline-block space fix */
margin-right:-4px;
}
Essentially, given a setup such as the one outlined, how does one go about making sure that:
The row containing the elements is vertically centered depending on the size of the parent container.
The columns making up the row are all centered vertically to each other, despite potentially being different sizes.
The items in each column are aligned horizontally (centered).
The structure of the columns and rows on collapse (with smaller devices) isn't scrambled, and would appear with the columns displayed underneath one another.
I have tried several arrangements of nested rows, columns, and divs, but seemingly cannot get the columns to be vertically aligned, despite applying the appropriate classes (in my mind).
Is it possible to do so using a combination of CSS + Bootstrap?
Any advice and help is appreciated!
CSS (added vertical-align: middle and media query):
.row-centered {
text-align: center;
}
.col-centered {
/* reset the text-align */
text-align: left;
}
#media (min-width: 768px) {
.col-centered {
display: inline-block;
float: none;
/* inline-block space fix */
margin-right: -4px;
vertical-align: middle;
}
}
JSFIDDLE
Use this class on the column parent to make it centered with the other columns
.align-vertically {
display: flex;
align-items: center;
}
Use the same class on the column itself to make its content vertically centered
Use text-center on the column to align the text, and center-block on the children containers to make them aligned horizontally
Read up on the bootstrap grid system. In your case use col-xs-12 for full width of each container on smaller devices.
CODEPEN

bootstrap pulling/pushing 12 columns not working properly

I am trying to have 2 columns inverted in middle (up) resolutions.
Here is my code on bootply that you can see here as well:
<div class="container">
<div style="border:1px solid red;">
<div class="row">
<div class="col-md-12 col-md-push-12"><div style="background:yellow;">in xs this first</div></div>
<div class="col-md-12 col-md-pull-12"><div style="background:orange;">in xs this after</div></div>
</div>
</div>
</div>
As you can see, in xs screen, the order is the one written on screen.
What I wanted is that on bigger resolutions, the order should be inverted. Even if I push/pull a number of 12 columns (to have them inverted), the result is what you see.
Do you have any suggestion?
Thank you
Bootstarp pulling pushing need proper column number like below
div style="border: solid 1px red">
<div class="row">
<div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
<div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
</div>
</div>
Refer here
If your interested in doing without bootstrap than here's a JSFiddle
You can call any classes and use wisely with Media Queries
HTML:
<div class="parent">
<div class="child-1">
<p>First</p>
</div>
<div class="child-2">
<p>Second</p>
</div>
</div>
CSS:
.parent{
display:table;
width: 100%;
}
.parent .child-1{
display: table-footer-group;
background:yellow;
}
.parent .child-2{
display: table-header-group;
background:green;
}
It's not really possible to reverse full-width (12) columns using Bootstrap push pull class. You'd have to use the responsive utility classes which requires duplicate content.
Or, use a CSS media query and transform to reverse the order on xs...
#media (max-width: 768px) {
.row.switch-xs {
transform: rotate(180deg);
direction: rtl;
}
.row.switch-xs > [class^="col-"] {
transform: rotate(-180deg);
direction: ltr;
}
}
Demo of both methods: http://bootply.com/nT8kUqB6d8
Update 2017
Now it's possible to reverse order of full-width columns on XS.
For Bootstrap 4, no extra CSS is necessary for this. Simply use the flexbox ordering classes to "pull up" the 2nd div on xs screens:
<div class="row text-center">
<div class="col-md-12">
<div style="background:orange;">last on xs</div>
</div>
<div class="col-md-12 flex-first flex-md-unordered">
<div style="background:yellow;">first on xs (pull me up)</div>
</div>
</div>
http://codeply.com/go/NV4mTbRKpC

Bootstrap column to extend out side of the container

im trying to use bootstrap for a site, but what i am trying to achieve to have to columns within a row col-sm-6. Where i want one column to be in an ordinary container and the other to have container fluid effect.
So it would one column as normal and the other column as full width. So far this has been difficult to do:
<div class="container">
<div class="row">
<div class="col-sm-6 first">
</div>
<div class="col-sm-6 second">
</div>
</div>
</div>
http://jsfiddle.net/8fyjtn09/
i think i figured it out, instead of using a normal container i would have to use container fluid as i said before but just an offset
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-2 col-xs-4 first">
</div>
<div class="col-sm-6 second">
</div>
</div>
</div>
Do you know flex-box layout in CSS ?
As i understand your question, i believe that on wider screens you want to have first column fixed and second column should be responsive. And for a smaller screen you want the default bootstrap behaviour. If this is what you are looking for, i have created a simple fiddle. Please check that and let me know if this is helpful.
Here is the link : https://jsfiddle.net/YameenYasin/7zaxx06z/23/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<div class="container">
<div class="row flexbox">
<div class="first">
</div>
<div class="second">
</div>
</div>
</div>
.flexbox {
display: -webkit-flex;
-webkit-flex-flow: row; // Children elements will be in rows
-webkit-flex-wrap: wrap;
width: 100%;
}
.first
{
border:1px solid red;
height:50px;
width:200px;
}
.second{
border:1px solid green;
width:100%;
-webkit-flex: 1;
height:50px;
}
#media (max-width: 767px) {
.flexbox {
-webkit-flex-flow: column;
}
.first,.second{
width:100%;
}
}

4 divs and need to center 2 center divs

I have four divs inside a container like this:
<div class="container">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
I would like to keep the first and last div aligned to the edges of the screen and only center the two divs in the middle. I tried display inline block and adjusting the margins, but i just can't figure it out. Please try and enlighten me!
Kind Regards
Use this HTML
<div class="container">
<div class="block left">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block right">4</div>
</div>
and then this CSS
.container {
display:block;
text-align:center
}
.block {
display:inline-block;
border:1px solid #000;
padding:10px;
margin:auto
}
.left {
float:left
}
.right {
float:right;
}
you can also use first-child and last-child, but it's easier to add a class to first and last div
See fiddle here
Try using :nth-of-type() pseudo-class allows you to select elements with a formula,like this: http://jsfiddle.net/csdtesting/11fh7suy/
//second div
div.container div:nth-of-type(2) {
background-color: #9999ff;
text-align:center;
}
//third div
div.container div:nth-of-type(3) {
background-color: #9999ff;
text-align:center;
}
Hope it helps!
Adding an extra container will give you more control:
<div class="container">
<div class="block">1</div>
<div class="wrap">
<div class="block">2</div>
<div class="block">3</div>
</div>
<div class="block right">4</div>
</div>
.wrap{width:50px;margin:0 auto}
.block{float:left;width:25px;text-align:center}
.right{float:right}

Arranging rows with alternating counts of items with CSS in a responsive layout

Running into a strange issue trying to arrange items that are placed in multiple rows, each row containing either 5 or 4 items (alternating 5 items, 4 items, 5 items, etc...). However, at 480px screen width, the layout switches simply to 2 items wide no matter how many in each row. This creates a layout issue between the 4-item rows and 5-item rows. When they're stacked on top of each other the rows with 4 work fine (2x2), but the rows of 5 have a hanging item/blank spot at the end of the box (2x2x1).
4/5 Row Layout:
2x2 row layout (with hanging item issue)
HTML:
<section id="rowArea">
<div class="row fiveRow clearfix">
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
</div>
<div class="row fourRow clearfix">
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
</div>
<div class="row fiveRow clearfix">
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
<div class="rowItem"></div>
</div>
</section>
CSS:
/* Screen Width > 480px */
#rowArea {width:810px; height:auto; }
.row {height:100px; }
.fourRow {width:80%; padding:0 10%; }
.fourRow .rowItem {width:25%; height:100px; float:left; }
.fiveRow {width:90%; padding:0 5%; }
.fiveRow .rowItem {width:20%; height:100px; float:left; }
/* Screen Width < 810px */
#rowArea {width:100%; height:auto; }
/* Screen Width < 480px */
#rowArea {width:100%; height:auto; }
.row {height:100px; }
.fourRow, .fiveRow {width:100%; padding:0 0; }
.fourRow .rowItem, .fiveRow .rowItem {width:50%; height:100px; float:left; }
How do i restructure this/style this to eliminate the blank spot at the end of the 5-item rows? Any help is much appreciated!
It's probably the clearfix class that is causing those gaps. A clearfix class is meant to force an element to contain any floated child elements. Remove that class, and set your mobile css on .row to height: auto and the child elements should float against each other.
basic example: http://jsfiddle.net/gv5jL/1/