【发布时间】:2011-02-14 02:23:55
【问题描述】:
我有以下标记,我试图让第二个表格的右侧与上方标题的右侧对齐。这适用于 IE8、Firefox 和 Chrome,但在 IE6/7 中,表格被错误地拉伸以填充页面的宽度。
我在 IE6/7 中使用Trip Switch hasLayout trigger 应用inline-block。
有谁知道我如何(或者即使)我可以让表格只填充在 IE6/7 中以 inline-block 显示的包装元素的自然宽度?
您可以在http://jsbin.com/uyuva 看到实时运行的代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
<style>
.wrapper {
display: inline-block;
border: 1px solid green;
}
/*
display: inline-block triggers the wrapper element to have layout for IE 6/7.
The trip switch then provides the inline component of the display behaviour.
See http://www.brunildo.org/test/InlineBlockLayout.html for more details.
*/
.wrapper {
*display: inline;
}
table {
border: 1px solid red;
}
</style>
</head>
<body>
<h1>No width on table:</h1>
<div class="wrapper">
<h2>The right hand side of the table doesn't stretch to the end of this heading</h2>
<table><tr><td>foo</td></tr></table>
</div> text
<h1>Width on table:</h1>
<div class="wrapper">
<h2>The right hand side of the table should stretch to the end of this heading</h2>
<table style="width: 100%"><tr><td>foo</td></tr></table>
</div> text
</body>
</html>
更新:这是问题的另一个示例,这次没有使用表格并使用绝对定位的容器元素。您可以在http://jsbin.com/igila4 看到实时运行的代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
<style>
div {
position: absolute;
border: 1px solid red;
}
input {
width: 100%;
*width: 95%; /* fudge factor for IE 6/7 which don't support box-sizing */
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
textarea {
width: 400px;
}
</style>
<body>
The width of the input and textarea below should be identical.<br/>
<div>
<input value="This is an input with width 100%"><br/>
<textarea>This is a text area with width 400px.</textarea>
</div>
</body>
</html>
【问题讨论】:
-
您是想让表格专门匹配其上方的 H2 宽度,还是只填充 div.wrapper 的宽度而不考虑 H2?
-
如果 H2 比表格的自然宽度宽,我正在尝试让表格与 H2 的宽度相匹配。如果 H2 比自然表格宽度窄,那么我希望 div.wrapper 的宽度最终成为自然表格宽度。
标签: internet-explorer internet-explorer-6 internet-explorer-7 css