我使用HTML列表和CSS创建了一个水平菜单。一切工作,它应该除了当你悬停在链接。您可以看到,我为链接创建了一个粗体悬浮状态,现在菜单链接因为粗体大小的差异而移动。

我遇到了和这篇SitePoint文章一样的问题。然而,这篇文章并没有合适的解决方案。我到处寻找解决办法,但没有找到。 我肯定不是唯一一个想这么做的人。

有人有什么想法吗?

附注:我不知道菜单项中文本的宽度,所以我不能只设置li项的宽度。

.nav { margin: 0; padding: 0; } .nav li { list-style: none; display: inline; border-left: #ffffff 1px solid; } .nav li a:link, .nav li a:visited { text-decoration: none; color: #000; margin-left: 8px; margin-right: 5px; } .nav li a:hover{ text-decoration: none; font-weight: bold; } .nav li.first { border: none; } <ul class="nav"> <li class="first"><a href="#">item 0</a></li> <li><a href="#">item 1</a></li> <li><a href="#">item 2</a></li> <li><a href="#">item 3</a></li> <li><a href="#">item 4</a></li> </ul>


当前回答

你可以用

<ul>
   <li><a href="#">Some text<span><br />Some text</span></a></li>
</ul>

然后在CSS中,将span content设置为粗体,用可见性隐藏它:hidden,这样它就能保持它的维度。然后,您可以调整以下元素的边距,使其适当适合。

我不确定这种方法是否是SEO友好的。

其他回答

折衷的解决方案是使用文本阴影来伪装粗体,例如:

文字阴影:0 0 0.01px黑色;

为了更好地进行比较,我创建了以下示例:

a, li { color: black; text-decoration: none; font: 18px sans-serif; letter-spacing: 0.03em; } li { display: inline-block; margin-right: 20px; color: gray; font-size: 0.7em; } .bold-x1 a.hover:hover, .bold-x1 a:not(.hover) { text-shadow: 0 0 .01px black; } .bold-x2 a.hover:hover, .bold-x2 a:not(.hover){ text-shadow: 0 0 .01px black, 0 0 .01px black; } .bold-x3 a.hover:hover, .bold-x3 a:not(.hover){ text-shadow: 0 0 .01px black, 0 0 .01px black, 0 0 .01px black; } .bold-native a.hover:hover, .bold-native a:not(.hover){ font-weight: bold; } .bold-native li:nth-child(4), .bold-native li:nth-child(5){ margin-left: -6px; letter-spacing: 0.01em; } <ul class="bold-x1"> <li><a class="hover" href="#">Home</a></li> <li><a class="hover" href="#">Products</a></li> <li><a href="#">Contact</a></li> <li><a href="#">About</a></li> <li>Bold (text-shadow x1)</li> </ul> <ul class="bold-x2"> <li><a class="hover" href="#">Home</a></li> <li><a class="hover" href="#">Products</a></li> <li><a href="#">Contact</a></li> <li><a href="#">About</a></li> <li>Extra Bold (text-shadow x2)</li> </ul> <ul class="bold-native"> <li><a class="hover" href="#">Home</a></li> <li><a class="hover" href="#">Products</a></li> <li><a href="#">Contact</a></li> <li><a href="#">About</a></li> <li>Bold (native)</li> </ul> <ul class="bold-x3"> <li><a class="hover" href="#">Home</a></li> <li><a class="hover" href="#">Products</a></li> <li><a href="#">Contact</a></li> <li><a href="#">About</a></li> <li>Black (text-shadow x3)</li> </ul>

传递给文本阴影非常低的模糊半径值将使模糊效果不那么明显。

一般来说,你重复的文字阴影越多,你的文字就会变得越粗壮,但同时也会失去字母的原始形状。

我应该警告你,将模糊半径设置为分数并不会在所有浏览器中呈现相同的效果!例如,Safari需要更大的值来呈现它,就像Chrome一样。

Seeing a lot of great but very involved answers here. There's really not a need for resetting widths, adding hidden elements, reworking elements for different layout declarations, etc. The simplest solution is just to account for the increase in size from the bold text by adjusting the font size in the hover declaration to a slightly smaller size. Like for example font-size: .985em from the normal 1em declaration, which allows the text to bold when hovered but also maintain the original size appearance.

如果粗体字体比常规字体宽,你也可以试试负边距。(不总是) 因此,我们的想法是使用类来设置:hover状态的样式。

jQuery:

 $(".nav-main .navbar-collapse > ul > li > a").hover(function() {
    var originalWidth = $(this).outerWidth();

    $(this).parent().addClass("hover");
    $(this).css({
       "margin-left": -(($(this).outerWidth() - originalWidth) / 2),
       "margin-right": -(($(this).outerWidth() - originalWidth) / 2)
    });
 },
 function() {
    $(this).removeAttr("style");
    $(this).parent().removeClass("hover");
 });

CSS:

ul > li > a {
    font-style: normal;
}

ul > li > a.hover {
    font-style: bold;
}

希望我能帮上忙!

使用字母间距和文本阴影:

a {
  letter-spacing: .1em;
}

a:hover {
  text-shadow: 0 0 .9px #color, 0 0 .9px #color, 0 0 .9px #color;
}

这个呢?一个javascript - CSS3免费的解决方案。

http://jsfiddle.net/u1aks77x/1/

ul{}
li{float:left; list-style-type:none; }
a{position:relative; padding-right: 10px; text-decoration:none;}
a > .l1{}
a:hover > .l1{visibility:hidden;}
a:hover > .l2{display:inline;}
a > .l2{position: absolute; left:0; font-weight:bold; display:none;}

<ul>
  <li><a href="/" title="Home"><span class="l1">Home</span><span class="l2">Home</span></a></li>
  <li><a href="/" title="Contact"><span class="l1">Contact</span><span class="l2">Contact</span></a></li>
  <li><a href="/" title="Sitemap"><span class="l1">Sitemap</span><span class="l2">Sitemap</span></a></li>
</ul>