css - 使用 flexbox 设置布局

标签 css layout flexbox flexboxgrid

我在下面有一个线框,想看看用 flexbox 编写代码的最佳方法是什么。

我编码了一个 simple flexbox grid system但是我的线框需要比我的网格系统更多的定制。

enter image description here

我的父 div 有 display: flex 并且有 2 个子 div 有 flex:1flex: 0 0 40%。 添加内容 div(蓝色和红色内部的灰色框)的最佳方式是什么,该内容将与主包装器的其余部分保持一致(整个灰色框设置为 max-width: 1400px)?

谢谢。

最佳答案

这是总体思路。

定位的伪元素,一个用于行的每一部分。具有合适的宽度(注意 body 应该有 overflow-x:hidden)和合适的定位值。

* {
  box-sizing: border-box;
}
body {
  overflow-x: hidden;
}
.wrapper {
  min-height: 100vh; /* for demo purposes */
  width: 60%;
  margin: auto;
  background: grey;
  display: flex;
  flex-direction: column;
}
header {
  height: 20vh;
  background: green;
}
.inner {
  height: 30vh;
  display: flex;
}
main {
  background: blue;
  flex: 1;
  border: 2px solid black;
}
aside {
  background: red;
  flex: 0 0 40%;
  border: 2px solid black;
}
.other {
  background: pink;
  flex: 1;
}
/* magic section */

.extend {
  position: relative;
}
.extend::after {
  content: '';
  position: absolute;
  width: 100vw;
  top: 0;
  height: 100%;
  background: inherit;
  z-index: -1;
}
.left::after {
  right: 0;
}
.right::after {
  left: 0;
}
<div class="wrapper">
  <header></header>
  <div class="inner">
    <main class="extend left"></main>
    <aside class="extend right"></aside>
  </div>
  <div class="other"></div>
</div>

关于css - 使用 flexbox 设置布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38924593/

相关文章:

android - 从底部滚动隐藏的 View /布局

javascript - 对齐 self :flex start is not sending child element to the top

html - 试图将两个 div 彼此对齐并失败

twitter-bootstrap - Flexbox 在 Safari 中包装第一行的最后一列

html - 将两个元素对齐到列内的底部

jquery - 需要一个 css 或 jquery 解决方案来将嵌套的 div 扩展到 100%

html - 调整大小时如何使全部内容适合屏幕

javascript - Bootstrap tooltip——为不同的tooltip设置不同的宽度

java - 以编程方式从网站 URL 获取 Logo 图标的任何方法?

css - 哪种测量单位最适合网页布局,px、百分比或 em?