Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does display: inline-block; remove an underline from a child element?

Recently I answered a question and the OP wanted text-decoration: underline; for the entire text wrapped inside the a element, but not the one wrapped inside span, so it was something like this

<a href="#"><span>Not Underline</span>Should Be Underlined</a>

So simply giving

span {
   text-decoration: none;
}

doesn't remove the underline for the text wrapped inside a span element

But this does remove the underline

span {
   text-decoration: none;
   display: inline-block;
}

So I made the span an inline-block and it worked, which is how I usually do it. But when it came to explanation I was not able to explain why doing this actually removes the underline where simply using text-decoration: none; doesn't.

Demo

like image 719
Mr. Alien Avatar asked Dec 13 '12 10:12

Mr. Alien


People also ask

What does display block do for an inline element?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

What does the display inline property do?

display: inlineAn element with a display property set to inline will not start on a new line and it will take up the remaining/available screen width. It just takes up the space such an element would normally take.

What's one advantage to using inline-block over inline display?

Compared to display: inline , the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline , top and bottom margins & paddings are not respected, and with display: inline-block they are.

What is the difference between display block and display inline?

inline The element doesn't start on a new line and only occupy just the width it requires. You can't set the width or height. inline-block It's formatted just like the inline element, where it doesn't start on a new line. BUT, you can set width and height values.


1 Answers

Text decorations are propagated from an element to certain descendants in certain cases. The spec describes all the cases in which this happens and doesn't happen (as well as cases where the behavior is explicitly undefined). Here, the following portion is relevant:

Note that text decorations are not propagated to floating and absolutely positioned descendants, nor to the contents of atomic inline-level descendants such as inline blocks and inline tables.

Note that this propagation is not the same as inheritance and is a separate concept entirely; indeed, text-decoration: none and text-decoration: inherit do not affect propagation in the way you'd expect them to:

  • text-decoration: none simply means "this element has no text decorations of its own", and
  • text-decoration: inherit means "this element has the same specified text-decoration value as its parent."

In both situations, parent text decorations will still be propagated to the element where applicable. You can force an inline-block to have the same text decoration as its parent using inherit, but not any other decorations that the parent gains through propagation from its own ancestors.

This also means that simply having display: inline-block is enough to prevent the text decorations from being propagated. You do not need to specify text-decoration: none again — it's already the initial value.

like image 139
BoltClock Avatar answered Sep 20 '22 11:09

BoltClock