简体   繁体   English

拉伸div宽度以匹配另一个

[英]Stretch div width to match another

I have an html layout like the following: 我有一个像以下html布局:

<div id="header"></div> 
<div id="body"></div> 
<div id="footer"></div>

If the header is a fixed width, how can I force it to stretch to match the width of the body - for cases where the body is wider than the header. 如果标题是固定宽度,我如何强制它拉伸以匹配正文的宽度 - 对于正文比标题更宽的情况。

First, encapsulate all your divs in a parent div . 首先,将所有divs封装在父div This sets a boundary to prevent certain divs from outgrowing others and makes the min-width hack a little easier to use. 这设置了一个边界,以防止某些divs超出其他divs ,并使min-width hack更容易使用。

<div id='container'>
  <div id='header'></div>
  <div id='body'></div>
  <div id='footer'></div>
</div>

Then, in your CSS, use the following min-width hack to make the minimum width directive work across all browsers. 然后,在CSS中,使用以下min-width hack使最小宽度指令适用于所有浏览器。 The details of how it works are included in the comments. 注释中包含有关其工作原理的详细信息。 Note that when referring to IE, I mean IE 6-7, I believe IE 8 works like all other browsers. 请注意,当提到IE时,我的意思是IE 6-7,我相信IE 8与所有其他浏览器一样。

#header {
   min-width:800px;   /*minimum width for non-ie browsers, ignored by ie*/
   width: 100% !important; /*width will autoexpand as necassary in non-ie browsers*/
   width: 800px;  /*ie uses width as a min-width by default.*/  
   /*Also IE ignores !important and instead uses the last directive found*/
}

Now as the body div expands to a size greater than that of the header, the header will expand to match it. 现在,当body div扩展到大于标题的大小时,标题将展开以匹配它。

I agree with Ian Elliott, I suspect you do not really need it. 我同意Ian Elliott,我怀疑你并不真的需要它。 Maybe centering will do. 也许居中会做。

If you do need an instrument that allows header to match width with content that follows, table is that instrument. 如果您确实需要一种允许标题与后面的内容匹配宽度的工具,则表格就是该工具。 Table with three one cell rows will produce the layout you want, and it used to be a standard way to layout sites. 具有三个单元格行的表将生成您想要的布局,并且它曾经是布局网站的标准方法。 Are you trying to match some old layout? 你想要匹配一些旧的布局吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM