Changing divs order based on width [duplicate] - css

This question already has an answer here:
Change div order with CSS depending on device-width
(1 answer)
Closed 3 years ago.
I've got 3 divs in a wrapper side by side, using:
<div id="left"><h1>title left</h1></div>
<div id="right"><h1>title right</h1></div>
<div id="center"><img src="img/titleimage.jpg" alt=""/></div>
aligned like this with css:
#left{
width:250px;
float:left;
margin:200px auto;
position:relative;
}
#right{
width:250px;
float:right;
position:relative;
margin:200px auto;
}
#center{
margin:60px auto;
margin-bottom:0;
width:500px;
position:relative;
float:left;
}
I would like for the divs to reorder when the browser window becomes smaller. I would like them to appear top to bottom like this :
LEFT
RIGHT
CENTER
or even better
CENTER
LEFT
RIGHT
Any ideas?

Move the center div all the way to the top
<div id="center"><img src="img/titleimage.jpg" alt=""/></div>
<div id="left"><h1>title left</h1></div>
<div id="right"><h1>title right</h1></div>

I think the key here is to think about this from a small-screen-first approach.
If your project can use flexbox, that is something you could work with and change the order of div's with CSS, but I am betting that is not the case. I think you are going to have to use a little absolute positioning once you get to a larger screen to get this working. Here is an example: and a fiddle
HTML
<div class="container">
<div class="inner-w">
<div class="block center">Center</div>
<div class="block left">Left</div>
<div class="block right">Right</div>
</div>
</div> <!-- .container -->
CSS
* {
box-sizing: border-box;
}
.container {
width: 100%;
float: left;
overflow: hidden; /* should be clearfix instead */
}
.container .inner-w {
position: relative;
max-width: 50em;
margin-right: auto;
margin-left: auto;
overflow: hidden; /* should be clearfix instead */
}
.block {
width: 100%;
float: left;
min-height: 10em; /* just for show */
}
#media (min-width: 50em) {
.center {
width: 50%;
position: relative;
left: 25%;
}
.left {
position: absolute;
top: 0;
left: 0;
width: 25%;
}
.right {
float: right;
width: 25%;
}
} /* end break-point */

Related

Centering the middle of three divs and positioning the other two relative to the middle one

