【问题标题】:CSS FlexBox | Reordering Elements in MobileCSS 弹性框 |在移动设备中重新排序元素
【发布时间】:2021-10-25 23:49:19
【问题描述】:

我正在尝试学习 CSS Flexbox,但发现了一个障碍。

我的内容可以在桌面屏幕大小上左右显示,而对于移动设备,我有flex-direction: column

见下图:

桌面:

手机:

这是完成此操作的代码:

 <div class="container">
    <div class="box box1">
      <div class="a">a</div>
      <div class="b">b</div>
    </div>
    <div class="box box2">
       <div class="c">c</div>
      <div class="d">d</div>
    </div>
  </div>

这些是 flexbox 样式:

.box {
  color: white;
  font-size: 100px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  width: 100vw;
}

.container {
  display: flex;
  height: 100vh;
}

.a, .b, .c, .d {
  height: 50%;
}

@media all and (max-width: 500px) {
  .container {
    flex-direction: column;
  }
  
  .box {
     height: 50vh;
  }
}

在移动设备中,我如何按以下顺序排列以下 div(按原样)然而

一个

c

d

b

不幸的是,我似乎无法找到解决方案。

我有一个 CodePen here 重要的 CSS 行从第 162 行开始。

【问题讨论】:

  • 仅使用 CSS,我认为您的内容应该处于同一级别,以便您能够在您的用例中使用 order,现在它们被困在父级 div.box 中。
  • 谢谢努里。你能提供一个答案例子吗?
  • 我的错,看Temani Afif的回答。
  • @NuriKatsuki 我认为你的观点还是有道理的,因为任何 IE/Edge 版本都缺乏支持以及其他主流浏览器的无法访问问题,但display: contents 非常酷。
  • @NullisTrue 如果您需要 Edge 支持,可以查看此答案:stackoverflow.com/questions/55477305/…

标签: html css flexbox


【解决方案1】:

