【发布时间】:2017-08-12 19:49:54
【问题描述】:
为什么在这个实现中没有应用 100% 宽度(其中 uk-width-1-1 类应用于网格容器的子级嵌套 div):
<div uk-grid>
<!-- column 01 -->
<div>
<!-- this will be a row, stacked -->
<div class="uk-width-1-1 mine">Row 01</div>
<!-- this will be a row, stacked -->
<div class="mine">Row 02</div>
</div>
</div>
但是,当这样实现时(uk-width-1-1 类应用于网格容器的子级):
<div uk-grid>
<!-- column 01 -->
<div class="uk-width-1-1">
<!-- this will be a row, stacked -->
<div class="mine">Row 01</div>
<!-- this will be a row, stacked -->
<div class="mine">Row 02</div>
</div>
</div>
我可以看到如何达到我想要的效果,但想知道它背后的逻辑是什么,以便我更好地理解它。
显示这两种实现的 jsFiddle 是 here。
编辑:
我可以只使用 flex 样式来复制行为 - 所以我需要弄清楚为什么子 div 可以是 100% 而嵌套 div 不能?
<!-- nested div is only the width of the content -->
<div style="display:flex; flex-wrap: wrap">
<div>
<div style="width:100%; background: red">Item 1</div>
<div style="width:100%; background: red">Item 2</div>
</div>
</div>
<!-- if applied to child div, is 100% width of parent -->
<div style="display:flex; flex-wrap: wrap">
<div style="width:100%">
<div style="background: red">Item 1</div>
<div style="background: red">Item 2</div>
</div>
</div>
<!-- if not using flex at all, nested divs are 100% width of parent -->
<div>
<div>
<div style="width:100%; background: red">Item 1</div>
<div style="width:100%; background: red">Item 2</div>
</div>
</div>
也许是一个flex item,它是flex container 中的任何直接子div,默认情况下是其内容的宽度,因此嵌套div,如果给定width: 100%,忠实地表示其直接父容器宽度的100%而不是定义display: flex 的顶级容器?
【问题讨论】:
-
我的答案中是否缺少某些内容,我可以添加以使其被接受吗?