Sorry if the title is confusing. Basically, I'm working on a tumblr theme where I need three adjacent divs wrapped in a fixed-width container. None of their contents are fixed, so they all have variable widths. The middle div should always be centered to the container, while the divs to the left and right will always be "touching" the middle div, and, thus, move around as the middle div's width changes (the left and right s may be images, so text-align doesn't always work). Plus, I may also need to hide the left, right, or both the left and right divs.
Here's a conceptual image:
I can obtain this using flexboxes easily (JFiddle), but flex only has 86% global support.
This is the closest I could get without using flexboxes, but I can't get that middle div (with the text) centered to the title div, while preserving the relative positions of the two images on either side: JFiddle
* {
height: 100%;
position: relative;
}
body {
height: 200px;
}
/* just to get rid of scrollbar */
p {
margin: 0;
}
.title {
background: #aaa;
height: 22px;
width: 450px;
/* for example */
margin: 0 auto;
}
.container {
background: #abc;
float: left;
}
.lr {
transform: translate(0, -100%);
}
.left {
background: green;
float: left;
}
.left img {
transform: translate(-100%);
}
.center {
background: red;
display: inline-block;
z-index: 2;
}
.right {
background: blue;
float: right;
}
.right img {
transform: translate(100%);
}
.left img, .right img {
height: 100%;
}
<div class="title">
<div class="container">
<div class="center">CENTERCENTERCENTERCEN</div>
<div class="lr">
<div class="left">
<img src="http://i.imgur.com/7bvErJN.jpg" />
</div>
<div class="right">
<img src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
</div>
</div>
Other people have mentioned trying to display the title as a table, but that would require centering the middle cell to the whole row, and having the cells to the left and right take up the rest of the space, and I'm not sure if you can do that when their widths aren't fixed.
Anyone know of any other solutions?
If you can change your HTML then apply this:
First move the left and right elements inside center:
<div class="center">
CENTERCENTERCENTERCEN
<div class="left">
testtest<img src="http://i.imgur.com/7bvErJN.jpg" />
</div>
<div class="right">
<img src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
Then on the CSS :
/*Keep the center container on the middle*/
.title {
text-align:center;
}
.center {
position:relative;
display:inline-block;
}
/*Position elements based on the relative center parent*/
.left {
position:absolute;
top:0;left:0;
transform:translateX(-100%)
}
.right {
position:absolute;
top:0;right:0;
transform:translateX(100%)
}
Check this DemoFiddle
Using position: absolute should help in this.
I changed your HTML to following:
<div class="title">
<div class="container">
<img class="left" src="http://i.imgur.com/7bvErJN.jpg" />
<div class="center">CENTERCENTERCENTERCEN</div>
<img class="right" src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
CSS
.title {
background: #aaa;
height: 22px;
width: 450px;
/* for example */
margin: 0 auto;
text-align: center;
}
.container {
background: #abc;
display: inline-block;
margin: 0 auto;
position: relative;
text-align: left;
}
.center {
background: red;
}
.left, .right {
position: absolute;
top: 0px;
}
.left {
right: 100%;
}
.right {
left: 100%;
}
Working Fiddle
Updated to show OP Update
No need for flex here, why not just use percentages? Float all the containers and put the percentages as relative to the sizes you want. (50% for the middle, 25% for the outside containers).
You can use the outside containers as wrappers so you can still use a border on the inner containers without messing up the sizing. Then just float the inner containers within the outside containers (if that makes sense). The example below just floats the inner p tags to the outer containers.
This makes it always hug the inner container, while keeping relative sizes and also keeping the middle centered.
Example below:
Fiddle
HTML
<div class="container">
<div class="flexa">
<div class="left">
<p>leftleft</p>
</div>
<div class="center"><p>CENTERCENTdsfdfdERCENTsdfdfsfERCEN</p></div>
<div class="right">
<p>ri</p>
</div>
</div>
<div class="bottom">BOTTOMOMOM</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
div {
background: #aaaaaa;
overflow: hidden;
}
p{
border: 1px solid black;
}
.container {
width: 500px;
/* for example */
margin: 0 auto;
}
.right p{ /* This is what makes it work. This could be a div with class of inner or something like that. */
float:left;
}
.left p{
float:right;
}
.flexa div{
float:left;
}
.left {
width:25%;
}
.center {
width: 50%;
}
.right {
width:25%;
}
.bottom {
display: block;
margin: 0 auto;
text-align: center;
}

Place three DIV boxes horizontally and centered

I'd like to place three DIV boxes horizontally and centered. If i resize (narrower) the browser boxes should take place vertically and centered.
----BrowserWide-----
______X X X______
----BrowserWide-----
----BrowserNarrow-----
________X_________
________X_________
________X_________
----BrowserNarrow-----
This is my html:
<div class="premium_features">
<div class="premium1">
<h2>Some Heading</h2>
<p>Some paragraph, Some paragraph, Some paragraph, Some paragraph, Some paragraph,
</p>
</div>
<div class="premium2">
<h2>Some Heading</h2>
<p>Some paragraph, Some paragraph, Some paragraph, Some paragraph, Some paragraph,
</p>
</div>
<div class="premium3">
<h2>Some Heading</h2>
<p>Some paragraph, Some paragraph, Some paragraph, Some paragraph, Some paragraph,
</p>
</div>
</div>
This is my css:
.premium1 {
background: url("4.png") no-repeat top center;
padding-top: 95px;
float:left;
width: 33%;
min-width: 300px;
max-width: 320px;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
.premium2 {
background: url("5.png") no-repeat top center;
padding-top: 95px;
float:left;
padding-top: 95px;
float:left;
width: 33%;
min-width: 300px;
max-width: 320px;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
.premium3 {
background: url("6.png") no-repeat top center;
padding-top: 95px;
float:left;
width: 33%;
height: 100px;
min-width: 300px;
max-width: 320px;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
.premium_features {
width: 75%;
margin-left: auto;
margin-right: auto;
}
With this code: It is OK when it is wide and when it is narrow.
But during resize 2 of 3 boxes are staying at the same block for a while. I need to sort them vertically when resizig starts.
Thanks for help.
There are many ways to centre elements:
Margin way:
With a set width or display: inline-block; you can use:
margin-left: auto;
margin-right: auto;
text-align way:
With a set width or display: inline-block; you can add this to the parent:
text-align: center;
Absolute way:
position: absolute;
left: 50%;
margin-left: width/2;
or
position: absolute;
left: 50%;
transform: translate(0, -50%);
Also don't worry too much about ie7 and below as the the majority of people use higher versions of ie or a different browser though this should work up until ie6
Place your three divs within one large div and style that div
#largeDiv{ margin : 0 auto 0;}
I'm assuming this is what you meant:
HTML:
<div id="longone">long one</div>
<center>
<div id="box"><img src="http://placehold.it/100x100"></div>
<div id="box"><img src="http://placehold.it/100x100"></div>
<div id="box"><img src="http://placehold.it/100x100"></div>
</center>
CSS:
#longone
{
display: block;
width: 100%;
border-bottom: 1px solid black;
}
#box
{
display: inline-block;
width: 100px;
}
http://jsfiddle.net/R9ENg/
Although, personally, I'd use <ul>s and <li>s.
Are you looking for something like this?
http://jsfiddle.net/a2cPu/
HTML
<div class="container-wrapper">
<div class="container-1"></div>
<div class="container-2"></div>
<div class="container-3"></div>
</div>
CSS
.container-wrapper {
text-align:center;
}
.container-1, .container-2, .container-3 {
width:200px;
height:200px;
display:inline-block;
margin-left:-4px;
}
.container-1 {
background:red;
margin-left:0;
}
.container-2 {
background:green;
}
.container-3 {
background:blue;
}
#media all and (max-width: 650px) {
.container-1, .container-2, .container-3 {
width:100%;
display:block;
margin:0;
}
}
you can use mediaqueries and test screenwidth to apply different style :
example mediaquerie + display:flex;
demo: http://codepen.io/anon/pen/qmFlt/
#container {
display:flex;
flex-direction:row;
justify-content:center;
}
#container div {
width:200px;
height:100px;
background:gray;
margin:auto 1em;
}
/* next block, #media, can be clone to set other media queries */
#media only screen
and (max-width : 800px) {/* under 800px width , this CSS is overriding precedent rules */
#container {
flex-direction:column;
}
#container div {
margin:1em auto;
}
<div id = "container">
<div>box
</div>
<div>box
</div>
<div>box
</div>
</div>
You can do it setting display:inline-block / block the #container div and drop the display flex rules for #container, keep margins for the 3 div .
demo: http://codepen.io/anon/pen/utflx/

