Why is this clear:both acting globally?

倖福魔咒の 提交于 2019-12-02 05:51:45

问题


Issue

As far as I know clearing floats mostly works on parent items. But there is one issue in my template after the post thumbnail, where the clear: both acts on the whole content wrapper. Without clearing the float, the thin line will stick to the text right to the thumbnail.

What I want to do is to have the line 45px below either the thumbnail or the text (depending on what height is higher).

Preview

Please have a look at this sample.

Any help would be highly appreciated!


回答1:


Just use the overflow: hidden; hack to end floats.

Add the CSS overflow: hidden to the parent element.

EDIT

As a bonus. If you want to use some fancy CSS3 stuff (like dropshadows) the above hack will fail.

There is a new hack: http://fordinteractive.com/2009/12/goodbye-overflow-clearing-hack/

Although this hack also has some issues.

And it would take some time before you can make some serious use of fancy CSS3 stuff.

You could use it, but the browser support will be poor for a long time :)




回答2:


I would recommend using a .clear class that could be used anywhere to clear floats, it would look like this:

.clear { height:0; overflow:hidden; clear:both; }

Insert it under your floated elements to clear them, it




回答3:


Float the thumbnail div left and the text div left as well. after them, set a div

<div style="clear:both"><!-- --></div>

The div that contains all 3 of these will take the length of the heighest div.

Basically:

<div class="container">
   <div class="thumbnail" style="float:left; width: 50%;"><img src="whatever.png" /></div>
   <div class="text" style="float:left; width: 50%">My text</div>
   <div style="clear:both;"><!-- --></div>
</div>


来源:https://stackoverflow.com/questions/7085100/why-is-this-clearboth-acting-globally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!