Swap div order using css only - html

I am wondering if it's possible to display two div elements in inverted order using only css.
No html change or javascript code, just css.
I have the following html:
<div id="container" class="clearfix">
<div id="right-sidebar">Right</div>
<div id="left-sidebar">Left</div>
</div>
and this current css:
#container {
width: 200px;
border: 2px solid blue;
padding: 2px;
margin: 0;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#left-sidebar, #right-sidebar {
width: 150px;
padding: 2px;
}
#left-sidebar {
border: 2px solid red;
float: left;
}
#right-sidebar {
border: 2px solid green;
float: right;
}
The result shows the right div above the left one. I'd like to swap them, showing the left one above the right one, maintaining the container properties (auto calculated height).
To explain it in different words, I'd like to achive using just CSS the same result I would obtain by swapping the two divs in the html code.
Is it even possible with only css? [I'm dreaming about a float: bottom property :)]
http://jsfiddle.net/mT7JJ/1/

According to this and many others, i am afraid you can not swap only with css, but I've found something that will help you in this situation and that is this
So this will be your edit on fiddle
#container {
display: table; width: 200px;
border:1px red solid;
}
#left-sidebar {
display: table-header-group;
}
#right-sidebar {
display: table-footer-group;
}

The only think i can think about is relative/absolute position. But it will not be really efficient though

One modern solution, as has been comented, is flex layout.
Another tricky posibilitity is using transforms
webkit demo
I an just rotating the container upside down, and then rotaing the inner divs to make them look ok. It's done in the hover, to show the net effect.
The hover is a little bit inestable due to the clearfix, but this is not relevant here.
#container:hover {
-webkit-transform: rotateX(180deg);
}
#container:hover div {
-webkit-transform: rotateX(180deg);
}
I enjoy answering a question that has been declared imposible :-)

<div id="container" class="clearfix">
<div id="left-sidebar">Left</div>
<div id="right-sidebar">Right</div>
</div>
#container {
width: 200px;
border: 2px solid blue;
padding: 2px;
margin: 0;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#left-sidebar, #right-sidebar {
width: 150px;
padding: 2px;
}
#left-sidebar {
border: 2px solid red;
}
#right-sidebar {
border: 2px solid green;
}
check this left div above right div

Using CSS only:
#blockContainer {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
}
#blockA {
-webkit-box-ordinal-group: 2;
-moz-box-ordinal-group: 2;
box-ordinal-group: 2;
}
#blockB {
-webkit-box-ordinal-group: 3;
-moz-box-ordinal-group: 3;
box-ordinal-group: 3;
}
<div id="blockContainer">
<div id="blockA">Block A</div>
<div id="blockB">Block B</div>
<div id="blockC">Block C</div>
</div>
http://jsfiddle.net/hLUHL/713/

no expert and old question but use case apply for me under specific conditions to anwser this question: "Swap div order using css only"
My answer is almost entirely based on Harry's answer here https://stackoverflow.com/a/31366853/3913379 (Original Answer v2) and tested nowadays (Chrome/FF) under the condition that the child divs are same size (use case is main Div contains TWO divs with link icons and text).
So to swap TWO divs left to right and vice versa using CSS only I used a css transform on X axis (transform: translateX ), like this:
<STYLE type="text/css">
#Child1Div {transform: translateX(100%);}
#Child2Div {transform: translateX(-100%);}
</STYLE>
<div id="MotherDiv" >
<div id="Child1Div">Context of child one</div>
<div id="Child2Div">Content of child two</div>
</div>
So, note usage of percentages and negative values in one case, to attain basically "the swap". HTML structure is unchanged (and JS/JQuery listeners were unaffected, for example link's onClick(...) )
This may fail under some inherited special styles but simple case worked nicely for me.
Style can be applied in HEAD tag or inline on divs or also dynamically via JS (for example to swap two Icons/Images via a button click or something like that)

There are two ways to do that with css. Flex and Grid.
Flex:
#container {
display: flex;
flex-wrap: wrap;
position: relative;
justify-content: flex-start;
flex-direction: row-reverse;
}
Grid:
#container {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr))
}
#left-sidebar {
order 2
}
#right-sidebar {
order 1
}

Related

How to force a flex item to the bottom of the page?

This is how I want my Layout
Currently it looks like this
I can increase the size of the second box by specifying the height.
.row-2 {
height: 500px;
}
Surely there is a better way than this.
I've also tried to force the last row of boxes to the bottom without any luck:
.row-3 {
margin-top: auto;
}
My flexbox is as follows:
<div className="container">
<div className="row-1">1</div>
<div className="row-2">2</div>
<div className="row-3">
<div>3</div>
<div>4</div>
</div>
</div>
.container {
display: flex;
flex-direction: column;
max-width: 100%;
}
.row-1 {
border: 2px solid green;
}
.row-2 {
border: 2px solid blue;
}
.row-3 {
border: 2px solid red;
display: flex;
flex: 1;
margin-top: auto; /* not working */
}
.row-3 > div {
flex-grow: 1;
border: 2px solid purple;
}
Having flex-grow: 1; is probably a problem. I think that you're specifically saying that the third row can grow more. You might want to delete that and set flex-grow: 2; on the second row.
Maybe set width: 100%; on the second row?
It does look like you should use css-grid instead though. I'd recommend this tutorial for learning it
I am suspecting that your container is the immediate child of the <body>. If so, body will have height as much as it has to hold, in this case it will have the height same as the container. Because of this you are not able to do what you really want.
I would recommend you to add min-height: 100vh to the container, this will create the container of height 100% of the viewport.
.container {
min-height: 100vh
}
And your rest of the code will do the job.

CSS Flex stretching out links

So I am learning a bit more about using CSS flex instead of using static positioning of content. However, I have defined my link styles as well as bold styles. My guess is that it's adapting to the container that is in (which is using flex feature) and that is why it is stretching across the size of the container it is inside. My question now is, how do I fix this? I've seen that I can do "display:inline-block" on the link, but that has not fixed it.
Here is my code:
.container{
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
width: 80%;
margin: auto;
background-color:#fff;
overflow: hidden;
font-size: 15px;
line-height: 20px;
padding:1em;
}
.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}
a{
text-decoration:none;
border-bottom-style:double;
border-bottom-width:2px;
color:#99d3df;
display:inline-block;
padding:0px;
overflow: hidden;
}
i{
display:inline-block;
color:#88bbd6;
text-decoration:italic;
}
And what I have:
This is a Google Link<BR>
Google is <i>extremely helpful</i>!
This is what it looks like for reference.
Problem image
It seems you missed the .container wrapper div in the markup you provided.
Let's look at this code:
<!-- HTML -->
<div class="container">
<span>This is a </span><a href="http://google.com">Google Link</a
</div>
<div class="container">
<span>Google is </span><i>extremely helpful</i>!
</div>
<!-- /HTML -->
/* CSS */
.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}
Property flex with value of 1 100% means we tell the browser to style any elements (the asterisk *) nested in .container to have 100% width of their parent's width.
I would suggest you to just remove that part to fix the problem.
Here's my approach to your markup.
.container {
display: flex;
width: 80%; /* flexible value */
flex-direction: row; /* this is to make sure that we'll have side-to-side layout within .container */
}
a {
text-decoration: none;
border-bottom-style: double;
border-bottom-width: 2px;
color: #99d3df;
display: inline-block;
padding: 0px;
}
a, i{
margin-left: 5px; /* this is to give a 10px spacing */
}
<div class="container"><span>This is a </span>random link<span></span></div>
<div class="container"><span>Google </span><i>is extremely helpful! </i></div>
It is working fine when I tried your code in js fiddle
see in this image
May be some other css is affecting your links to stretch it out.

Alternate div width per row

So basically i have this markup
<div class="container">
<article>1</article>
<article>2</article>
<article>3</article>
<article>4</article>
<article>5</article>
<article>6</article>
</div>
How will i be able to have alternate width for the first & second div per row?
I tried article:nth-child+ diff stepping to target alternate divs, but i cant find the right combination of stepping
Edit: I really can't edit the HTML structure since this is a WordPress plugin output
You will need to use :nth-child() selector here...
Actually here the pattern is repeating itself after every 4th element...So you will need to taregt 4n, 4n+1, 4n+2 and 4n+3
.container {
display: flex;
flex-wrap: wrap;
}
article {
display: flex;
align-items: center;
justify-content: center;
height: 100px;
border: 5px solid #fff;
background: gray;
box-sizing: border-box;
color: #fff;
font: bold 20px Verdana;
}
article:nth-child(4n),
article:nth-child(4n+1) {
width: 25%
}
article:nth-child(4n+2),
article:nth-child(4n+3) {
width: 75%
}
<div class="container">
<article>1</article>
<article>2</article>
<article>3</article>
<article>4</article>
<article>5</article>
<article>6</article>
<article>7</article>
<article>8</article>
<article>9</article>
<article>10</article>
<article>11</article>
<article>12</article>
</div>
you should apply class for correct adjustment of articles use only two class with width 66% and 33%
1
2
3
4
5
6
article
{
float:left;
height:50px;
width: 30%;
background: #444;
margin: 0 0 5px 0;
}
article:nth-child(2n) {
background: #f0f;
width: 70%;
float: right;
}
article:nth-child(3n) {
width: 70%;
}
article:nth-child(4n) {
width: 30%;
float: right;
}
You can use flexbox. With display: flex;flex-wrap: wrap; on ".container" and flex-grow: 1;flex-shrink:1; on article. The article will get the width based on the content.
With flexbox you can do anything, here is a good tutorial https://css-tricks.com/snippets/css/a-guide-to-flexbox/

