CSS underline less than width of headline? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-28 21:32:03

问题


This question already has an answer here:

  • Border length smaller than div width? 11 answers

Is it possible with CSS to make the underline of a headline less wide than the headline text? I have the following style for an H1 headline:

h1 {
  font-weight: 300;
  display: inline-block;
  padding-bottom: 5px;
  border-bottom: 1px #d2202f solid;
}

This produces a thin red underline below my H1 headlines. However, is it possible to make it so that the underline is 50% of the text in the headline?


回答1:


You can use a pseudo element with :before (or :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;
}

http://jsfiddle.net/9e27b/




回答2:


Yes and no.

Normal borders cannot do this, but you can fake it with a CSS3 gradient border.

See: CSS3 Gradient Borders



来源:https://stackoverflow.com/questions/23502675/css-underline-less-than-width-of-headline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!