Chrome和Safari忽略(?)位置:绝对

时间:2013-04-03 17:39:26

标签: html css webkit

查看http://www.habitatlandscape.co.uk/

在Firefox甚至Internet Explorer(!!!)中,弹出菜单显示完美,垂直居中于白色条带,并始终从最左侧开始。

在Chrome中,菜单在父li下水平展开,并且不会垂直居中。我可以通过将webkit定位到不同的位置来修复垂直对齐,但我无法修复水平对齐。

为什么Webkit会忽略position:absolute;left:0;

CSS:

#header #menu
{
 margin:0;
 padding:0;
}

#header #menu ul
{
 list-style-type:none;
 margin:0;
 padding:0;
 margin-top:28px;
 height:24px;
}

#header #menu ul li
{
 display:inline;
 position:relative;
}

#header #menu ul li a
{
 display:block;
 float:left;
 padding:7px;
 padding-bottom:3px;
 background:#fff;
 margin-right:5px;
 text-decoration:none;
 -webkit-border-top-left-radius: 5px;
 -webkit-border-top-right-radius: 5px;
 -moz-border-radius-topleft: 5px;
 -moz-border-radius-topright: 5px;
 border-top-left-radius: 5px;
 border-top-right-radius: 5px;
 font-family:'museo', serif;
 font-size:12px;
 text-transform:uppercase;
 color:#fff;
 font-weight:bold;
 padding-left:12px;
 padding-right:12px;
 background:#01973D;
 position:relative;
 z-index:2;
}

#header #menu ul li:hover a
{
 background:#00BB4A;
}

#header #menu ul li ul
{
 clear:both;
 display:none;
 position:absolute;
 top:39px;
 width:700px;
 margin:0;
 padding:0;
}

#header #menu ul li ul li
{
 display:block;
}

#header #menu ul li ul li a
{
 background:#fff !important;
 color:#000;
 font-weight:normal;
 padding:7px;
 padding-left:11px;
 color:#01973D;
 padding-top:10px;
 margin:0;
 float:left;
}

#header #menu ul li ul li a:hover
{
 color:#000;
}

#header #menu ul li:hover ul
{
 display:block;
}

HTML(CMS生成):

<div id="menu">
 <ul>
  <li class="parent"><a class="parent" href="http://www.habitatlandscape.co.uk/about-us/"><span>About Us</span></a>
   <ul>
    <li><a href="http://www.habitatlandscape.co.uk/about-us/"><span>Company History</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/about-us/meet-the-team/"><span>Meet The Team</span></a></li>
   </ul>
  </li>
  <li class="parent"><a class="menuactive parent" href="http://www.habitatlandscape.co.uk/portfolio/"><span>Portfolio</span></a>
   <ul>
    <li><a href="http://www.habitatlandscape.co.uk/portfolio/"><span>View before, during and after photos from recent projects</span></a></li>
   </ul>
  </li>
  <li class="parent"><a class="parent" href="http://www.habitatlandscape.co.uk/services/"><span>Services</span></a>
   <ul>
    <li><a href="http://www.habitatlandscape.co.uk/services/design/"><span>Design</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/patios/"><span>Patios</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/decking/"><span>Decking</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/turf/"><span>Turf</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/ponds/"><span>Ponds</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/driveways/"><span>Driveways</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/fencing/"><span>Fencing</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/electrics/"><span>Electrics</span></a></li>
    <li><a href="http://www.habitatlandscape.co.uk/services/garden-structures/"><span>Structures</span></a></li>
   </ul>
  </li>
 </ul>
 // etc
</div>

2 个答案:

答案 0 :(得分:3)

你已经display:inline创建了一个混乱 - 你的<li>元素,display:block - <a>元素。

在HTML中,将块级元素嵌套在内联元素中是无效的:

<span><div>FAIL</div></span>

当你做这样的事情时,你会遇到跨浏览器的问题。如果您使用CSS更改display属性,则同样如此:

<div style="diplay:inline"><span style="display:block">STILL A FAIL</span></div>

你做过的是:

#header #menu ul li {
    display: inline;
    /* ... */
}
#header #menu ul li a {
    display:block;
    /* ... */
}

就规格而言,这种行为或多或少是不明确的(因为它没有意义)所以浏览器保留做疯狂或荒谬的事情的权利 - 这就是你所看到的。它只适用于Firefox,因为你很幸运,它可以在Internet Explorer中运行,因为Internet Explorer本质上是疯狂和荒谬的。

如果您希望这些<li>元素水平堆叠,float:left它们而不是内联它们。然后,您可display:block <a>元素,而不会出现问题。一旦完成,您仍然需要切换哪些元素position:relative; - 编辑,并可能在某处添加left:0

Here's an example of your current issue on jsfiddlehere's an example of my suggested fix on jsfiddle,其中涉及相对定位#header #menu ul元素而不是#header #menu ul li

答案 1 :(得分:0)

当我给#header #menu ul li一个display:inline-block;时,它就修好了。它还改变了隐藏的ul top定位的结果,如果按钮不对,它应该是24px以匹配高度,对吗?