简体   繁体   English

CSS:流体包装高度取决于浮动元素

[英]CSS: fluid wrapper height depending on floated element

I know this is a question asked 12324 times probably, but I still can't find any reliable answers :/我知道这个问题可能被问了 12324 次,但我仍然找不到任何可靠的答案:/

Here is a sample code: http://tinkerbin.com/c0wqtfSa这是一个示例代码: http : //tinkerbin.com/c0wqtfSa

The wrap should take auto height depending on the float image, but how?包裹应该根据浮动图像采用自动高度,但是如何? :/ :/

Thanks a lot!!!非常感谢!!!

ps, I can't add any extra div like "clear:both", it should be a solution only with css ps,我不能添加任何额外的div,比如“clear:both”,它应该是一个只有css的解决方案

在此处输入图片说明

Try adding尝试添加

Overflow: auto;

to your wrapper div, example here: http://tinkerbin.com/pxFxaNX2到您的包装器 div,例如: http : //tinkerbin.com/pxFxaNX2

Add a <div style="clear:both"></div> after your < p >在 <p> 之后添加<div style="clear:both"></div> >

You need to clear after a floating element...您需要在浮动元素之后清除...

So:所以:

    <div class="wrap">
      <div class="imagefloat"></div>
      <p>sldkjfn asdhbf sdjlbhgls dhjbfg ljdshbfgl jsdbfgljh dsgf sgf</p>
      <div style="clear:both"></div>
    </div>

Use clear:both like below:使用clear:both如下:

<div class="wrap">
  <div class="imagefloat"></div>
  <p>sldkjfn asdhbf sdjlbhgls dhjbfg ljdshbfgl jsdbfgljh dsgf sgf</p>
  <div style="clear:both;"></div>
</div>

您只需要在包装 div 上设置溢出和宽度,例如

overflow:hidden

You should use the clearfix workaround.您应该使用 clearfix 解决方法。 Read this: http://www.webtoolkit.info/css-clearfix.html阅读: http : //www.webtoolkit.info/css-clearfix.html

The css you need is:你需要的css是:

.clearfix:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}

.clearfix {
     display: inline-block;
}

html[xmlns] .clearfix {
    display: block;
}

* html .clearfix {
    height: 1%;
}

Then just add the clearfix class to your "wrap" div然后只需将 clearfix 类添加到您的“包装”div

A very easy and quick fix it to make the wrap div also float.一个非常简单和快速的修复它,使包装 div 也浮动。 This will make it automatically change it size to all floated elements within in, so:这将使其自动将其大小更改为 in 中的所有浮动元素,因此:

.wrap {
    float:left;
    border:1px solid #000;
    width: 500px;
    padding: 20px;
}

Whether this is an option for your project depends on the rest of the layout.这是否适合您的项目取决于布局的其余部分。

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

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