html - 如何使用 flex 在父 div 中的 2 个兄弟 div 旁边设置 1 个 div?

标签 html css layout flexbox

我正在尝试设置这样的布局: desired layout

但是使用下面的代码我只能正确设置前两个 div,但第三个不在 div.outer 中。如何使用 CSS flex 正确设置它?到目前为止,我用 float: left 做到了,但我想在 flexbox 中做到这一点。 如果我们将有超过 3 个 div.inner,则应该从左侧开始堆叠(如果右侧没有更多空间)

.outer {
   width: 100%;
   border: 2px solid black;
   box-sizing: border-box;
   display: flex;
   flex-wrap: wrap;
   height:500px;
 }

 .inner {
   width: 50%;
   border: 2px solid red;
   box-sizing: border-box;
   
 }

.inner:first-child {
  height:100%
}

.inner:nth-child(n+2) {
  height:50%;  
}
<div class="outer">
  <div class="inner"></div>
  <div class="inner"></div>
  <div class="inner"></div>
</div>

最佳答案

使用相同的标记,您可以将 margin-left: auto 添加到 third innertranslate它使用 transform: translate(-100%) 朝向顶部 - 请参见下面的演示:

.outer {
  width: 100%;
  border: 2px solid black;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  height: 500px;
}

.inner {
  width: 50%;
  border: 2px solid red;
  box-sizing: border-box;
}

.inner:first-child {
  height: 100%;
}

.inner:nth-child(n+2) {
  height: 50%;
}

.inner:last-child { /* ADDED */
  margin-left: auto;
  transform: translateY(-100%);
}
<div class="outer">
  <div class="inner"></div>
  <div class="inner"></div>
  <div class="inner"></div>
</div>

关于html - 如何使用 flex 在父 div 中的 2 个兄弟 div 旁边设置 1 个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55020318/

相关文章:

javascript - "live-editing"多用户平台列表(用于网站项目)

php - 保持网页之间导航的最佳技术

javascript - 提交按钮同时使用两种方法,而不是一种方法,我该如何解决这个问题?

javascript - DataTables 不显示 JSON 数据

android - 如何在7英寸两个不同的平板电脑上设置布局和绘图?

java - java中的两个面板布局(第一个面板具有固定大小,第二个面板具有剩余大小)

HTML 3 列标题

css - 带有 CSS 的宇宙飞船菜单

jquery - Bootstrap 移动导航栏链接转到 URL 而不是切换

css - 使用 css 隐藏文本 : text-indent