html - Flexbox 列布局 : 1 to left and two stacked on right

标签 html css flexbox

我正在尝试使用 flexbox 实现以下响应式网格布局,但我无法在没有固定高度的情况下获得正确的“桌面”版本。我希望 #1 在左侧独立,#2 和 #3 在右侧相互堆叠(#1 不必与右栏具有相同的高度)

enter image description here

HTML(简体)

<div class="wrapper">
  <div class="inner">
    <div class="col1"></div>
    <div class="col2"></div>
    <div class="col3"></div>
  </div>
</div>

CSS(简化)

.wrapper {
  max-width: 600px;
  margin: 0 auto;
  border: 1px solid #c4c4c4
}

.col1, .col2, .col3 {
  background: #fe4c4c;
  position: relative;
  margin: 10px;
  padding: 5px;
  color: #FFF;
  font-weight: bold;
  font-size: 40px;
  font-family: sans-serif;

  min-height: 100px;

  &:after {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}

.col1 {
  &:after {
    content: '1';
  }

  width: 100px;
}

.col2 {
  &:after {
    content: '2';
  }

  width: calc(100% - (100px + 60px))
}

.col3 {
  &:after {
    content: '3';
  }

  width: calc(100% - 30px);
}

.inner {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}

@media only screen and (min-width: 768px) {  
  .inner {
    flex-direction: column;
  }

  .col1 {
    width: 300px;
  }

  .col2 {
    width: auto;
  } 

  .col3 {
    width: auto;
  }
}

jsFiddle https://jsfiddle.net/pcwudrmc/28053/

我四处寻找类似的问题,但没有固定高度就找不到答案。

提前致谢!

最佳答案

使用 CSS 网格的解决方案:

https://codepen.io/seanstopnik/pen/bd511c728bb79d02d11ae04edbfe9a33

* {
  box-sizing: border-box;
}

.wrapper {
  max-width: 600px;
  margin: 0 auto;
  border: 1px solid #c4c4c4;
  padding: 10px;
}

.col1,
.col2,
.col3 {
  background: #fe4c4c;
  position: relative;
  color: #fff;
  font: {
    family: sans-serif;
    size: 40px;
    weight: 700;
  }
  min-height: 100px;

  &:after {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}

.col1:after {
  content: '1';
}

.col2:after {
  content: '2';
}

.col3:after {
  content: '3';
}


// Inner
.inner {
  display: grid;
  grid: {
    template-columns: 100px auto;
    template-rows: 100px;
    gap: 10px 10px;
  }

  @media (min-width: 768px) {
    grid-template-columns: 300px auto;
  }
}

.col1 {

  @media (min-width: 768px) {
    grid: {
      column: 1 / span 1;
      row: 1 / span 2;
    }
  }
}

.col3 {
  grid: {
    column: 1 / span 2;
    row: 2 / span 2;
  }

  @media (min-width: 768px) {
    grid: {
      column: auto;
      row: auto;
    }
  }
}

关于html - Flexbox 列布局 : 1 to left and two stacked on right,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51310838/

相关文章:

html - FlexBox 容器高度不灵活

html - 是显示:flex inherited?

html - 在 Chrome 和 Safari 中正常工作时,Flex 在 Firefox 中无法工作

html - Angular Div 布局容器边框折叠

css - 如何选择特定元素之前的每个元素

php - WordPress 菜单间距

html - IE 中的水平滚动区域有问题吗?

javascript - 如何在不让整个浏览器跳下的情况下转到可滚动div中的 anchor 标记?

javascript - Angular JS : data binding with dynamically added html elements

JavaScript 代码编辑