css - 带有 float 元素的背景色

标签 css html css-float background-color

我想在主 DIV 中放置 2 个垂直 DIV 时遇到问题。 background-color 值不适用于主 DIV。我想我必须看到一些红色背景的部分。
请看一下它的jsFiddle并帮助我解决它。

最佳答案

尝试使用 clear:both CSS 属性添加一个空的 div,或者为你的主 div 设置一个高度。

<div id="content">
    <div id="float-left">Some Text floating left</div>
    <div id="float-right">Some Text floating right</div>
    <div id="clear"></div>
</div>

CSS 代码

#content {
    background-color:red; /* The background you want */
    width:500px; /* Demo: not important */
}

#float-right {
    float:right;
    width:300px;
    padding-right:20px;
    line-height:200%;
    background-color:#f2f2f2
}

#float-left {
    float:left;
    width:50px;
    background-color:#e1e1e1
}

#clear{
   clear:both
}

See Demo

关于css - 带有 float 元素的背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460213/

相关文章:

html - 三列,内部自动换行,但元素之间没有换行符

html - Float 在 IE 7 和 IE 8 中看起来不同(html 验证)

javascript - Jquery Hashtag 链接系统(显示用户所在的页面)

css - Sublime Text 2 中突出显示的颜色表示什么?

html - 在两个不同的区域替换同一个类 - CSS

windows - 使用 CSS3、HTML5 和 Javascript 重新创建 WP7 Metro UI 的外观

html - 对齐不同高度的 float

css - 为什么这个 Gulp 片段中有多个 gulp.dest() ?

javascript - 悬停时更改图像

html - 从 Google Chrome 中的 Inspect Element 复制代码