【问题标题】:Can we reorder elements when they are present inside different parents? [duplicate]当元素存在于不同的父元素中时,我们可以重新排序元素吗? [复制]
【发布时间】:2019-12-05 02:52:10
【问题描述】:

我有一个简单的部分,我想在移动设备中对其元素重新排序。

在桌面上,它看起来像这样:

我希望它在移动设备中看起来像这样:

这是我迄今为止尝试过的:JSFiddle live demo

@media only screen and (max-width: 480px) {
  .flex-item1 {
    order: 1;
  }
  .flex-item2 {
    order: 3;
  }
  .flex-item3 {
    order: 2;
  }
}
<section id="main-content">
  <div class="container">
    <div class="row" id="top-content">
      <div class="col-lg-4" id="left-content">

        <h1 class="flex-item1"></h1>
        <div class="card-custom flex-item2">
        </div>

      </div>
      <div class="col-lg-8" id="right-content">
        <div class="flex-item3" canplay id="video-block">
        </div>
      </div>
    </div>
  </div>

</section>

我做错了什么?

【问题讨论】:

  • order 属性检查父级是否具有 flexbox 模型。但在您的情况下,flexbox 设置为 row 类,您尝试的顺序不适用于直接子级。
  • 您的结构不允许这样做。一个 div 的子级不能优先于另一个 div 的子级。这违背了div 标签的整个定义。
  • @HusainTezabwala 那么我需要做什么才能得到我想要的东西??
  • @ManojKumar 嗨,我需要在我的代码中进行哪些更改才能获得我想要的内容?
  • 你在使用引导程序对吗?

标签: html css twitter-bootstrap bootstrap-4 flexbox


【解决方案1】:

在 col-xs-12 中用于移动版本。

<section id="main-content">
           `enter code here`<div class="container">
                <div class="row" id="top-content">
                    <div class="col-md-4 col-xs-12" id="left-content">
                        <div class="row col-md-12>
                             <h1 class="flex-item1"></h1>
                        </div>
                        <div class="row col-md-12>
                             <h1 class="flex-item2"></h1>
                        </div>
                    </div>
                    <div class="col-md-8 col-xs-12" id="right-content">
                            <div class="flex-item3" canplay id="video-block">
                            </div>
                    </div>
                </div>
        </div>

</section>  

css代码:

@media only screen and (max-width: 480px) {
.flex-item1 {
    order: 1; 
   }
.flex-item2{
    order: 3;
}
.flex-item3{ 
  order: 2; 
}
}  

【讨论】:

    【解决方案2】:

    我用table标签代替flexbox

    看看它是否适合你。

    .flex-item1 {
      background: skyblue;
      width;
      150px;
      height: 100px;
    }
    
    .flex-item2 {
      width: 150px;
      height: 100px;
      background: green
    }
    
    .flex-item3 {
      width: 300px;
      height: 400px;
      background: red;
    }
    
    @media only screen and (max-width: 600px) {
      table {
        width: 100%;
      }
      .flex-item1,
      .flex-item2,
      .flex-item3 {
        display: block;
        width: 100%;
      }
    }
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    
    <section id="main-content">
      <div class="container">
        <div class="row" id="top-content">
          <table>
            <tr>
              <th class="flex-item1">1</th>
              <th rowspan="2" class="flex-item3">3</th>
            </tr>
            <tr>
              <td class="flex-item2">2</td>
            </tr>
          </table>
        </div>
      </div>
    
    </section>

    【讨论】:

    • 我的兄弟,我要去看看这个,brb
    • 如果你想使用表格的好解决方案,我更喜欢带有 flexbox 的网格或引导解决方案,因为我将在这些列中添加大量数据,但感谢您的帮助,赞成快速简单的解决方案
    • 当我自己选择 table 而不是 flexbox 时,我感到很惊讶,因为它是老式的。但是 Manoj 共享的基于网格的解决方案绝对是正确的。这是正确的做法。我自己赞成他的回答;)
    • 你说得对,我花了 3 个小时试图弄清楚到底发生了什么:(
    【解决方案3】:

    您可以使用 CSS Grid。我在这里玩了Grid Generator。请使用全屏模式查看输出。

    注意:如果有人可以减少此代码,请这样做,因为我刚刚深入研究了 CSS Grid。

    JSfiddle Demo

    .container {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr;
      grid-template-rows: 1fr 1fr 1fr 1fr;
      grid-column-gap: 10px;
      grid-row-gap: 10px;
      height: 500px;
    }
    
    .div1 {
      grid-area: 1 / 1 / 3 / 3;
      background: #00A2E8;
    }
    
    .div2 {
      grid-area: 3 / 1 / 5 / 3;
      background: #22b14c;
    }
    
    .div3 {
      grid-area: 1 / 3 / 5 / 5;
      background: #ED1C24;
    }
    
    @media ( max-width: 600px) {
      .div1 {
        grid-area: 1 / 1 / 2 / 5;
      }
      .div2 {
        grid-area: 4 / 1 / 5 / 5;
      }
      .div3 {
        grid-area: 2 / 1 / 4 / 5;
      }
    }
    
    
    /* Additional styles */
    
    .container>div {
      color: #fff;
      font-size: 2em;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    <div class="container">
      <div class="div1">1</div>
      <div class="div2">2</div>
      <div class="div3">3</div>
    </div>

    【讨论】:

    • 不错的解决方案,跨浏览器兼容性如何?
    • 是的兄弟,我正在检查现在看起来不错,
    • 感谢那个网格生成器,哇,太棒了,我会用真实数据测试你的解决方案,我会回来接受你的回答
    • Manoj 正在为网格系统而苦苦挣扎,这是我的真实数据现场演示,你能帮我编写代码来获得这个meed.audiencevideo.com 吗?
    猜你喜欢
    • 2011-07-09
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2019-10-14
    相关资源
    最近更新 更多