4

在网站菜单中,我实现了客户关于 CSS 排版的一些愿望。她需要对字体进行不同的跟踪,没问题。但是,她希望在活动链接上加下划线。由于我还没有实现针对活动链接的代码,所以我只是将它们都加了下划线以查看它的外观。CSS如下:

.main-navigation a {
    display: block;
    color: black;
    font-weight: bold;
    letter-spacing: 0.45em;
    line-height: 4.5em;
    text-decoration: underline;
    text-transform: uppercase;
}

这是结果:

带有样式链接的网站菜单

问题是字母间距会弄乱下划线。我已经手绘了一些投票磁铁圆圈来指示问题。这条线很好地从左侧开始,但letter-spacing向右延伸了值。

屏幕截图来自 Firefox 25。Jsfiddle 自己看看

我可以使用边框和使用边距而不是行高来解决这个问题,但是这可以解决吗?

4

1 回答 1

3

应用字母间距时CSS文本下划线太长?

http://jsfiddle.net/isherwood/JWcGh/2

.main-navigation a:after {
  /* absolute positioning keeps it within h1's relative positioned box, takes it out of the document flow and forces a block-style display */
  position: absolute;
  /* the same width as our letter-spacing property on the h1 element */
  width: 0.45em;
  /* we need to make sure our 'mask' is tall enough to hide the underline. For my own purpose 200% was enough, but you can play and see what suits you */
  height: 200%;
  /* set the background colour to the same as whatever the background colour is behind your element. I've used a red box here so you can see it on your page before you change the colour ;) */
  background-color: #fff;
  /* give the browser some text to render (if you're familiar with clearing floats like this, you should understand why this is important) */
  content: ".";
  /* hide the dynamic text you've just added off the screen somewhere */
  text-indent: -9999em;
  /* this is the magic part - pull the mask off the left and hide the underline beneath */
  margin-left: -.40em;
}
于 2013-12-13T18:32:34.303 回答