【问题标题】:How to change the length/position of text-decoration:underline?如何更改文本装饰的长度/位置:下划线?
【发布时间】:2018-07-25 14:30:37
【问题描述】:

我想在下划线可以是不同长度的地方加下划线。我还希望能够更改下划线的位置(在单词下方向左或向右移动)。这似乎是一个很容易的任务,但我无法让它工作。

.underline {

  border-bottom: 1px solid #5fca66;
  padding-bottom: 5px;

}
This is a <span class="underline">sentence</span>.  I would like some words to have longer <span class="underline">underlines</span> than others.  I would also like to be able to change the <span class="underline">position</span> of the <span class="underline">underline</span>(to be centered under the word, for example).

【问题讨论】:

  • 您是否尝试对其进行动画处理。请解释

标签: html css


【解决方案1】:

使用渐变,您可以轻松调整大小和位置:

.underline {
  background-image: linear-gradient(#5fca66, #5fca66);
  background-position: bottom center; /*Adjust the background-position to move the line*/
  background-size: 80% 2px; /*Adjust the background size to control length and height*/
  background-repeat: no-repeat;
  padding-bottom: 4px; /* this can also control the position */
}

.small {
  background-size: 50% 1px;
}

.left {
  background-position: bottom left;
}

.center-close {
  background-position: bottom 5px center;
  background-image: linear-gradient(red, red);
}

.right {
  background-position: bottom right;
}

.close {
  padding-bottom: 0;
  background-position: bottom 5px center;
  background-image: linear-gradient(blue, blue);
}

.big {
  background-size: 100% 3px;
}

.far {
  padding-bottom: 10px;
  background-image: linear-gradient(purple, purple);
}

body {
  font-size: 30px;
}
This is a <span class="underline">sentence</span>. I would like <span class="underline close">some words to have</span> longer <span class="underline left">underlines</span> than others. I would <span class="underline big center-close">also like</span> to be able to change the <span class="underline small right">position</span> of the <span class="underline big">underline</span>(to
<span class="underline far">be centered under the word, for example</span>).

【讨论】:

    【解决方案2】:

    这可以使用伪元素来完成。

    .underline {
    
      position:relative;
    
    }
    
    .underline:after{
    content: "";
    position:absolute;
    bottom:-2px;/*position the underline using left and bottom property*/
    left:0;
    width:50px;/* adjust the width of underline as you like*/
    border-bottom:1px solid #ccc;/* adjust the underline width and color as you like*/
    
    }
    &lt;p&gt;This is &lt;span class='underline'&gt;sample&lt;/span&gt; text&lt;/p&gt;

    【讨论】:

      【解决方案3】:
      .underline {
          position: relative;
      }
      .underline:before {
          content: '';
          position: absolute;
          display: block;
          /* you can adjust the properties to your liking */
          bottom: 0;
          left: 0;
          width: 100%;
          border-bottom: 1px solid #5fca66;
      }
      

      【讨论】:

      • 虽然这可能会回答问题,但最好添加一些关于此答案如何有助于解决问题的描述。请阅读How do I write a good answer了解更多信息。
      猜你喜欢
      • 2012-03-24
      • 2022-10-05
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 2016-01-17
      • 2019-06-25
      • 2020-11-18
      • 2021-07-11
      相关资源
      最近更新 更多