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

问题描述 投票:237回答:14

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

css underline
14个回答
298
投票

不,但你可以选择像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> - Demo


1
投票

如果你想:

  • 自定义底部填充
  • 没有包装

下划线,您可以使用repeat-x100% 100%位置的1像素高度背景图像:

display: inline;
background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAYAAAD0In+KAAAAEUlEQVQIW2M0Lvz//2w/IyMAFJoEAis2CPEAAAAASUVORK5CYII=') repeat-x 100% 100%;

你可以用100%px之类的东西替换第二个em来调整下划线的垂直位置。如果你想添加垂直填充,也可以使用calc,例如:

padding-bottom: 5px;
background-position-y: calc(100% - 5px);

当然,您也可以使用其他颜色,高度和设计制作自己的base64 png图案,例如这里:http://www.patternify.com/ - 只需设置2x1的正方形宽度和高度。

灵感来源:http://alistapart.com/article/customunderlines


0
投票

我能够使用U(下划线标签)来做到这一点

u {
    text-decoration: none;
    position: relative;
}
u:after {
    content: '';
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 1px;
    border-width: 0 0 1px;
    border-style: solid;
}


<a href="" style="text-decoration:none">
    <div style="text-align: right; color: Red;">
        <u> Shop Now</u>
    </div>
</a>

0
投票

这是我用的:

HTML:

<h6><span class="horizontal-line">GET IN</span> TOUCH</h6>

CSS:

.horizontal-line { border-bottom: 2px solid  #FF0000; padding-bottom: 5px; }

0
投票

我用的是什么:

<span style="border-bottom: 1px solid black"> Enter text here </span>

0
投票

如果你使用text-decoration: underline;,那么你可以使用text-underline-position: under;在下划线和文本之间添加空格

有关更多文本下划线位置属性,您可以查看here


106
投票

更新2019年:CSS工作组已经发布了text decoration level 4草案,该草案将添加一个新属性text-underline-offset(以及text-decoration-width),以允许控制下划线的确切位置。在撰写本文时,它是一个早期阶段的草案,并没有被任何浏览器实现,但看起来它最终将使该技术过时。

原答案如下。


直接使用border-bottom的问题是,即使使用padding-bottom: 0,下划线往往离文本太远而看起来不错。所以我们仍然没有完全控制。

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

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;
}

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

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


63
投票

你可以使用这个text-underline-position: under

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

另见browser compatibility


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;
}
<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>
  • 调整%值(81%和85%)以更改行与文本的距离
  • 调整两个%值之间的差异以更改线条粗细
  • 调整颜色值(#222222)以更改下划线颜色
  • 适用于多行内联元素
  • 适用于任何背景

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

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;
}

更新:SASSY版本

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

@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;
}

然后像这样使用它:

$blue = #0054a6;
a {
    color: $blue;
    @include fake-underline(lighten($blue,20%));
}
a.thick {
    color: $blue;
    @include fake-underline(lighten($blue,40%), 86%, 99%);
}

更新2:下降提示

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

信用

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


7
投票

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


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;
}

6
投票

这对我来说效果很好。

<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>

3
投票

看看我的fiddle

您需要使用border width属性和padding属性。我添加了一些动画让它看起来更酷:

body{
  background-color:lightgreen;
}
a{
  text-decoration:none;
  color:green;
  border-style:solid;
  border-width: 0px 0px 1px 0px;
  transition: all .2s ease-in;
}

a:hover{
  color:darkblue;
  border-style:solid;
  border-width: 0px 0px 1px 0px;
  padding:2px;
}
<a href='#' >Somewhere ... over the rainbow (lalala)</a> , blue birds, fly... (tweet tweet!), and I wonder (hmm) about what a <i><a href="#">what a wonder-ful world!</a> World!</i>

2
投票

作为多行文本或链接的替代方法,您可以将文本包装在块元素内的跨度中。

<a href="#">
    <span>insert multiline texts here</span>
</a> 

然后你可以在<span>上添加border-bottom和padding。

a {
  width: 300px;
  display: block;
}

span {
  padding-bottom: 10px;
  border-bottom: 1px solid #0099d3;
  line-height: 48px;
}

你可以参考这个小提琴。 https://jsfiddle.net/Aishaterr/vrpb2ey7/2/

© www.soinside.com 2019 - 2024. All rights reserved.