您可以在.box 元素上考虑display:contents (https://caniuse.com/#feat=css-display-contents),然后您将能够在内部元素上使用顺序:

.box {
  color: white;
  font-size: 80px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  width: 100vw;
}
body {
 margin:0;
}
.container {
  display: flex;
  height: 100vh;
  background:blue;
}

.a,.b,.c,.d {
  height: 50%;
  border:2px solid;
}

@media all and (max-width: 500px) {
  .container {
    flex-direction: column;
  }
  .box {
    display:contents;
  }
  .b {
    order:2;
  }
}
<div class="container">
  <div class="box box1">
    <div class="a">a</div>
    <div class="b">b</div>
  </div>
  <div class="box box2">
    <div class="c">c</div>
    <div class="d">d</div>
  </div>
</div>

display: contents 使元素的子元素显示为就好像它们是元素父元素的直接子元素,忽略元素本身。当使用 CSS 网格或类似布局技术时,应该忽略包装元素,这可能很有用。


如果您愿意更改 html,您可以按如下方式进行:

.container > * {
  color: white;
  font-size: 80px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  border:2px solid;
  box-sizing:border-box;
}
body {
 margin:0;
}
.container {
  display: flex;
  flex-direction:column;
  flex-wrap:wrap;
  height: 100vh;
  background:blue;
  padding:10px;
  box-sizing:border-box;
}

.a,.b,.c,.d {
  height: 50%;
  width:50%;
}

@media all and (max-width: 500px) {
  .a,.b,.c,.d {
    width:100%;
    height:25%;
  }
  .b {
    order:2;
  }
}
<div class="container">
    <div class="a">a</div>
    <div class="b">b</div>
    <div class="c">c</div>
    <div class="d">d</div>
</div>

还有 CSS 网格:

.container > * {
  color: white;
  font-size: 80px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  box-sizing:border-box;
  border:2px solid;
}
body {
 margin:0;
}
.container {
  display: grid;
  grid-template-areas:  
    'a b'
    'c d';
  grid-template-columns:1fr 1fr;
  grid-template-rows:1fr 1fr;
  grid-gap:10px;
  min-height: 100vh;
  background:blue;
  padding:10px;
  box-sizing:border-box;
}

.a {
  grid-area:a;
}
.b {
  grid-area:b;
}
.c {
  grid-area:c;
}
.d {
  grid-area:d;
}

@media all and (max-width: 500px) {
  .container {
   grid-template-areas:  
    'a'
    'c' 
    'd'
    'b';
  grid-template-columns:1fr;
  grid-template-rows:1fr 1fr 1fr 1fr;
   }
}
<div class="container">
    <div class="a">a</div>
    <div class="b">b</div>
    <div class="c">c</div>
    <div class="d">d</div>
</div>

【讨论】:

    【解决方案2】:

    如果您在.box 2 中以.box1 中的.a.c.c.d 的形式订购您的div,您可以将.container 中的列用于桌面,将.box 中的列在手机+订单内.box2

    .box {
      color: white;
      font-size: 100px;
      text-align: center;
      text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
      padding: 10px;
      width: 100vw;
      display: flex; /* <-- */
    }
    
    .container {
      display: flex;
      height: 100vh;
      flex-direction: column; /* <-- */
    }
    
    .a, .b, .c, .d {
      height: 50%;
    }
    
    @media all and (max-width: 500px) {
      .box {
        height: 50vh;
        flex-direction: column; /* <-- */
      }
    
      .box2 .d {
        order: 0; /* <-- */
      }
    }
       <div class="container">
        <div class="box box1">
          <div class="a">a</div>
          <div class="c">c</div>
        </div>
        <div class="box box2">
          <div class="b">b</div>
          <div class="d">d</div>
        </div>
      </div>

    【讨论】:

      【解决方案3】:

      Nuri Katsuki 的评论是正确的。如果“abcd” div 在同一级别,您可以使用 CSS order 属性在移动设备上实现您想要的顺序。

      另外,flex-wrap: wrap 使子项在桌面查询中流入列

      我已经编辑了你的示例来说明它:

      .container {
        color: white;
        font-size: 100px;
        text-align: center;
        text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
      
        width: 100vw;
        
        display: flex;
        height: 100vh;
        flex-direction: column;
        flex-wrap: wrap;  
      }
      
      .a { background:#e67e22;}
      .b { background:#e74c3c;}
      .c { background:#9b59b6;}
      .d { background:#34495e;}
      
      
      .a, .b, .c, .d {
        height: 50%;
        width: 50%;
      }
      
      @media all and (max-width: 500px) {
        .container {
          height: auto;
        }
        .a, .b, .c, .d { width: 100%; }
        .b {
          order: 3;
        }
      }
      <div class="container">
        <div class="a">a</div>
        <div class="b">b</div>
        <div class="c">c</div>
        <div class="d">d</div>
      </div>

      /* Some default styles to make each box visible */
      html,body {
        padding: 0;
        margin: 0;
      }
      
      .container {
        color: white;
        font-size: 100px;
        text-align: center;
        text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
      
        width: 100vw;
        
        display: flex;
        height: 100vh;
        align-items: stretch;
        flex-direction: column;
        flex-wrap: wrap;
      }
      
      .a { background:#e67e22;}
      .b { background:#e74c3c;}
      .c { background:#9b59b6;}
      .d { background:#34495e;}
      
      .a, .b, .c, .d {
        height: 50%;
        width: 50%;
      }
      
      @media all and (max-width: 500px) {
        .container {
          height: auto;
        }
        .a, .b, .c, .d { width: 100%; }
        .b {
          order: 3;
        }
      }
      <div class="container">
        <div class="a">a</div>
        <div class="b">b</div>
        <div class="c">c</div>
        <div class="d">d</div>
      </div>

      【讨论】:

        猜你喜欢
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-05
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多