CSS强调低于标题宽度?

the*_*lou 12 css underline

使用CSS是否可以使标题的下划线不比标题文本宽?我有H1标题的以下风格:

h1 {
  font-weight: 300;
  display: inline-block;
  padding-bottom: 5px;
  border-bottom: 1px #d2202f solid;
}
Run Code Online (Sandbox Code Playgroud)

这会在H1头条新闻下方产生一条细红色下划线.但是,是否可以使下划线占标题中文本的50%?

Jam*_*gne 39

您可以使用带:before(或:after)的伪元素:

h1 {
    font-weight: 300;
    display: inline-block;
    padding-bottom: 5px;
    position: relative;
}
h1:before{
    content: "";
    position: absolute;
    width: 50%;
    height: 1px;
    bottom: 0;
    left: 25%;
    border-bottom: 1px solid red;
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/9e27b/

  • 无法处理多行文本... (2认同)