html - 使用 CSS 属性 "float"在 HTML 中 float div 时遇到问题?

标签 html css css-float

我的 div 有问题。我目前在 html 中有四个“div”,如下所示。我想要做的是将我页面中的所有内容都放在“页面”div 中。在“main”div 中,我有“content”和“side”div,它们都有来自 CSS 的“float:left”属性。令人高兴的是,当我这样做时,我失去了白色的“页面”div 背景。我怎样才能防止这种情况并创建我的内容和侧面 div?我知道这很容易,但出于某种原因我没有做对。任何帮助将不胜感激。

谢谢

<div id=”page”>
   <div id=”container”>
   </div>
   <div id=”main”>
      <div id=”content”>
      </div>
      <div id=”side”>
      </div>
   </div>
</div>

这是我的 CSS 代码

#page 
{
margin: 2em auto;
max-width: 1000px;
background-color:#fff;
}

#main
{
clear: both;
padding: 1.625em 0 0;
width:700px;
}

#content 
{
clear: both;
padding: 1.625em 0 0;
width:740PX;;
float:left;
}

#side
{
width:250px;
margin:5px;
float:left;
}

最佳答案

你失去你的 div 背景的原因是因为它只包含 float 内容,导致它没有高度。一旦有东西“清除”了 float ,它就会再次占据空间。尝试在 main div 之后添加它(您可以在样式表中使用样式):

<div style="clear: both;"></div>

关于html - 使用 CSS 属性 "float"在 HTML 中 float div 时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168818/

相关文章:

javascript - Safari : "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." 的 html5 localStorage 错误

html - 使用相对 URL 切换到 SSL

css - 如何使 DIV 自动适应其宽度和高度?

javascript - 如何摆脱 iOS 11 Safari <video> 覆盖

JavaScript/jQuery : Divs not changing background color on loop

html - 仅在由 flex-wrap :wrap 创建的 'second row' 中对齐 flex 元素

html - 为什么有这么多文本从 flexbox 对齐的内容中溢出?

html - 同时使用 `position: absolute` 和 `float: left` 时的预期行为是什么?

javascript - 调整容器分区大小时,css float 布局不移动

css - 使用 css 将 div 绝对居中,但我无法弄清楚为什么它不移动到中心