Distribute objects vertically when viewport changes

I've been sitting at this problem now for about 4h. Way to long I suppose. So here I am:
I would like to distribute div containers vertically as soon as the viewport exceeds a specific height. Here's a sketch of an example.
HTML:
<div class="bubu">
<div class="center1"></div>
<div class="center2">
<div class="element"></div>
</div>
<div class="center3"></div>
<div class="center4">
<div class="element"></div>
</div>
<div class="center5"></div>
</div>
CSS:
* {
margin:0;
padding:0
}
body {
margin:0;
text-align:center;
background: no-repeat fixed center center #030303;
allowtransparency:true
}
.bubu {
background-color:#eee;
position: absolute;
height: 100%;
width:500px;
left: 50%;
margin-left: -275px;
/* width / 2 */
}
.center1 {
background-color:red;
position: relative;
height: 10%;
width:100%
}
.center2 {
background-color:yellow;
position: relative;
height: 35%;
width:100%
}
.center3 {
background-color:red;
position: relative;
height: 10%;
width:100%
}
.center4 {
background-color:yellow;
position: relative;
height: 35%;
width:100%
}
.center5 {
background-color:red;
position: relative;
height: 10%;
width:100%
}
.element {
background-color:#123456;
position: absolute;
height: 250px;
width:500px;
top:50%;
margin-top: -125px;
/* width / 2 */
}
Since margin:auto 0; will not do the job (will convert to 0 in height) I tried all different kinds of solutions. This (jsfiddle) is the one that only came close to it.
What I did was basically to add five classes, three of them height:10%; and two of them containing my containers height:35%;
Everything surrounded by one container height:100%;
As you can see, every time the container expands (my example size) off 500px the center expands twice.
How on earth can I solve this??
I assume you want to do some responsive design.
So what about using bootstrap?
It has a very flexible Grid system and it works out of the box on the most devices.

Three-Column Layout... left (fixed px), center (50%), right (50%)

I want the left column to be 40px. I want the center column to be 50% of the remaining viewport and I want the right column to be the other 50% of the remaining viewport.
It should look something like this:
[LEFTCOLUMN][...CENTER COLUMN...][...RIGHT COLUMN....]
[...40px...][........50%........][........50%........]
The solution presented here (link) will not work for my case as the center column can become too collapsed on mobile devices.
Thanks!
I think this may work for you:
http://jsfiddle.net/KR9zj/
Essentially the trick is to float LEFTCOLUMN, and wrap both CENTERCOLUMN AND RIGHTCOLUMN in a wrapper with overflow: hidden.
Use display:table; and display:table-cell;. No need to struggle with float:x;.
HTML:
<div id='container'>
<div id='first'>a</div>
<div id='second' class='fifty'>b</div>
<div id='third' class='fifty'>c</div>
</div>​
CSS:
#container { display:table; width:100%; }
#container > * { display:table-cell; }
#first { width:40px; min-width:40px; }
#container .fifty { width:50%; }
Live example: http://jsfiddle.net/j25wK/
Will this work?
Demo: http://jsfiddle.net/BVhCZ/
As you can see, the left is absolute, and "remaining" is one block div that containing two 50% floated children. Should work for any width >~ 40px
Code:
<div class="left">LEFT</div>
<div class="content">
<div class="content-left">CONTENT LEFT</div>
<div class="content-right">CONTENT RIGHT</div>
</div>
.left {
position: absolute;
top: 0;
left: 0;
width: 40px;
background-color: #ddd;
}
.content {
margin-left: 40px;
}
.content .content-left {
float: left;
width: 50%;
clear: none;
background-color: #fdd;
}
.content .content-right {
float: right;
width: 50%;
clear: none;
background-color: #ddf;
}

