Lonely
Lonely

Reputation: 613

Align underline under the texts in a horizontal div

In this Question, I followed w3d user answer but didn't get divs under the top divs.

CSS

div.demo {
    display:table;
    width:100%;
}
div.demo span {
    display:table-cell;
    text-align:center;
}
#under {
    width:100px;
    height:2px;
    background-color:blue;
}

HTML

<div class="demo">
    <span>Span 1</span>
    <span>Span 2</span>
    <span>Span 3</span>
</div>
<div class="demo">
    <span><div id='under'></div></span>
    <span><div id='under'></div></span>
    <span><div id='under'></div></span>
</div>

JSFiddle

Upvotes: 1

Views: 158

Answers (2)

thgaskell
thgaskell

Reputation: 13226

I know you've already accepted an answer, but maybe consider using border-spacing and border-bottom?

JSFiddle

CSS

div.demo {
    border-spacing: 75px 0;
    display: table;
    width: 100%;
}

div.demo span {
    border-bottom: 2px solid blue;
    display: table-cell;
    text-align: center;
}

HTML

<div class="demo">
    <span>Span 1</span>
    <span>Span 2</span>
    <span>Span 3</span>
</div>

Upvotes: 1

Love Trivedi
Love Trivedi

Reputation: 4046

Try this css for under id

#under {
    width:100px;
    height:2px;
    background-color:blue;
    margin:0px auto; /*ADDED*/
}

Note: Use class instead of ID because you are using under id many times in single page.

Here is a valid HTML

<div class="demo">
    <div>Span 1</div>
    <div>Span 2</div>
    <div>Span 3</div>
</div>
<div class="demo">
    <div><span class="under"></span></div>
    <div><span class="under"></span></div>
    <div><span class="under"></span></div>
</div>

And here is CSS

div.demo {
    display:table;
    width:100%;
}
div.demo div {
    display:table-cell;
    text-align:center;
}
.under {
    width:100px;
    height:2px;
    background-color:blue;
    margin:0px auto;
    display:block;
}

DEMO HERE

Upvotes: 1

Related Questions