当 UL 单独位于 TD 中而不是有一个额外的空 DIV 时,为什么 IE 中的布局会发生变化?
我正在向仍使用基于表格布局的网站添加基于 css 的选项卡导航。 当我将选项卡列表放入 td 中时,您可以看到一个视觉“间隙”。 如果我在 td 中放置一个宽度为 100% 的空 div,那么我的选项卡列表将正确显示。 (它在表格外也能正常工作。)
为什么 div 可以使选项卡正确布局,是否有更好的方法让它们在不添加内容无关的 div 的情况下做到这一点?
这是我的测试用例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Strange gap
</title>
<style type ="text/css" >
/* stolen from http://unraveled.com/publications/css_tabs/, then hacked to death */
ul.tabnav { /* general settings */
border-bottom: 1px solid #cbcbcd; /* set border COLOR as desired */
list-style-type: none;
padding: 3px 10px 3px 10px; /* THIRD number must change with respect to padding-top (X) below */
margin: 3px 0px 0px 0px; /* Right on top of the next row */
}
ul.tabnav li { /* do not change */
display: inline;
}
ul.tabnav li.current { /* settings for selected tab */
border-bottom: 1px solid #fff; /* set border color to page background color */
background-color: #fff; /* set background color to match above border color */
/* border: solid 1px red; */
}
ul.tabnav li.current a { /* settings for selected tab link */
background-color: #fff; /* set selected tab background color as desired */
color: #000; /* set selected tab link color as desired */
position: relative;
top: 1px;
padding-top: 4px; /* must change with respect to padding (X) above and below */
}
ul.tabnav li a { /* settings for all tab links */
padding: 3px 4px; /* set padding (tab size) as desired; FIRST number must change with respect to padding-top (X) above */
border: 1px solid #cbcbcd; /* set border COLOR as desired; usually matches border color specified in #tabnav */
background-color: #cbcbcd; /* set unselected tab background color as desired */
color: #666; /* set unselected tab link color as desired */
margin-right: 0px; /* set additional spacing between tabs as desired */
text-decoration: none;
border-bottom: none;
}
/* end css tabs */
</style>
</head>
<body>
<table>
<tr>
<td>
I'm making some tab navigation with css. I copied the code
from <a href=" http://unraveled.com/publications/css_tabs/"> http://unraveled.com/publications/css_tabs/</a>,
and hacked it up. There's an odd behavior that I see on IE (v 7).
There's a gap below.
</td>
</tr>
<tr>
<td>
<ul class="tabnav">
<li class="current"><a >Home</a></li>
<li ><a >Search</a></li>
</ul>
</td>
</tr>
<tr>
<td>
No gap below this
</td>
</tr>
<tr>
<td >
<div style="width: 100%"><!-- This div forces the menu to render properly in IE7. I don't know why --></div>
<ul class="tabnav">
<li class="current"><a >Home</a></li>
<li ><a>Search</a></li>
</ul>
</td>
</tr>
<tr>
<td>
why? The difference is the presence of a div with style="width: 100%" on it in the second
case. I don't know why this fixes the rendering; I'd like a better way to do it without adding an
extra empty div. This page should be in standards mode (or at least non-quirks mode).
</td>
</tr>
</table>
For comparison, it works fine outside of a table:
<ul class="tabnav">
<li class="current"><a >Home</a></li>
<li ><a >Search</a></li>
</ul>
</body>
</html>
我现在已将其交叉发布到我的博客: 培根驱动编码:当 UL 单独位于 TD 中而不是有一个额外的空 DIV 时,为什么 IE 中的布局会发生变化?,屏幕截图,以便人们可以看到我在说什么。
这是屏幕截图:
I'm adding css-based tab navigation to a site that is still using table-based layout. When I place my tab list inside a td, there is a visual "gap" that you can see. If I put an empty div with width: 100% in the td, then my tab list displays correctly. (It also works fine outside the table.)
Why does the div make the tabs lay out correctly, and is there a better way to make them do so without adding a content-free div?
Here's my test case:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Strange gap
</title>
<style type ="text/css" >
/* stolen from http://unraveled.com/publications/css_tabs/, then hacked to death */
ul.tabnav { /* general settings */
border-bottom: 1px solid #cbcbcd; /* set border COLOR as desired */
list-style-type: none;
padding: 3px 10px 3px 10px; /* THIRD number must change with respect to padding-top (X) below */
margin: 3px 0px 0px 0px; /* Right on top of the next row */
}
ul.tabnav li { /* do not change */
display: inline;
}
ul.tabnav li.current { /* settings for selected tab */
border-bottom: 1px solid #fff; /* set border color to page background color */
background-color: #fff; /* set background color to match above border color */
/* border: solid 1px red; */
}
ul.tabnav li.current a { /* settings for selected tab link */
background-color: #fff; /* set selected tab background color as desired */
color: #000; /* set selected tab link color as desired */
position: relative;
top: 1px;
padding-top: 4px; /* must change with respect to padding (X) above and below */
}
ul.tabnav li a { /* settings for all tab links */
padding: 3px 4px; /* set padding (tab size) as desired; FIRST number must change with respect to padding-top (X) above */
border: 1px solid #cbcbcd; /* set border COLOR as desired; usually matches border color specified in #tabnav */
background-color: #cbcbcd; /* set unselected tab background color as desired */
color: #666; /* set unselected tab link color as desired */
margin-right: 0px; /* set additional spacing between tabs as desired */
text-decoration: none;
border-bottom: none;
}
/* end css tabs */
</style>
</head>
<body>
<table>
<tr>
<td>
I'm making some tab navigation with css. I copied the code
from <a href=" http://unraveled.com/publications/css_tabs/"> http://unraveled.com/publications/css_tabs/</a>,
and hacked it up. There's an odd behavior that I see on IE (v 7).
There's a gap below.
</td>
</tr>
<tr>
<td>
<ul class="tabnav">
<li class="current"><a >Home</a></li>
<li ><a >Search</a></li>
</ul>
</td>
</tr>
<tr>
<td>
No gap below this
</td>
</tr>
<tr>
<td >
<div style="width: 100%"><!-- This div forces the menu to render properly in IE7. I don't know why --></div>
<ul class="tabnav">
<li class="current"><a >Home</a></li>
<li ><a>Search</a></li>
</ul>
</td>
</tr>
<tr>
<td>
why? The difference is the presence of a div with style="width: 100%" on it in the second
case. I don't know why this fixes the rendering; I'd like a better way to do it without adding an
extra empty div. This page should be in standards mode (or at least non-quirks mode).
</td>
</tr>
</table>
For comparison, it works fine outside of a table:
<ul class="tabnav">
<li class="current"><a >Home</a></li>
<li ><a >Search</a></li>
</ul>
</body>
</html>
I've now crossposted this to my blog: Bacon Driven Coding: Why does layout change in IE when UL is alone in a TD vs having an extra empty DIV?, with a screenshot so people can see what I'm talking about.
Here's the screenshot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种差距是由 IE 处理列表的有趣方式造成的。 由于
ul
标签只能包含,IE 显然也认为列表项之间的空格不合适,并将其移动到前面的列表项内,在您的情况下在
之后。
然后,这会导致所选选项卡的白色背景与
的底部边框重叠(由于其定位)。
看起来在这个例子中解决这个问题的最简单方法就是删除 ul.tabnav li.current 上的背景颜色。 事实上,我认为您可以删除整个规则集,因为边界也不执行任何操作。
如果这些都不能帮助您在实际站点上修复它,最后的办法可能是删除
如果您想保留某种格式的外观,则可以使用类似的内容:
但是该空白也是造成选项卡之间间距的全部原因,因此您必须添加右边距才能将其恢复。
The gap is caused by IE's funny ways with lists. Since
ul
tags can only contain<li>
s, IE apparently also believes the whitespace between the list items is inappropriate and moves it inside the preceding list item, in your case following the<a>
.That then causes the white background of the selected tab to overlap the
<ul>
s bottom border (due to its positioning).It looks like the easiest way to solve it in this example is simply to remove the background color on
ul.tabnav li.current
. In fact, I think you can remove that entire ruleset, since the border doesn't do anything either.If none of that helps you fix it on the actual site, a last resort could be to get rid of all whitespace outside the
<li>
tags. E.g.or something like this if you want to retain some semblance of formatting:
But that whitespace is also all that's responsible for the spacing between the tabs, so you'd have to add a right margin to bring it back.