HTML/CSS:创建一个始终填充整个页面的 div...然后在其中创建一个可调整大小的 div?
我觉得这是一个简单的问题,但出于某种原因我今天无法弄清楚。
我需要一个始终填满整个页面的 div,无论该页面有多大。然后我需要另一个 div,我可以使用 javascript 调整其大小(mydiv.style.width = x; mydiv.style.height = y;)。
如果将第二个 div 的大小调整为高于现有浏览器窗口的高度,则应调整第一个 div 的大小以适应。
即,如果第一个 div 的背景颜色是红色,我应该永远看到任何白色背景颜色,因为第一个 div 总是扩展到整个页面的大小。
我尝试了这个,它不起作用,因为红色背景没有扩展到整个页面的大小:
I feel like this is an easy question, but for whatever reason I can't figure it out today.
I need a div that always fills the entire page, no matter how large that page is. Then I need another div which I can re-size with javascript (mydiv.style.width = x; mydiv.style.height = y;).
If the second div is resized to be taller than the existing browser window height, the first div should resize to fit.
i.e. If the first div's background color is red, I should never see any white background color, because the first div always expands to the size of the entire page.
I tried this, it doesn't work because the red background doesn't expand to the size of the entire page:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为扎克的替代方案是最好的答案:
body
元素是一个始终填充整个“页面”的块级元素。您可以使用 JavaScript 和 CSS 来连接它,就像使用 div 一样。将您的body
元素设置为红色,如果调整内部 div 的大小,您将永远不会看到白色。如果您不希望将 CSS 应用于站点中的每个页面,请将类或 ID 添加到要影响的页面的正文中,并编写 CSS 以仅选择具有特定类或 ID 的正文元素。我是否缺少使用
body
元素无法解决的要求?I think Zack's alternate is the best answer: the
body
element IS a block-level element that always fills the entire 'page'. You can hook into it with JavaScript and CSS, just as you can with a div. Color yourbody
element red and you'll never see white if your inner div is resized. If you don't want your CSS applied to every page in your site, add a class or ID to the body of the page you want to affect, and write your CSS to select only body elements with a specific class or ID.Am I missing a requirement that's not addressed by using the
body
element?我一直为此受到 CSS 纯粹主义者的抨击,但我最近通过使用表格解决了这个问题。
你需要一个外部 div,设置为“position:relative”和 100% 高度,然后你在里面放一个表格,每个方向也是 100%。
更多解释请参见:http://wondersofcomputing.blogspot。 com/2009/07/table-height-browser-window-height.html
欢迎您拒绝表格解决方案。但那样我就帮不了你了。
I keep getting blasted by the CSS purists for this, but I recently solved this problem by using a table.
You need an outer div, set to "position:relative" and 100% height, and then you put a table inside, also 100% each way.
More explanation here: http://wondersofcomputing.blogspot.com/2009/07/table-height-browser-window-height.html
You're welcome to spurn the table solution. But then I can't help you.
像这样的事情怎么样?
另一种选择 - 如果背景 div 只需要一种颜色 - 就是将正文的背景颜色设置为您需要的任何颜色。那么你就不需要担心任何 javascript 调整背景大小的问题。
how about something like this?
An alternative -- if that background div only needs to be a color -- is to just set the body's background-color to whatever you need. Then you don't need to worry about any javascript resizing of the background.
天哪,我解决了它,一个完全居中的 DIV,享受吧。
编辑:较小的修饰修复
Index.htm
Holy crap ive solved it, a FULLY CENTERED DIV, enjoy.
EDIT: minor cosmetic fix
Index.htm