Two divs, one fixed width, the other, the rest

I've got two div containers.
Whilst one needs to be a specific width, I need to adjust it, so that, the other div takes up the rest of the space. Is there any way I can do this?
.left {
float: left;
width: 83%;
display: table-cell;
vertical-align: middle;
min-height: 50px;
margin-right: 10px;
overflow: auto;
}
.right {
float: right;
width: 16%;
text-align: right;
display: table-cell;
vertical-align: middle;
min-height: 50px;
height: 100%;
overflow: auto;
}
<div class="left"></div>
<div class="right"></div> <!-- needs to be 250px -->
See: http://jsfiddle.net/SpSjL/ (adjust the browser's width)
HTML:
<div class="right"></div>
<div class="left"></div>
CSS:
.left {
overflow: hidden;
min-height: 50px;
border: 2px dashed #f0f;
}
.right {
float: right;
width: 250px;
min-height: 50px;
margin-left: 10px;
border: 2px dashed #00f;
}
You can also do it with display: table, which is usually a better approach: How can I put an input element on the same line as its label?
It's 2017 and the best way to do it is by using flexbox, which is IE10+ compatible.
.box {
display: flex;
}
.left {
flex: 1; /* grow */
border: 1px dashed #f0f;
}
.right {
flex: 0 0 250px; /* do not grow, do not shrink, start at 250px */
border: 1px dashed #00f;
}
<div class="box">
<div class="left">Left</div>
<div class="right">Right 250px</div>
</div>
You can use calc() Function of CSS.
Demo: http://jsfiddle.net/A8zLY/543/
<div class="left"></div>
<div class="right"></div>
.left {
height:200px;
width:calc(100% - 200px);
background:blue;
float:left;
}
.right {
width:200px;
height:200px;
background:red;
float:right;
}
Hope this will help you!!
If you can flip the order in the source code, you can do it like this:
HTML:
<div class="right"></div> // needs to be 250px
<div class="left"></div>
CSS:
.right {
width: 250px;
float: right;
}
An example: http://jsfiddle.net/blineberry/VHcPT/
Add a container and you can do it with your current source code order and absolute positioning:
HTML:
<div id="container">
<div class="left"></div>
<div class="right"></div>
</div>
CSS:
#container {
/* set a width %, ems, px, whatever */
position: relative;
}
.left {
position: absolute;
top: 0;
left: 0;
right: 250px;
}
.right {
position: absolute;
background: red;
width: 250px;
top: 0;
right: 0;
}
Here, the .left div gets an implicitly set width from the top, left, and right styles that allows it to fill the remaining space in #container.
Example: http://jsfiddle.net/blineberry/VHcPT/3/
If you can wrap them in a container <div> you could use positioning to make the left <div> anchored at left:0;right:250px, see this demo. I'll say now that this will not work in IE6 as only one corner of a <div> can be absolutely positioned on a page (see here for full explanation).
1- Have a wrapper div, set the padding and margin as you like
2- Make the left side div the width you need and make it float left
3- Set the right side div margin equal to the left side width
.left
{
***width:300px;***
float: left;
overflow:hidden;
}
.right
{
overflow: visible;
***margin-left:300px;***
}
<div class="wrapper">
<div class="left">
...
</div>
<div class="right" >
...
</div>
</div>
Hope this works for you!
There are quite a few ways to accomplish, negative margins is one of my favorites:
http://www.alistapart.com/articles/negativemargins/
Good luck!
set your right to the specific width and float it, on your left just set the margin-right to 250px
.left {
vertical-align: middle;
min-height: 50px;
margin-right: 250px;
overflow: auto
}
.right {
width:250px;
text-align: right;
display: table-cell;
vertical-align: middle;
min-height: 50px;
height: 100%;
overflow: auto
}
If you need a cross browser solution, you can use my approach, clear and easy.
.left{
position: absolute;
height: 150px;
width:150px;
background: red;
overflow: hidden;
float:left;
}
.right{
position:relative;
height: 150px;
width:100%;
background: red;
margin-left:150px;
background: green;
float:right;
}
Use the simple this can help you
<table width="100%">
<tr>
<td width="200">fix width</td>
<td><div>ha ha, this is the rest!</div></td>
</tr>
</table>

Resources