Skip to content Skip to sidebar Skip to footer

How To Arrange 3 Divs In A Different Way For Responsive Design

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 re

Solution 1:

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

<divclass="container"><divid="one">Content1</div><!--!--><divid="three">Content3</div><divid="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:1pxsolidblack;}#three {width:100%;}@media(max-width:600px) {
    #one, #two, #three {width:100%;position:initial;top:default;
    }
}

Solution 2:

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;
}
@mediaonly screen and (max-width: 500px) {
  .one {
    flex-basis: 100%;
    order: 1;
  }
  .two {
    flex-basis: 100%;
    order: 3;
  }
  .three {
    flex-basis: 100%;
    order: 2;
  }
}
<divid="container"><divclass="one">Content1</div><divclass="two">Content2</div><divclass="three">Content3</div></div>

Solution 3:

Flexbox can do this.

JSfiddle Demo

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.containerdiv {
  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;
  }
<divclass="container"><divid="one">Content1</div><divid="two">Content2</div><divid="three">Content3</div></div>

Solution 4:

You can do like following:

@mediaonly 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.

Post a Comment for "How To Arrange 3 Divs In A Different Way For Responsive Design"