Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS style heading - background must cover only text with a bottom border

Tags:

html

css

I am trying to style a heading just like this:

enter image description here

But I need some help as I cannot get it correct.

I was planning on using gradient, but I must have only the text of the heading (with some padding) with a background color and gradient changes colour at a specific point in the table cell. The heading can be different words - some long & some short - so the background color cannot be stopped at a specific point. I also need the width of the cell to be underlined (border-bottom).

I am unsure if my HTML needs changing or my CSS needs changing or both or if this can be done.

My HTML code:

<div class="wrapper">
    <div class="row">
        <div class="heading">Heading</div>
    </div>
    <div class="row">
        <div class="stuff">Just some stuff.</div>
    </div>
</div>

My CSS code:

.wrapper {
    border-spacing: 0px 0px;
    display: table;
    width: 100%;
}

.row {
    display: table-row;
}
.heading {
    border-bottom: 2px solid red;
    background-color: lime;
    color: blue;
    direction: ltr;
    display: table-cell;
    font-weight: bold;
    min-height: 25px;
    overflow: hidden;
    padding: 2px;
    padding-left: 30px;
    padding-right: 30px;
    text-align: left;
    text-transform: uppercase;
    vertical-align: top;
}

.stuff {
    width: 100%;
    display: table-cell;
}
like image 212
user1261774 Avatar asked Jun 12 '15 03:06

user1261774


1 Answers

You should adjust the width of your heading to occupy that of only its contents. A common hack is to simply use display: inline-block. I changed a few of your styles and markup to create the lower border effect. Your wrapper and stuff classes were redundant so I removed them.

JSFiddle: http://jsfiddle.net/mkmeLzjL/11/

HTML

<div class="row">
    <div class="heading">Profile</div>
</div>
<br>
<div>
    <div>Just some stuff.</div>
</div>

CSS

@import url(http://fonts.googleapis.com/css?family=Open+Sans);

* {
    font-family: Open Sans;
}

.row {
    border-bottom: 2px solid black;
}

.heading {
    background-color: black;
    color: white;
    display: inline-block;
    font-weight: bold;
    padding: 7px 30px 7px 15px;
    font-size: 20px;
    text-transform: uppercase;
}
like image 145
Henry Zhu Avatar answered Oct 15 '22 13:10

Henry Zhu