【问题标题】:How to arrange 3 divs in a different way for responsive design如何以不同的方式排列 3 个 div 以进行响应式设计
【发布时间】:2015-08-09 11:59:12
【问题描述】:

我正在重新设计一个包含 3 个divs 的页面,我想让它具有响应性。

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

我尝试了不同的方法,例如将 position 更改为 relative/absolute/static 或使用替代 CSS 代码更改 divs 顺序,但到目前为止没有任何效果。

有什么想法可以实现吗?

.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

【问题讨论】:

  • 如果你使用jquery,你可以试试.insertbefore()
  • 你在使用引导程序吗?
  • Bootstrap 是你最好的朋友。
  • @ketan,这正是我所需要的!您可以将其粘贴为答案,以便我接受!

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


【解决方案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;
    }
}

【讨论】:

    【解决方案2】:

    这可以使用flexbox来实现:

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

    #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>

    【讨论】:

    • @Paulie_D 我会说不同,不一定更好;) 无论哪种方式,使用 Flexbox 肯定是实现此功能的最适应性方式。
    【解决方案3】:

    Flexbox 可以做到这一点。

    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>

    【讨论】:

      【解决方案4】:

      你可以这样做:

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

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 2017-04-11
        • 2014-06-15
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        相关资源
        最近更新 更多