我使用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>


当前回答

我结合了上面的一些技术来提供一些东西,如果关闭js,它不会完全糟糕,如果使用jQuery,效果会更好。现在浏览器对亚像素字母间距的支持正在改进,使用它真的很好。

jQuery(document).ready(function($) { $('.nav a').each(function(){ $(this).clone().addClass('hoverclone').fadeTo(0,0).insertAfter($(this)); var regular = $(this); var hoverclone = $(this).next('.hoverclone'); regular.parent().not('.current_page_item').hover(function(){ regular.filter(':not(:animated)').fadeTo(200,0); hoverclone.fadeTo(150,1); }, function(){ regular.fadeTo(150,1); hoverclone.filter(':not(:animated)').fadeTo(250,0); }); }); }); ul { font:normal 20px Arial; text-align: center; } li, a { display:inline-block; text-align:center; } a { padding:4px 8px; text-decoration:none; color: #555; } .nav a { letter-spacing: 0.53px; /* adjust this value per font */ } .nav .current_page_item a, .nav a:hover { font-weight: bold; letter-spacing: 0px; } .nav li { position: relative; } .nav a.hoverclone { position: absolute; top:0; left: 0; white-space: nowrap; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul class="nav"> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a></li> <li class="current_page_item"><a href="#">Item 3</a></li> <li><a href="#">Item 4</a></li> <li><a href="#">Item 5</a></li> </ul>

其他回答

如果您不能设置宽度,那么这意味着宽度将随着文本变得粗体而改变。除了修改每个状态的填充/边距之类的妥协,没有办法避免这种情况。

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.

如果不反对使用Javascript,可以在页面显示后设置适当的宽度。以下是我的做法(使用Prototype):

$$('ul.nav li').each(this.setProperWidth);

setProperWidth: function(li)
{
  // Prototype's getWidth() includes padding, so we 
  // have to subtract that when we set the width.
  var paddingLeft = li.getStyle('padding-left'),
      paddingRight = li.getStyle('padding-right');

  // Cut out the 'px' at the end of each padding
  paddingLeft = paddingLeft.substring(0,paddingLeft.length-2);
  paddingRight = paddingRight.substring(0,paddingRight.length-2);

  // Make the li bold, set the width, then unbold it
  li.setStyle({ fontWeight: 'bold' });
  li.setStyle({ width: (li.getWidth() - paddingLeft - paddingRight) + 'px'});
  li.setStyle({ fontWeight: 'normal', textAlign: 'center' });
}

通过使用一个不可见的伪元素预先设置宽度,该伪元素具有与父悬停样式相同的内容和样式。使用数据属性(如title)作为内容的源。

li { display: inline-block; font-size: 0; } li a { display:inline-block; text-align:center; font: normal 16px Arial; text-transform: uppercase; } a:hover { font-weight:bold; } /* SOLUTION */ /* The pseudo element has the same content and hover style, so it pre-sets the width of the element and visibility: hidden hides the pseudo element from actual view. */ a::before { display: block; content: attr(title); font-weight: bold; height: 0; overflow: hidden; visibility: hidden; } <ul> <li><a href="#" title="height">height</a></li> <li><a href="#" title="icon">icon</a></li> <li><a href="#" title="left">left</a></li> <li><a href="#" title="letter-spacing">letter-spacing</a></li> <li><a href="#" title="line-height">line-height</a></li> </ul>

我遇到过和你类似的问题。我想让我的链接变得粗体时,你悬停在他们,但不仅在菜单,而且在文本。正如你可以猜到的,这将是一个真正的苦差事,找出所有不同的宽度。解决方法很简单:

创建一个框,其中包含粗体的链接文本,但颜色像你的背景,但你的真实链接在上面。下面是我个人主页上的一个例子:

CSS:

.hypo { font-weight: bold; color: #FFFFE0; position: static; z-index: 0; }
.hyper { position: absolute; z-index: 1; }

当然,你需要将#FFFFE0替换为页面的背景色。z索引似乎没有必要,但我还是放了它们(因为“hypo”元素将出现在HTML-Code中的“hyper”元素之后)。现在,在你的页面上放一个链接,包括以下内容:

HTML:

You can find foo <a href="http://bar.com" class="hyper">here</a><span class="hypo">here</span>

第二个“这里”将是不可见的,隐藏在你的链接下面。因为这是一个静态的盒子 你的粗体链接文本,你的其余文本不会再移动,因为它已经移动之前,你悬停在链接。