简体   繁体   English

CSS下划线和字母间距

[英]CSS underline and letter-spacing

In a website menu, I have implemented some wishes of my customer concerning typography in CSS. 在网站菜单中,我已经实现了我的客户关于CSS排版的一些愿望。 She needs a different tracking on the font, no problem. 她需要对字体进行不同的跟踪,没问题。 But, she wants the active link to be underlined. 但是,她希望强调主动链接。 As I have not implemented the code to target the active link, I just underlined them all to see how it would look. 由于我没有实现代码来定位活动链接,我只是强调它们以查看它的外观。 The CSS is as follows: 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;
}

And this is the result: 这就是结果:

带有样式链接的网站菜单

The problem is that the letter spacing kind of messes up the underlining. 问题是字母间距有点弄乱了下划线。 I've drawn some vote magnets freehand circles to indicate the problem. 我画了一些 投票磁铁 手绘圆圈来表明问题。 The line starts nicely at the left side but is extended with the value of letter-spacing to the right. 该行在左侧很好地开始,但是向右扩展了letter-spacing的值。

Screenshot is from Firefox 25. Jsfiddle to see for yourself . 屏幕截图来自Firefox 25. Jsfiddle为自己看

I could solve this using borders and using margins instead of line height, but is this fixable otherwise? 我可以使用边框并使用边距而不是行高来解决这个问题,但这是否可以修复?

CSS Text underlining too long when letter-spacing is applied? 应用字母间距时CSS文本下划线太长?

http://jsfiddle.net/isherwood/JWcGh/2 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM