3

I am working on a page redesign that contains 3 divs and I want to make it responsive.

The problem I face is that the divs for large screen are arranged in the order 1,2,3. For responsive design however, I want to change the order to 1,3,2:

Css Reorder

I tried different approaches like changing position to relative/absolute/static or changing the divs order with alternative CSS code but nothing proved to work so far.

Any ideas how I can achieve this?

.one {
  float: left;
  width: 150px;
  border: solid 2px #eaeaea;
  position: relative;
  z-index: 0;
  margin-bottom: 20px;
  height: 100px;
}
.two {
  float: left;
  margin: 0 0 0 24px;
  width: 150px;
  border: solid 2px #eaeaea;
  height: 100px;
}
.three {
  float: left;
  width: 900px;
  border: solid 2px #eaeaea;
  height: 100px;
}
@media only screen and (max-width: 500px) {
  .one {
    width: 93%;
    padding: 3%;
  }
  .two {
    width: 100%;
    margin-left: 0px;
  }
  .three {
    width: 100%;
    margin: 0px;
  }
}
<div class="one">Content1</div>
<div class="two">Content2</div>
<div class="three">Content3</div>

<div class="500markup">This box is 500px</div>

JSFIDDLE HERE

Hidden Hobbes
  • 13,893
  • 3
  • 38
  • 64
user3132858
  • 609
  • 1
  • 11
  • 27

4 Answers4

1

https://jsfiddle.net/fehrda1c/4/

<div class="container">
    <div id="one">Content1</div><!--
    !--><div id="three">Content3</div>
    <div id="two">Content2</div>

</div>

.container {
    padding: 5px;
    position: relative;
}

#one, #two {
    width: 50%;
    display: inline-block;
    box-sizing: border-box;
}

#two {
    position: absolute;
    top: 0;
    right: 0;
}

#one {
    position: absolute;
    top: 0;
    left: 0;
}

#three {
    position: absolute;
    top: 200px;
    left: 0;
    box-sizing: border-box;
}

#one, #two, #three {
    margin: 0;
    height: 200px;
    border: 1px solid black;
}

#three {
    width: 100%;
}

@media (max-width: 600px) {
    #one, #two, #three {
        width: 100%;
        position: initial;
        top: default;
    }
}
Jack hardcastle
  • 2,748
  • 4
  • 22
  • 40
1

This can be achieved using flexbox:

  • Contain the divs in a #container set to display: flex; this will tell the child divs to use the flexbox model
  • Add flex: 1; to .one and .two to tell them to grow if required
  • Add flex-basis: 100%; to .three to ensure it takes up the full width of the container
  • Add order: *n*; to .one, .two and .three to give them the desired order when they adapt to the smaller screen size

#container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
.one {
  border: solid 2px #eaeaea;
  flex: 1;
  height: 100px;
  margin-bottom: 20px;
}
.two {
  border: solid 2px #eaeaea;
  flex: 1;
  height: 100px;
  margin-bottom: 20px;
}
.three {
  border: solid 2px #eaeaea;
  flex-basis: 100%;
  height: 100px;
  margin-bottom: 20px;
}
@media only screen and (max-width: 500px) {
  .one {
    flex-basis: 100%;
    order: 1;
  }
  .two {
    flex-basis: 100%;
    order: 3;
  }
  .three {
    flex-basis: 100%;
    order: 2;
  }
}
<div id="container">
  <div class="one">Content1</div>
  <div class="two">Content2</div>
  <div class="three">Content3</div>
</div>
Hidden Hobbes
  • 13,893
  • 3
  • 38
  • 64
  • @Paulie_D I'd say different, not necessarily better ;) Either way, using Flexbox would definitely be the most adaptable way to implement this functionality. – Hidden Hobbes May 28 '15 at 07:06
1

Flexbox can do this.

JSfiddle Demo

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.container div {
  height: 150px;
  border: 1px solid red;
  margin-bottom: 10px;
}
.container {
  padding: 5px;
  position: relative;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 500px;
  border: 1px solid grey;
}
#one,
#two {
  width: 220px;
}
#three {
  width: 500px;
}
@media (max-width: 600px) {
  #one {
    order: 1;
    width: 500px;
  }
  #two {
    order: 3;
    width: 500px;
  }
  #three {
    order: 2;
  }
<div class="container">
  <div id="one">Content1</div>
  <div id="two">Content2</div>
  <div id="three">Content3</div>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

You can do like following:

@media only screen and (max-width:500px)
{       
   .one{width: 93%; padding: 3%;}
   .two{width: 100%; margin-left: 0px; margin-bottom: 10px; position:absolute; top:320px;}
   .three{width: 100%; margin: 0px;}
}

Check Fiddle Here.

Alex
  • 8,461
  • 6
  • 37
  • 49
ketan
  • 19,129
  • 42
  • 60
  • 98