如何增加CSS中文本和下划线之间的差距

max*_*axp 268 css underline

使用CSS,在text-decoration:underline应用文本时,是否可以增加文本与下划线之间的距离?

che*_*rtz 322

没有,但你可能会喜欢的东西去border-bottom: 1px solid #000padding-bottom: 3px.

如果你想要"下划线"的相同颜色(在我的例子中是边框),你只需要省略颜色声明,即border-bottom-width: 1pxborder-bottom-style: solid.

对于多行,您可以在元素内的跨度中包装多行文本.例如,<a href="#"><span>insert multiline texts here</span></a>然后只需添加border-bottom,并padding<span>- 演示

  • 不适用于多行链接:( (23认同)
  • 我很不确定为什么这个答案有这么多的赞成.这不适用于多行文本. (6认同)
  • 这个技巧的唯一问题是当我使用等于div高度的line-height然后vertical-align:middle; 为了让它居中,然后我不能使用这个技巧,因为下划线最终在div下面. (3认同)
  • 对于多行,您可以在元素内的跨度中包装多行文本.例如,`<a href="#"> <span>在此处插入多行文本</ span> </a>`然后只需在`<span>`上添加border-bottom和padding.https://jsfiddle.net/Aishaterr/vrpb2ey7/1/ (3认同)

las*_*ild 118

text-underline-offset直接使用的问题是即使使用text-decoration-width,下划线往往文本太远而看起来不错.所以我们仍然没有完全控制.

一个为您提供像素精度的解决方案是使用border-bottom伪元素:

a {
    text-decoration: none;
    position: relative;
}
a:after {
    content: '';

    width: 100%;
    position: absolute;
    left: 0;
    bottom: 1px;

    border-width: 0 0 1px;
    border-style: solid;
}
Run Code Online (Sandbox Code Playgroud)

通过更改padding-bottom: 0属性(负数很好),您可以将下划线准确定位在您想要的位置.

这种技术要注意的一个问题是线条包裹它有点奇怪.

  • 好的工作,但这对跨越多行的锚点不起作用 (10认同)
  • 想象一下,对于那些不熟悉网络开发,平面设计师,普通用户或者你的妈妈的人来说,这就是在文本和下划线之间添加一些受控空间所需要的,他们会想知道你在做什么对你的生活有什么影响. . (7认同)
  • Chrome 和 Edge 最近添加了“text-underline-offset”。活着是多么美好的时光啊! (4认同)
  • 感谢更新。截至 2020 年 8 月,“text-underline-offset”在 Safari 和 Firefox 中运行良好。(Chrome 显然没有收到备忘录。) (3认同)
  • 这对我的特定问题不起作用,但这是一个很棒的技巧.+1 (2认同)

小智 67

你可以用它 text-underline-position: under

有关更多详细信息,请参见此处:https://css-tricks.com/almanac/properties/t/text-underline-position/

另请参阅浏览器的兼容性.

  • 对于没有任何解决方法的现代浏览器,这是正确的答案. (6认同)
  • 到目前为止,这只适用于Chrome浏览器,而不是其他浏览器的解决方案. (3认同)
  • 好吧,截至此评论之日,我发现这**也适用于 Firefox**。 (3认同)
  • 谢谢!如果您尝试使用虚线下划线并且看到它在文本底部和线条之间没有空间的情况下绘制,则可以解决问题. (2认同)
  • 它甚至不能在Firefox中使用,甚至不能与供应商前缀一起使用-而是在W3C规范中使用:https://www.w3.org/TR/css-text-decor-3/#text-underline-position-property 。其他相关资源:https://www.quackit.com/css/css3/properties/css_text-underline-position.cfm (2认同)

小智 22

只是使用

{
   text-decoration-line: underline;
   text-underline-offset: 2px;
}


Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到堆栈溢出。Stack Overflow 上不鼓励仅提供代码答案,因为它们没有解释它如何解决问题。请编辑您的答案以解释此代码的作用以及它如何改进此问题已经有的许多其他已投票答案,以便它对具有类似问题的其他用户有用 (4认同)
  • chrome 对此没有支持。https://caniuse.com/?search=text-underline-offset (3认同)

小智 12

对此有一个简单的答案:

text-decoration: underline;
text-underline-offset: 3px;
Run Code Online (Sandbox Code Playgroud)

text-underline-offset是css它自己的方法。这里有更多关于text-underline-offset


squ*_*ndy 11

深入了解视觉风格的细节text-decoration:underline几乎是徒劳的,所以你将不得不采用某种方式去除删除text-decoration:underline并用其他内容替换它,直到一个神奇的远期未来版本的CSS给我们更多的控制权.

这对我有用:

   a {
    background-image: linear-gradient(
        180deg, rgba(0,0,0,0),
        rgba(0,0,0,0) 81%, 
        #222222 81.1%,
        #222222 85%,
        rgba(0,0,0,0) 85.1%,
        rgba(0,0,0,0)
    );
    text-decoration: none;
}
Run Code Online (Sandbox Code Playgroud)
  • 调整%值(81%和85%)以更改行与文本的距离
  • 调整两个%值之间的差异以更改线条粗细
  • 调整颜色值(#222222)以更改下划线颜色
  • 适用于多行内联元素
  • 适用于任何背景

这是一个具有所有专有属性的版本,具有一些向后兼容性:

<a href="#">Lorem ipsum</a> dolor sit amet, <a href="#">consetetur sadipscing</a> elitr, sed diam nonumy eirmod tempor <a href="#">invidunt ut labore.</a>
Run Code Online (Sandbox Code Playgroud)

更新:SASSY版本

我为此做了一个scss mixin.如果您不使用SASS,上面的常规版本仍然很好...

a {     
    /* This code generated from: http://colorzilla.com/gradient-editor/ */
    background: -moz-linear-gradient(top,  rgba(0,0,0,0) 0%, rgba(0,0,0,0) 81%, rgba(0,0,0,1) 81.1%, rgba(0,0,0,1) 85%, rgba(0,0,0,0) 85.1%, rgba(0,0,0,0) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(81%,rgba(0,0,0,0)), color-stop(81.1%,rgba(0,0,0,1)), color-stop(85%,rgba(0,0,0,1)), color-stop(85.1%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0) 81%,rgba(0,0,0,1) 81.1%,rgba(0,0,0,1) 85%,rgba(0,0,0,0) 85.1%,rgba(0,0,0,0) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0) 81%,rgba(0,0,0,1) 81.1%,rgba(0,0,0,1) 85%,rgba(0,0,0,0) 85.1%,rgba(0,0,0,0) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0) 81%,rgba(0,0,0,1) 81.1%,rgba(0,0,0,1) 85%,rgba(0,0,0,0) 85.1%,rgba(0,0,0,0) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(0,0,0,0) 0%,rgba(0,0,0,0) 81%,rgba(0,0,0,1) 81.1%,rgba(0,0,0,1) 85%,rgba(0,0,0,0) 85.1%,rgba(0,0,0,0) 100%); /* W3C */

    text-decoration: none;
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

@mixin fake-underline($color: #666, $top: 84%, $bottom: 90%) {
    background-image: linear-gradient(
        180deg, rgba(0,0,0,0),
        rgba(0,0,0,0) $top,
        $color $top + 0.1%,
        $color $bottom,
        rgba(0,0,0,0) $bottom + 0.1%,
        rgba(0,0,0,0)
    );
    text-decoration: none;
}
Run Code Online (Sandbox Code Playgroud)

更新2:下降提示

如果你有一个坚实的背景颜色,尝试添加一个薄text-stroketext-shadow与你的背景相同的颜色,使下降器看起来不错.

信用

这是我最初在https://eager.io/app/smartunderline上找到的技术的简化版本,但该文章已被删除.


Jor*_*rrk 9

这对我来说很好。

<style type="text/css">
  #underline-gap {
    text-decoration: underline;
    text-underline-position: under;
  }
