html - 如何通过 CSS FLEX for RWD 将列跨度更改为行跨度

标签 html css flexbox

我想使用相同的 HTML 结构在桌面和移动设备上显示由 FLEX 组成的表格,但我不知道将列跨度更改为行跨度,我知道 position: absolute 它是不是维护的好主意

enter image description here


codepen

.wrapper {
    display: flex;
}

.block {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.block1 {
    background-color: cadetblue;
}

.block2 {
    background-color: cornflowerblue;
}

.block3 {
    background-color: beige;
}

.col-span {
    height: 100%;
}

/* row span */

.row-span-sample {
   position: relative;
}

.row-span {
    width: 200%;
    position: absolute;
    left: -100%;
}
<h1>column span on Desktop</h1>
<div class="wrapper">
  <div class="row">
    <div class="block block1 col-span">1</div>
  </div>
  <div class="row">

    <div class="block block2">2</div>
    <div class="block block3">3</div>
  </div>
</div>

<h1>row span on mobile</h1>
<div class="wrapper">
  <div class="row row-span-sample">
    <div class="block block1">1</div>
  </div>
  <div class="row row-span-sample">

    <div class="block block2">2</div>
    <div class="block block3 row-span">3</div>
  </div>
</div>

我可以在没有 position: absolute 的情况下更改它吗?

最佳答案

这是一个使用 CSS 网格布局和媒体查询的非常简单的解决方案。

没有绝对定位。

没有嵌套的容器。

.wrapper {
  display: grid;
  grid-template-columns: 200px 200px;
  grid-template-rows: 200px 200px;
  grid-template-areas:
    "one two"
    "one three";
}

@media (max-width: 700px) {
  .wrapper {
    grid-template-areas:
      "one two"
      "three three";
  }
}

.block1 { grid-area: one; background-color: cadetblue; }
.block2 { grid-area: two; background-color: cornflowerblue; }
.block3 { grid-area: three; background-color: beige; }

.block {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2em;
}
<div class="wrapper">
    <div class="block block1">1</div>
    <div class="block block2">2</div>
    <div class="block block3">3</div>
</div>

revised codepen

关于html - 如何通过 CSS FLEX for RWD 将列跨度更改为行跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50264544/

相关文章:

html - 影响文本的CSS不透明度过渡

javascript - jQuery 代码将所有内容隐藏在我的 div 中

html - 使行占据高度的剩余空间

javascript - 带有翻转图像的动画侧边菜单

html - Perl:HTML::PrettyPrinter - 处理自闭合标签

javascript - 将 div absolute 定位在 td 中在 firefox 中不起作用

php - CSS 布局和 <body> 标签不能一起工作并通过验证

html - 简单(宽度和高度)和最大/最小(宽度和高度)之间的区别?

html - 如何清除 flexbox 元素?

html - 时髦的 flexbox 高度