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/

<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;
    }
}

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;
}
@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>

Solution 3:

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>

Solution 4:

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.


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