5

My head is about to explode on this one. Check this fiddle please:

http://jsfiddle.net/337C7/1/

section ul li {
    text-decoration: underline;
}

section ul li ul li {
    text-decoration: none !important;
}

Basically I have unordered list and inside that another one. Im just trying to remove the text-decoration on the children ul. For whatever reason it doesnt want to work.. Am I stupid?

6
  • 1
    Pretty weird, check this out: jsfiddle.net/337C7/17 Commented Nov 26, 2013 at 22:10
  • 1
    How about wrapping your underline text in <span /> tags and applying the underline to those? Commented Nov 26, 2013 at 22:14
  • Span would be my suggestion also - especially if that second ul is related to the li it's inside. Commented Nov 26, 2013 at 22:21
  • How about using a border instead of text-decoration (float and clear also required)? Commented Nov 26, 2013 at 22:31
  • @MatthewRapati Yeah thats really weird stuff Commented Nov 26, 2013 at 22:34

7 Answers 7

4

According to MDN:

Text decorations draw across descendant elements. This means that it is not possible to disable on a descendant a text decoration that is specified on one of its ancestors.

For example, in the markup: <p>This text has <em>some emphasized words</em> in it.</p> the style rule:p { text-decoration: underline } would cause the entire paragraph to be underlined. However, the style rule: em { text-decoration: none } would not cause any change; the entire paragraph would still be underlined. (However, the rule em { text-decoration: overline } would cause a second decoration to appear on "some emphasized words".)

Sign up to request clarification or add additional context in comments.

3 Comments

1+ Nice find. I was looking for that.
+1. I borrowed this sample for my solution - hope you won't mind.
Using a span inside the li elements would prevent this. Then it would work similar to dropdown menus.
2

While the browser might render it without an issue, certain browsers will render the text-decoration, and get confused by the containing element (in this case, the <li>. This fact is proven by @Matthew Rapati's comment). For example, this is from the MDN article on the issue (borrowed from j08691's answer for completeness):

Text decorations draw across descendant elements. This means that it is not possible to disable on a descendant a text decoration that is specified on one of its ancestors.

For example, in the markup: <p>This text has <em>some emphasized words</em> in it.</p> the style rule: p { text-decoration: underline } would cause the entire paragraph to be underlined. However, the style rule: em { text-decoration: none } would not cause any change; the entire paragraph would still be underlined. (However, the rule em { text-decoration: overline } would cause a second decoration to appear on "some emphasized words".)

You should simply place the child <ul> as a sibling, rather than child of the <li>, for example:

<ul>
    <li>bla</li>
    <li>bla bla </li>
    <ul>
        <li>bla bla bla</li>
        <li>bla bla bla bla </li>
        <li>bla bla bla bla bla</li>
        <li>bla bla bla bla bla bla</li>
    </ul>
</ul>

You should then update your selector as follows:

section ul ul li {
    text-decoration: none !important;
}

You can see a working jsFiddle Demo

5 Comments

But that's invalid HTML?
I thought the only child of a <ul> could be an <li>
Correct. But as we've highlighted already by the comment from MDN, it's the only way to solve your issue.
Wrong text items. Take a look at my answer.
So probably the best way would be to add a span right? Since <li>bla bla</li><ul> is not correct, right?
0

The text-decoration none is working. But the the unclosed li above is affecting the li bellow.

Comments

0

Add a span element around the text inside the first set of li elements. Then change the css so you target the span instead of the li. If you add a class to the li elements, your css will be easier to read.

http://jsfiddle.net/337C7/27/

html:

<ul class="first">
                        <li class="first-item"><span>bla</span></li>
                        <li class="first-item"><span>bla bla </span>
                            <ul class="second">
                                <li>bla bla bla</li>
                                <li>bla bla bla bla </li>
                                <li>bla bla bla bla bla</li>
                                <li>bla bla bla bla bla bla</li>
                            </ul>
                        </li>
                    </ul>

css:

.first-item > span{text-decoration: underline;}

Comments

0

Try this: http://jsfiddle.net/337C7/30/

HTML:

                  <ul>
                        <li>bla</li>
                        <li>bla bla </li>
                            <ul>
                                <li>bla bla bla</li>
                                <li>bla bla bla bla </li>
                                <li>bla bla bla bla bla</li>
                                <li>bla bla bla bla bla bla</li>
                            </ul>
                      </ul>

CSS

section ul ul li {
    text-decoration: none !important;
}

1 Comment

But thats not valid HTML I assume, It might break something else.
0

You could use a border instead of text-decoration. You'll need to float and clear as <li> is block level. This way you don't have to change any HTML.

section ul li {
  border-bottom: 1px solid #000;
  float: left;
  clear: both;
} 

section ul ul li {
  border: none; 
}

http://jsfiddle.net/337C7/33/

Comments

0
section ul li /* descendants including direct children */ {
    text-decoration: underline;
}

section>ul>li /* children */ {
    text-decoration: none;
}

use children selector for the children after you style the descendants. I'm not sure if you wanted the children to be decorated or the descendants of the children so my rules may be reversed. But the children selector goes after the descendants and then should not require an !important rule.

Update per your fiddle

col-md-12 col-sm-12 >ul>li /* children */ {
        text-decoration: none;
    }

or

section>div>div>div>ul>li /* children */ {
    text-decoration: none;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.