在移动设备上更改列布局

Change column layout on mobile

我有一个导航菜单,包含 3 列,每列 33% 宽。在移动设备上,我希望每列都是 100% 宽并重新排列列,以便中间列位于所有其他列之上。

通过查看下面的示例,可以更容易地了解我想要实现的目标。你知道当屏幕宽度 <500px 时,我如何重新排列中间列,使其位于所有其他列之上吗?如果有一个很棒的 css 解决方案,我相信我可以在 Javascript 中轻松实现这一点,如果可能的话,我更喜欢 CSS2/CSS3 想法。

.col-1 {
  background-color: #eee;
  text-align: center;
}
.col-2 {
  background-color: #ccc;
  text-align: center;
}
.col-3 {
  background-color: #aaa;
  text-align: center;
}
.title {
  text-align: center;
  margin-top: 75px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/grids-responsive-min.css">

<p class="title">Desktop Look</p>
<div id="menu" class="pure-g">
  <div class="col-1 pure-u-1-3">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
  <div class="col-2 pure-u-1-3">
    <div class="pure-menu">
        <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-3 pure-u-1-3">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
</div>


<p class="title"><i>Desired </i>Mobile Look</p>
<div id="menu" class="pure-g">
  <div class="col-2 pure-u-1">
    <div class="pure-menu">
        <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-1 pure-u-1">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
  <div class="col-3 pure-u-1">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
</div>

您可以根据设备大小在弹性项目上使用 order 属性。

这里我在 .col-2 上使用了 order: -1;

Browser compatibility

.col-1 {
  background-color: #eee;
  text-align: center;
}

.col-2 {
  background-color: #ccc;
  text-align: center;
  order: -1;
}

.col-3 {
  background-color: #aaa;
  text-align: center;
}

.title {
  text-align: center;
  margin-top: 75px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/grids-responsive-min.css">

<p class="title">Desktop Look</p>
<div id="menu" class="pure-g">
  <div class="col-1 pure-u-1-3">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
  <div class="col-2 pure-u-1-3">
    <div class="pure-menu">
        <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-3 pure-u-1-3">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
</div>

On mobile I want each column to be 100% wide

您只需更新 css 来自:

<div class="pure-g">
    <div class="col-1 pure-u-1-3"> ... </div>
    <div class="col-2 pure-u-1-3"> ... </div>
    <div class="col-3 pure-u-1-3"> ... </div>
</div>

收件人:

<div class="pure-g">
    <div class="col-1 pure-u-1 pure-u-md-1-3"> ... </div>
    <div class="col-2 pure-u-1 pure-u-md-1-3"> ... </div>
    <div class="col-3 pure-u-1 pure-u-md-1-3"> ... </div>
</div>

现在,此网格中的元素在小屏幕上将 width: 100%,但在中等屏幕及以上屏幕上将缩小为 width: 33.33%


AND rearrange the columns so that the middle column sits above all other columns.

因为您可以在 .col-2 列上使用 order,例如:

@media screen and (max-width: 767px) {
  .col-2 {
    order: -1;
  }
}

工作演示:

.col-1 {
  background-color: #eee;
  text-align: center;
}
.col-2 {
  background-color: #ccc;
  text-align: center;  
}
@media screen and (max-width: 767px) {
  .col-2 {
    order: -1;
  }
}

.col-3 {
  background-color: #aaa;
  text-align: center;
}
.title {
  text-align: center;
  margin-top: 75px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/grids-responsive-min.css">

<p class="title">Desktop Look</p>
<div id="menu" class="pure-g">
  <div class="col-1 pure-u-1 pure-u-md-1-3">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
  <div class="col-2 pure-u-1 pure-u-md-1-3">
    <div class="pure-menu">
        <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-3 pure-u-1 pure-u-md-1-3">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
</div>


<p class="title"><i>Desired </i>Mobile Look</p>
<div id="menu" class="pure-g">
  <div class="col-2 pure-u-1">
    <div class="pure-menu">
        <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-1 pure-u-1">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
  <div class="col-3 pure-u-1">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
</div>

您可以使用媒体查询来设置弹性项目的宽度和顺序。

.col-1 {
  background-color: #eee;
  text-align: center;
}
.col-2 {
  background-color: #ccc;
  text-align: center;
}
.col-3 {
  background-color: #aaa;
  text-align: center;
}
.title {
  text-align: center;
  margin-top: 75px;
}

@media only screen and (max-width: 500px) {
  /* Using specificity to force 100% width */
  .col-1.pure-u-1-3, .col-2.pure-u-1-3, .col-3.pure-u-1-3 {
    width: 100%;
  }
  .col-1 {
    order: 2;
  }
  .col-2 {
    order: 1;
  }
  .col-3 {
    order: 3;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/grids-responsive-min.css">

<p class="title">Desktop Look</p>
<div id="menu" class="pure-g">
  <div class="col-1 pure-u-1-3">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
  <div class="col-2 pure-u-1-3">
    <div class="pure-menu">
        <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-3 pure-u-1-3">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
        <ul class="pure-menu-list">
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
            <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        </ul>
    </div>
  </div>
</div>

这可以使用 order 来完成 我创建了一个单独的 class 并向其中添加了 css 代码,因为在默认的纯 css 代码上编写 css 不是一个好习惯。

.col-1 {
  background-color: #eee;
  text-align: center;
}

.col-2 {
  background-color: #ccc;
  text-align: center;
}

.col-3 {
  background-color: #aaa;
  text-align: center;
}

.title {
  text-align: center;
  margin-top: 75px;
}

@media(max-width: 768px) {
  .top_menu {
    display: flex;
   
  }
  .top_menu-middle {
    order: 0;
  }
  .top_menu-left {
    order: 1;
  }
  .top_menu-right {
    order: 2;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.css">
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/grids-responsive-min.css">

<p class="title">Desktop Look</p>
<div id="menu" class="pure-g top_menu">
  <div class="col-1 pure-u-1 pure-u-md-1-3 top_menu-left">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
      <ul class="pure-menu-list">
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
      </ul>
    </div>
  </div>
  <div class="col-2 pure-u-1 pure-u-md-1-3 top_menu-middle">
    <div class="pure-menu">
      <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-3 pure-u-1 pure-u-md-1-3 top_menu-right">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
      <ul class="pure-menu-list">
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
      </ul>
    </div>
  </div>
</div>


<p class="title"><i>Desired </i>Mobile Look</p>
<div id="menu" class="pure-g">
  <div class="col-2 pure-u-1">
    <div class="pure-menu">
      <a href="#" class="pure-menu-heading custom-brand">Brand</a>
    </div>
  </div>
  <div class="col-1 pure-u-1">
    <div class="pure-menu pure-menu-horizontal custom-can-transform">
      <ul class="pure-menu-list">
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
      </ul>
    </div>
  </div>
  <div class="col-3 pure-u-1">
    <div class="pure-menu pure-menu-horizontal custom-menu-3 custom-can-transform">
      <ul class="pure-menu-list">
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
        <li class="pure-menu-item"><a href="#" class="pure-menu-link">Link</a></li>
      </ul>
    </div>
  </div>
</div>