Text-decoration: underline not applying with inline-block span elements
Asked Answered
U

3

8

I'm having an issue with text-decoration: underline on two spans that use inline-block. The [problem is only one part of the URL will underline when hovered, the other does not. I need to keep the display property, otherwise text-overflow won't get applied (see: Text-overflow: ellipsis alignment issue)

enter image description here

HTML:

<div class="details" itemscope itemtype="http://data-vocabulary.org/Product"> 
   <h2>
       <a class="heading" href="/product/acmesw" title="Acme Super Widget">
           <span class="trunc" itemprop="name">Acme Super Widget 3000</span>
           <span itemprop="offerDetails" itemscope itemtype="http://data-vocabulary.org/Offer">- <meta itemprop="currency" content="AUD" /><spanitemprop="price">$199.95</span></span>     
        </a>
    </h2>
</div>

CSS:

.details {
    width:300px;
    border:1px solid red;
}
.trunc {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width:60%;
}

h2 span {
    display:inline-block;
    vertical-align:top;
}

a:hover{
    text-decoration:underline;
    color:red;
}

jsFiddle: http://jsfiddle.net/c7p8w/2/

Underlayer answered 30/5, 2013 at 3:24 Comment(2)
The core of the problem is that your .trunc is not inheriting text-decoration: underline from its parent <a>. You can simply add .trunc { ..; text-decoration: inherit; .. }. I don't know if you'll be happy with the result though, as the ellipsis still won't have the underline... I don't know how to achieve that.Adjudge
Re: the ellipsis underline problem - it's been asked before and it looks like the only solution is to use border-bottom instead of text-decoration: underline. Sorry.Adjudge
C
2

How about using border-bottom to underline the text?

a {
     text-decoration:none;
    border-bottom: 1px solid blue; /*Remove this if you want it only on hover*/
}
a:hover {
    text-decoration:none;
    border-bottom: 1px solid red;
    color:red;
}

Fiddle

Charlatanism answered 30/5, 2013 at 3:41 Comment(1)
Do you know any tricks to reduce the gap beneath the text and the border to match what text-decoration does since you cannot have negative padding?Underlayer
T
11

By CSS specs, the underline does not affect inline blocks inside the element for which text-decoration: underline has been set.

You can make them underlined by explicitly setting that for them, for example by replacing the selector a:hover in your last CSS rule by the selector list a:hover, a:hover *.

This will not affect the ellipsis symbol, though. It is not part of any element content but inserted by a browser, so I don’t think you can underline it.

Tims answered 30/5, 2013 at 9:5 Comment(0)
C
2

How about using border-bottom to underline the text?

a {
     text-decoration:none;
    border-bottom: 1px solid blue; /*Remove this if you want it only on hover*/
}
a:hover {
    text-decoration:none;
    border-bottom: 1px solid red;
    color:red;
}

Fiddle

Charlatanism answered 30/5, 2013 at 3:41 Comment(1)
Do you know any tricks to reduce the gap beneath the text and the border to match what text-decoration does since you cannot have negative padding?Underlayer
C
0

a little bit weird though...

add these css?

CSS

.details h2{
    border-bottom:1px solid #fff; /*use the same color as your .details background color*/
}
.details h2:hover {
    border-bottom:1px solid #f00;
}

See DEMO on jsfiddel...http://jsfiddle.net/c7p8w/14/

Cyrano answered 30/5, 2013 at 3:44 Comment(1)
Thanks but you get the space between the spans and I'd prefer the line to go full length.Underlayer

© 2022 - 2024 — McMap. All rights reserved.