</style>
<body>
  <h1 id="underline-gap"><a href="https://Google.com">Google</a></h1>
</body>
Run Code Online (Sandbox Code Playgroud)

  • 请务必注意,根据https://developer.mozilla.org/zh-CN/docs/Web/CSS/text-underline-position#Browser_compatibility,Firefox不支持`text-underline-position` (2认同)

Dom*_*c P 7

我知道这是一个老问题,但对于单行文本设置display: inline-block然后设置height已经很好地控制边框和文本之间的距离.


Mic*_*man 6

@ last-child的答案是一个很好的答案!

但是,为我的H2添加边框会产生比文本更长的下划线.

如果您正在动态编写CSS,或者如果您喜欢我,那么您很幸运且知道文本是什么,您可以执行以下操作:

  1. 改变content到正确长度的东西(即相同的

  2. text)将字体颜色设置为transparent(或rgba(0,0,0,0))

要强调<h2>Processing</h2>(例如),将last-child的代码更改为:

a {
    text-decoration: none;
    position: relative;
}
a:after {
    content: 'Processing';
    color: transparent;

    width: 100%;
    position: absolute;
    left: 0;
    bottom: 1px;

    border-width: 0 0 1px;
    border-style: solid;
}
Run Code Online (Sandbox Code Playgroud)

  • H2是块元素,可以解释为什么下划线比文本长.也许`display:inline-block`可以修复它.用`content`替换文本看起来像是一个可以在很多方面打破的黑客. (9认同)