css - 如何使背景适用于整个div

标签 css html background

<分区>

Possible Duplicate:
why is my content showing outside the div?

我遇到一个问题,div 的背景样式没有涵盖嵌套在 div 中的所有内容。

这是一个jsfiddle向你展示我在说什么

如何在不设置绝对高度的情况下将 div ddg-corner-statements 中的灰色背景样式应用于 div 内的所有内容?

最佳答案

这是使用 float 时的常见问题。有几个常见的解决方案:

  1. 使用 clear: both 在 float 后添加一个 div。 Example .

    <div style="float: left"></div>
    <div style="float: left"></div>
    <div style="clear: both"></div>
    
  2. 使用 CSS 属性 overflow: auto 将两个 float 添加到一个容器中。 Example .

    <div style="overflow: auto">
        <div style="float: left"></div>
        <div style="float: left"></div>
    </div>
    
  3. 将父元素设为 float 。 Example .

    <div style="float: left">
        <div style="float: left"></div>
        <div style="float: left"></div>
    </div>
    
  4. 使用 :after CSS 伪元素。 Example .

    .parentelement:after {
        content: ".";
        display: block;
        height: 0; 
        clear: both; 
        visibility: hidden;
    }
    
  5. 向父元素添加设置的高度。 Example .

    <div style="height: 200px">
        <div style="float: left"></div>
        <div style="float: left"></div>
    </div>
    

就个人而言,出于简单性和语义的考虑,我使用选项 2

查看代码的更新版本 here .

关于css - 如何使背景适用于整个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859840/

上一篇:css - 一张图片在 CSS 中的另一张图片下方

下一篇:CSS 溢出不起作用

相关文章:

javascript - 完成某项操作后,逐一显示 ajax 进程

jquery - 如何在纯CSS表单下拉元素中隐藏元素点击菜单

html - Webkit (Chrome/Safari) 在选择的自定义属性更改时不更新显示

css - 输入表单下方文本在移动设备上被截断

html - float div 未按预期在 IE 中清除

html - 使用 CSS3 的动画幻灯片

iphone - UITableView背景图片

ios - 应用程序在后台时的自定义状态栏

html - 用瓷砖构建背景渐变

html - smarty tpl 文件没有正确读取 tpl 文件