CSS scale divs in float layout

I want to create an automatic scaling layout without using the height property. I use a float layout between two divs, as shown in the image. The boxes in the middle have content of different size and I want the boxes to scale in such way, that all have the same height.
Try reading this article at css-tricks.
My favorite choice is probably the one taken from Paul Irish's blog at HTML5Rocks - however it does rely on modern browsers. I've created a JSFiddle based on his code:
CSS
.box {
/* basic styling */
width: 100%;
height: 95px;
border: 1px solid #555;
font: 14px Arial;
/* flexbox setup */
display: -webkit-box;
-webkit-box-orient: horizontal;
display: -moz-box;
-moz-box-orient: horizontal;
display: box;
box-orient: horizontal;
}
.box > div {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
/* our colors */
.box > div:nth-child(1){ background : #FCC; }
.box > div:nth-child(2){ background : #CFC; }
.box > div:nth-child(3){ background : #CCF; }
HTML
<div class="box">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
However note this won't work with legacy browsers, and if you're targeting those, I suggest you just adopt a table layout.
I have made a JsFiddle.
What I basically do, is use the position:absolute in combination with the top and bottom CSS property to force the inner div to take full height.
HTML:
<div id="top"></div>
<div id="middle"></div>
<div id="bottom"></div>
CSS:
body{
margin: 0px;
padding: 0px;
border: 0px;
}
#top{
width: 100%;
height: 30px;
background: blue;
position: absolute;
}
#bottom{
width: 100%;
height: 30px;
background: yellow;
position: absolute;
bottom: 0px;
}
#middle{
width: 30%;
position: absolute;
top: 30px;
bottom: 30px;
background: gray;
}
place the middle one in a div
and make the three divs inside it and give them height 100%
Use min-height. Examples of this property are provided below.
MDN
MSDN

vertical align two divs within a relative div but float one to the right

I have been trying to use display: inline-block; more often. I have two divs or images or whatever at inline-block level. I am vertically aligning them at middle. However, I would like to have one float left, and one float right - which breaks the spell. For example: a logo in a header on the left and a mobile navigation symbol on the right. I might as well just say that absolute positioning is not an option:
A FIDDLE is here: Thanks.
HTML
<header class="global-header">
<div class="logo">logo</div>
<div class="hamburger">☰</div>
</header>
CSS
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
html, body {
margin: 0;
}
.global-header {
position: relative;
width: 100%;
float: left;
padding: 1em;
background-color: rgba(0,0,0,.2);
}
.logo, .hamburger {
display: inline-block;
vertical-align: middle;
background-color: #f06;
padding: 1em;
}
.logo {
width: 12em; height: 5em;
}
.hamburger {
width: 3em; height: 3em;
}
One way of addressing this is to use text-align:justify. For this to work, the content needs to be more than one line long, so we have to add a second line of content using the :after pseudo-element. Then it's all about stopping the second line taking up any vertical space.
Add this to your css:
.global-header {
text-align:justify;
line-height:0;
}
.global-header:after {
content: '\A0';
display: inline-block;
width:100%;
line-height:0;
}
See http://jsfiddle.net/RZsyx/
Depending on what you are really putting in the logo and hamburger elements, you may need to apply a line-height to each of them.
Would the following give the desired result?
.hamburger {
width: 3em; height: 3em;
position:relative;
left:100%;
margin-left:-15em;
}
The -15 comes from hamburger width + logo width
I suppose you want to do a header for a mobile page.
you should try to use the box-model with box-orient and box-flex.
a good guide can be found here (it s in german though):
http://www.html5rocks.com/de/tutorials/flexbox/quick/
at the bottom is some kind of CSS-fiddle-box, so you can try if it
is the right choice for you.
the only drawback I can think of, is that you have do fill in another
element between .logo and .hamburger which gets the box-flex: 1.
leading you:
<header>
<div class="logo boxes">logostuff</div>
<div class="fill boxes"></div>
<div class="hamburger boxes">E</div>
</header>
with the following (additional) css
.boxes {
display:-moz-box;
background:#ff0000;
}
.global-header {
float: none;
display:-moz-box;
-moz-box-orient: horizontal;
-moz-box-align: center;
}
.fill {
-moz-box-flex:1;
}