css - 为什么我的第二个 div 超出了第一个 div?

标签 css

我想将所有内容都保留在父 div 中,但第二个 div 在外面,就好像开始新的一行一样,我无法弄清楚这一点。谁能告诉我为什么>

谢谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title></title>
</head>
  <body>
    <div style="border:1px solid #000;">
      <h3>Title</h3>
      <p>Everything here expands with the parent div but the div just below this paragraph tag will not expand.</p><br />

      <div>
        <span style="float:left; width:49%; border:1px solid #000;">
          Book<br />
          <select id="book" name="book">
            <option value="">Select..</option>
          </select>
        </span>
        <span style="float:right; width:49%; border:1px solid #000;">
          Chapter<br />
          <input type="text" id="chapter" name="chapter" />
        </span>
      </div>

    </div>
  </body>
</html>

这些是新的变化并正确呈现。请查看我是否遗漏了其他任何内容。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title></title>
</head>
  <body>
    <div style="border:1px solid #000;">
      <h3>Title</h3>
      <p>Everything here expands to the parent div but the div just below this paragraph tag will not expand.</p><br />

      <div>
        <span style="float:left; width:49%; border:1px solid #000;">
          Book<br />
          <select id="book" name="book">
            <option value="">Select..</option>
          </select>
        </span>
        <span style="float:right; width:49%; border:1px solid #000;">
          Chapter<br />
          <input type="text" id="chapter" name="chapter" />
        </span>
      </div>
      <div style="clear: both;"></div>

    </div>
  </body>
</html>

最佳答案

我会将以下样式添加到包含 float div 的 div:

overflow: hidden;
zoom: 1;

溢出设置确保它完全包裹其 float 子 div,缩放设置用于 IE 浏览器,将它们踢入“hasLayout”模式。将特殊的空 div 与 clear:both 结合使用是一种相当丑陋、过时的确保布局正确的方法。

这里有一篇关于这个主题的优秀文章: http://www.quirksmode.org/css/clearing.html 尽管文章使用 width: 100% 来达到同样的目的。

关于css - 为什么我的第二个 div 超出了第一个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658309/

相关文章:

jquery - 垂直适合全屏 img 标签

javascript - 如何阻止 60 秒计时器成为 "jumpy"(css/javascript)

html - Raphael js文本定位: centering text in a circle

css 从 svg 样式溢出到另一个

javascript - Scrolltop 函数在滚动确定数量的像素后添加类

css - 为什么有一种强烈的趋势是为非桌面设备使用 m.*(移动)子域而不是使用 CSS 规则来调整网站的外观?

CSS 宽度行为奇怪

html - 如何在 CSS 中的一个选择器上使用背景图像和渐变背景颜色?

javascript - 如何使用 Javascript 获取 'css-pixel'(媒体查询)宽度?

html - Div CSS 中的多个图像