当在TD中单独使用UL与具有额外的空DIV时,为什么IE中的布局会发生变化?
-
06-07-2019 - |
题
我正在将基于css的选项卡导航添加到仍在使用基于表格的布局的网站上。当我将标签列表放在td中时,会出现一个视觉<!>“gap <!>”;你可以看到。如果我在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>
我现在将此转到我的博客:培根驱动编码:为什么当在TD中单独使用UL而在具有额外的空DIV时,IE中的布局会发生变化?,带有屏幕截图所以人们可以看到我在说什么。
这是截图:
解决方案
这种差距是由IE浏览器列表的有趣方式引起的。由于ul
标签只能包含<li>
s,因此IE显然也认为列表项之间的空格不合适,并将其移到前面的列表项中,在<a>
之后的情况下。
然后导致所选标签的白色背景与<ul>
底部边框重叠(由于其定位)。
在此示例中,解决此问题的最简单方法就是删除ul.tabnav li.current
上的背景颜色。事实上,我认为你可以删除整个规则集,因为边框也没有做任何事情。
如果没有一个可以帮助您在实际网站上修复它,最后的办法可能是摆脱<=>标签之外的所有空格。 E.g。
<ul><li><a>Home</a></li><li>etc.</li></ul>
或类似的东西,如果你想保留一些相似的格式:
<ul><!--
--><li><a>Home</a></li><!--
--><li>etc.</li><!--
--></ul>
但是这个空格也是导致标签之间间距的原因所在,所以你必须添加一个合适的边距才能恢复。
不隶属于 StackOverflow