浏览器调整大小时 CSS 100% 宽度
大家好,我正在尝试使用 CSS 构建布局,但遇到了一个奇怪的问题。对我来说很奇怪。我有 3 个 div,一个 页眉、一个 页脚 和一个 MainContent 区域。页眉和页脚必须保持 100% 的恒定宽度,而主要内容区域必须集中固定在 996 像素;这一切都很好,但是当我将浏览器窗口的大小调整到低于 996px 的宽度,然后向右滚动窗口的内容时,100% 页眉和页脚似乎被截断并且不再是 100%。我已经敲出了一些简单的脚本来说明这个问题(内联样式以保持紧凑)。我知道我可以向每个容器添加溢出:隐藏,以便在调整窗口大小时关闭滚动条。我还编写了一小段 jQuery 来强制 div 返回到 宽度(如果宽度低于某个宽度)。然而我的问题是围绕 CSS 的,是否有更好的纯 CSS 解决方案来解决这个问题?或者有人可以解释为什么会发生这种情况吗? 先感谢您! 点C
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div width test</title>
</head>
<body style="border:none; margin:0; padding:0; height:100%; width:100%">
<div id="header-content" style="width:100%; margin:0; padding:0; background-color:#0000ff; height:50px"></div>
<div id="main-content" style="width:996px; margin:0; padding:0; background-color:#ff00ff; height:250px; margin:auto">
<div id="inner-content">
CONTENT OF THE PAGE HERE
</div>
</div>
<div id="footer-content" style="width:100%; margin:0; padding:0; background-color:#00ffff; height:70px"></div>
</body>
Hi all I'm trying to build a layout using CSS and I'm coming up against a strange problem. Well strange for me. I have 3 divs a Header, a Footer and a MainContent area. Header and Footer must remain at a constant width of 100% while the MainContent area must be fixed centrally at 996px; This is all fine however when I resize the browser window to a width lower than 996px and then scroll the content of the window right the 100% header and footer seem to be truncated and are no longer 100%. I've knocked up a little bare-bones script to illustrate the issue (styles inline to keep it compact). I know I can add overflow:hidden to each of the containers in order to turn off the scrollbars when to window is resized. I've also written a small piece of jQuery to force the div's back to
the width, if the width drops below a certain width. However my question is around the CSS, is there a better pure CSS fix for this issue? Or can anybody explain why this happens?
Thankyou in advance!
DotsC
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div width test</title>
</head>
<body style="border:none; margin:0; padding:0; height:100%; width:100%">
<div id="header-content" style="width:100%; margin:0; padding:0; background-color:#0000ff; height:50px"></div>
<div id="main-content" style="width:996px; margin:0; padding:0; background-color:#ff00ff; height:250px; margin:auto">
<div id="inner-content">
CONTENT OF THE PAGE HERE
</div>
</div>
<div id="footer-content" style="width:100%; margin:0; padding:0; background-color:#00ffff; height:70px"></div>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下,请使用 HTML5 的 doctype。
Try this, and please use HTML5's doctype.
我不太清楚您的问题,但您可以在页眉和页脚上设置
min-width:996px;
,以使其至少与内容一样宽。I'm not completely clear on your issue, but you can set
min-width:996px;
on the header and footer to keep them at least as wide as the content.