如何以不同的方式安排3个div用于响应式设计

时间:2015-05-27 11:59:15

标签: html css responsive-design position css-position

我正在进行包含3 div s的页面重新设计,我想让它具有响应性。

我遇到的问题是大屏幕的div按照 1,2,3 的顺序排列。但是,对于响应式设计,我想将订单更改为 1,3,2

Css Reorder

我尝试了不同的方法,例如将position更改为relative / absolute / static或使用其他CSS代码更改div的顺序,但没有证明到目前为止工作。

我是如何实现这一目标的?



.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>
&#13;
&#13;
&#13;

JSFIDDLE HERE

4 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

这可以使用flexbox

来实现
  • div设置为#container包含display: flex;,这将告诉孩子div使用flexbox模型
  • flex: 1;添加到.one.two,告诉他们如果需要增长
  • flex-basis: 100%;添加到.three以确保占据容器的整个宽度
  • order: *n*;添加到.one.two.three,以便在适应较小的屏幕尺寸时为其提供所需的顺序

&#13;
&#13;
#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;
  }
}
&#13;
<div id="container">
  <div class="one">Content1</div>
  <div class="two">Content2</div>
  <div class="three">Content3</div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

> db.authenticationToken.find().pretty() { "_id" : ObjectId("5565b4d444ae3b1a9228be87"), "_class" : "com.samepinch.domain.user.AuthenticationToken", "token" : "545c1d10-d769-41cf-a47c-2d698ec4df72", "user" : { "_id" : ObjectId("5565b4d444ae3b1a9228be86"), "age" : 0, "username" : "dinesh.dhiman@oodlestechnologies.com", "firstName" : "Dinesh Dhiman", "email" : "dinesh.dhiman@oodlestechnologies.com", "gender" : "male", "createdDate" : ISODate("2015-05-27T12:13:08.562Z"), "updatedDate" : ISODate("2015-05-27T12:13:08.562Z") }, "createdDate" : ISODate("2015-05-27T12:13:08.605Z"), "updatedDate" : ISODate("2015-05-27T12:13:26.436Z") } 可以做到这一点。

JSfiddle Demo

Flexbox
* {
  -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;
  }

答案 3 :(得分:0)

你可以这样做:

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

检查Fiddle Here.