1

I have this html:

<div class="leading-4">
<h2>title</h2>
<p>one para</p>
<p>Maybe another para</p>
<p><ul><li>.....</li></ul></p>
<h4>text</h4>
</div>

I want to edit the p that has the ul in it -

I thought about

.leading-4 p:nth-last-child(1){
}

and

.leading-4:nth-last-child(1){
}

but it doesn't' work either (though I didn't think it would)

What am i doing wrong please

maxelcat
  • 1,333
  • 2
  • 18
  • 31

2 Answers2

1

What you want is last-of-type.

But as Praveen says you should fix your HTML. Use the W3C validator when in doubt.

MatTheCat
  • 18,071
  • 6
  • 54
  • 69
0

You cannot have a <ul> inside a <p> tag! So when you put <p><ul></ul></p>, the browser considers it as <p></p><ul></ul>.

So technically, there's and cannot be <ul> inside a <p> so your CSS selector never ever selects an element under any case in HTML.

So, consider changing the element to <div> instead.

Proof:

p {color: red;}
ul {color: blue;}
p ul {color: green;}
<p>This is para</p>
<p>
  The next UL element will be kicked out of P. Before UL.
  <ul><li>This is LI inside UL inside P</li></ul>
  After UL. And this becomes normal text under the body, and not under P.
</p>
<ul><li>This is LI inside UL</li></ul>

References:

Solution:

The solution for selecting the last <p> for you would be:

p:last-of-type { /* rules */}
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252