Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS underline less than width of headline? [duplicate]

Tags:

css

underline

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?

like image 380
therealbiglou Avatar asked May 06 '14 18:05

therealbiglou


Video Answer


2 Answers

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/

like image 75
James Montagne Avatar answered Nov 16 '22 01:11

James Montagne


Yes and no.

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

See: CSS3 Gradient Borders

like image 39
Diodeus - James MacFarlane Avatar answered Nov 16 '22 00:11

Diodeus - James MacFarlane