简体   繁体   English

CSS:调整元素大小而不是清除浮动

[英]CSS: Resize Element rather than clear the float

I have a box which contains an image, which has float:left set, and textual contents. 我有一个包含图像的框,该图像具有float:left设置和文本内容。

-------------------------------------
|---------                           |
||       |                           | 
|| Image |                           |
||       |       Content             |
|---------                           |
|                                    |
|                                    |
|                                    |
--------------------------------------
                                Fig. 1

I generally consider it good to have the content float around the image. 我通常认为让内容围绕图像浮动是很好的。 However, in case of using lists, the following look is annoying: 但是,在使用列表的情况下,以下外观令人讨厌:

-------------------------------------
|---------                           |
||       |   List                    | 
|| Image |                           |
||       |   1. Item                 |
|---------   2. Item                 |
|  3. Item                           |
|  4. Item                           |
|                                    |
--------------------------------------
                                Fig. 2

I'd rather have it the following way (at least for considerably short lists, let's assume the list is short for now) 我希望采用以下方式(至少对于相当短的列表,让我们假设列表目前很短)

-------------------------------------
|---------                           |
||       |   List                    | 
|| Image |                           |
||       |   1. Item                 |
|---------   2. Item                 |
|            3. Item                 |
|            4. Item                 |
|                                    |
| Additional content (not in list)   |
--------------------------------------
                                Fig. 3

I got the above look by making the list display: inline-block (and either inserting a <br> before the list, or wrapping it in a block-level element) 通过使列表display: inline-block我得到了上面的外观display: inline-block (并在列表之前插入<br>或将其包装在块级元素中)

However, in case of any long list items (longer than the small width of the content field), the float is cleared. 但是,如果有任何长列表项(长于内容字段的小宽度),则清除浮点数。

-------------------------------------
|---------                           |
||       |                           | 
|| Image |                           |
||       |                           |
|---------                           |
| 1. Item                            |
| 2. A very long item, which makes   |
|    the list box just as wide as the|
|    outer box.                      |
| 3. More items                      |
-------------------------------------|
                                Fig. 4

Why this happens seems clear to me. 为什么发生这种情况在我看来很清楚。 In the floated environment, first, the list is rendered as a block (because of display: inline-block ), using the width of the outer box as environment width. 在浮动环境中,首先,使用外部框的宽度作为环境宽度,将列表呈现为一个块(由于display: inline-block )。 As there is a long items, the resulting block will be as wide as the outer box. 由于物品很长,因此块的宽度将与外盒一样宽。 In a second step, the block is tried to fit next to the floating image, where it won't fit. 在第二步中,尝试将块适配到不适合浮动图像的位置。 Lastly, the float is cleared. 最后,清除浮点数。

Is there any way to amend the situation? 有什么办法可以改变这种情况? Like, first try to render the list with the shorter width, and if that fails, re-render? 像,首先尝试以更短的宽度呈现列表,如果失败,重新呈现? Or a completely different way to achieve what I want? 还是完全不同的方式来实现我想要的?

将列表放在DIV ,该DIV也以定义的宽度向左浮动。

Try a plain overflow:hidden on your list - this should do the trick. 尝试一个简单的overflow:hidden在您的列表中-这可以解决问题。

See the example . 参见示例

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

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