简体   繁体   English

为什么溢出:隐藏影响宽度

[英]Why does overflow: hidden affect width

In the following code, in #content rules, overflow:hidden does some magic to auto adjust the width for the div. 在下面的代码中,在#content规则中, overflow:hidden会自动调整div的width I tried to manually set width to a certain value such as 10000px in #content but the width doesn't work. 我试图在#content中手动将宽度设置为某个值,例如10000px,但宽度不起作用。 The width is determined if I use overflow:hidden|scroll|auto . 如果我使用overflow:hidden|scroll|auto则确定宽度。

I'm just wondering why overflow:hidden does the magic? 我只是想知道为什么overflow:hidden的魔法?

I've made some study on the possibility that overflow:hidden may start a BFC. 我已经研究了overflow:hidden的可能性overflow:hidden可能会启动BFC。 But display:flex has the same effect of BFC. display:flex与BFC具有相同的效果。

To be more specific, my original attempt was to let all text in #content ellipsis line by line. 更具体地说,我最初的尝试是让#content省略号中的所有文本逐行。 And I found that setting the width in #content does the work correctly. 我发现在#content中设置width可以正常工作。 But I accidentally found that I can do the same by overflow:hidden without setting a width. 但我偶然发现我可以通过overflow:hidden来做同样的事情overflow:hidden而不设置宽度。 So why is that work? 那么为什么这样呢?

 #container { padding: 12px; display: flex; } #leftblock { width: 100px; height: 100px; background-color: #ccc; } #content { margin-left: 10px; display: flex; flex-direction: column; flex: 1; overflow: hidden; } p { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; margin: 8px 0; } 
 <div id="container"> <div id="leftblock"></div> <div id="content"> <p><b>This div will take the rest of the available width, and the <code>overflow:hidden;</code> makes sure this text doesn't fall below the <code>#floated</code> div.</b></p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tincidunt vestibulum magna, nec rutrum lacus lobortis at. Duis convallis tincidunt neque eu viverra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam condimentum faucibus mauris, pretium varius dui tempor tempor. Aenean in elit dolor, a pellentesque urna. Vestibulum egestas metus id massa elementum quis posuere nunc tincidunt. Curabitur dapibus dictum lacus a fermentum.</p> <p>Quisque cursus gravida sem quis pretium. Cras cursus, neque sit amet bibendum ornare, nunc mi euismod ligula, ut cursus enim risus nec mi.</p> </div> </div> 

Fully Updated: 完全更新:

 #container { width:500px; padding: 12px; display: flex; flex-wrap: wrap; } #floated { width: 100px; height: 100px; background-color: #ccc; } #content { margin-left: 10px; display: flex; flex-direction: column; flex: 1; } p { text-overflow: ellipsis; margin: 8px 0; } 
 <div id="container"> <div id="floated"></div> <div id="content"> <p><b>This div will take the rest of the available width, and the makes sure this text doesn't fall below the <code>#floated</code> div.</b></p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tincidunt vestibulum magna, nec rutrum lacus lobortis at. Duis convallis tincidunt neque eu viverra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam condimentum faucibus mauris, pretium varius dui tempor tempor. Aenean in elit dolor, a pellentesque urna. Vestibulum egestas metus id massa elementum quis posuere nunc tincidunt. Curabitur dapibus dictum lacus a fermentum.</p> <p>Quisque cursus gravida sem quis pretium. Cras cursus, neque sit amet bibendum ornare, nunc mi euismod ligula, ut cursus enim risus nec mi.</p> </div> </div> 

UPDATED: You determine the width through .container because flex:1; 更新:您通过.container确定宽度,因为flex:1; will take up the space that is left within container. 将占用容器内剩余的空间。 I have shrunk down the container to show you. 我缩小了容器向你展示。

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

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