简体   繁体   English

如何使用Flexbox?

[英]How to use Flexbox?

I have a layout like this: 我有这样的布局:

<div class="outer">
  <div class="inner"></div>
  <div class="inner"></div>
  <div class="inner">
    <div class="deeper">
      <div class="newLineContent"></div>
      <div class="newLineContent"></div>
      <div class="newLineContent"></div>
    </div>
  </div>
</div>
<div class="outer">
  <div class="inner"></div>
  <div class="inner"></div>
  <div class="inner">
    <div class="deeper">
      <div class="newLineContent"></div>
      <div class="newLineContent"></div>
      <div class="newLineContent"></div>
    </div>
  </div>
</div>

I have to align the div s like: All the div s with "outer" class has to start from new line and all the div s with "inner" class has to be in the same line within "outer" div and the div s with deeper class inside "outer" div should start from new line and the "newLineContent" div s has to be in the same line within "deeper" div s 我必须将div对齐:所有带有“外部”类的div必须从新行开始,所有带有“inner”类的div必须在“outer” divdiv中的同一行中内部更深层次的“外部” div应该从新行开始,而“newLineContent” div必须在“更深层次”的div中处于同一行

How can I achieve this using flexbox? 如何使用flexbox实现这一目标? or is there any other way to achieve it? 或者还有其他方法可以实现吗?

.outer{
  display: flex;
}

Simple as that! 就那么简单! How you configure parent and child options, depends on the effect you want to achieve. 如何配置父选项和子选项取决于您想要实现的效果。

You can do it with the Flexbox : 您可以使用Flexbox执行此操作

 .outer { display: flex; /* displays flex-items (children) inline */ justify-content: space-between; /* MDN: The items are evenly distributed within the alignment container along the main axis. The spacing between each pair of adjacent items is the same. */ } .inner:last-child .deeper { display: flex; } 
 <div class="outer"> <div class="inner">1.1</div> <div class="inner">1.2</div> <div class="inner">1.3 <div class="deeper"> <div class="newLineContent">1.3.1</div> <div class="newLineContent">1.3.2</div> <div class="newLineContent">1.3.3</div> </div> </div> </div> <div class="outer"> <div class="inner">2.1</div> <div class="inner">2.2</div> <div class="inner">2.3 <div class="deeper"> <div class="newLineContent">2.3.1</div> <div class="newLineContent">2.3.2</div> <div class="newLineContent">2.3.3</div> </div> </div> </div> 

With your current HTML structure this is the result you get. 使用您当前的HTML结构,这是您获得的结果。 Flex-items of the .inner:last-child .deeper div can't stretch the full width of the browser because the .deeper div represents one third of the parent element , ie the .outer div. .inner:last-child .deeper div的Flex项无法拉伸浏览器的整个宽度 ,因为.deeper div代表父元素的三分之一,即.outer div。

One can do similar without Flexbox, thought Flexbox appears to be the best in this case. 没有Flexbox,可以做类似的事情,因为在这种情况下,Flexbox似乎是最好的。

 .outer, .deeper { display: flex; flex-wrap: wrap; } .inner { flex-grow: 1; padding: 20px 10px; border: 1px solid white; background: lightgray; } .inner:last-child { flex-basis: 100%; } .newLineContent { flex-grow: 1; padding: 10px; border: 1px solid white; background: lightgray; } 
 <div class="outer"> <div class="inner"></div> <div class="inner"></div> <div class="inner"> <div class="deeper"> <div class="newLineContent"></div> <div class="newLineContent"></div> <div class="newLineContent"></div> </div> </div> </div> <div class="outer"> <div class="inner"></div> <div class="inner"></div> <div class="inner"> <div class="deeper"> <div class="newLineContent"></div> <div class="newLineContent"></div> <div class="newLineContent"></div> </div> </div> </div> 


Updated based on a comment 根据评论更新

If the deeper can be a child to any of the inner , and to achieve similar result, one would need either a parent selector, which doesn't exist, or give the inner an additional class for those that contain a deeper . 如果deeper可以是任何inner ,并且为了获得类似的结果,则需要一个不存在的父选择器,或者为那些包含deeperinner选择器提供一个额外的类。

Another possible workaround could be to use viewport units vw . 另一种可能的解决方法是使用视口单元vw

 .outer, .deeper { display: flex; flex-wrap: wrap; width: calc(100vw - 40px); /* 40px to make up for body's margins/scrollbar */ margin: 0 auto; } .inner { flex-grow: 1; padding: 20px 10px; border: 1px solid white; background: lightgray; } .deeper { width: calc(100vw - 62px); /* 62px is to make up for "inner" padding/border, 22px, and 40px for body's margins/scrollbar */ } .newLineContent { flex-grow: 1; padding: 10px; border: 1px solid white; background: lightgray; } 
 <div class="outer"> <div class="inner"></div> <div class="inner"></div> <div class="inner"> <div class="deeper"> <div class="newLineContent"></div> <div class="newLineContent"></div> <div class="newLineContent"></div> </div> </div> </div> <div class="outer"> <div class="inner"></div> <div class="inner"> <div class="deeper"> <div class="newLineContent"></div> <div class="newLineContent"></div> <div class="newLineContent"></div> </div> </div> <div class="inner"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM