Reputation: 93
But here <article>
element has other elements before first <p>
element I tried section article p:first-child
and section article>p:first-child
I think it's wrong so please tell me what is the answer
<section id="news">
<article>
<header>
<h1><a href="item.html">Quisque a dapibus magna, non scelerisque</a></h1>
</header>
<img src="http://lorempixel.com/600/300/business/" alt="">
<p>Etiam massa.</p>
<p>Dut venenatis.</p>
<p>Mauroncus lorem eget.</p>
<footer>
<span class="author">Dominic Woods</span>
<span class="tags"><a href="index.html">#politics</a> <a href="index.html">#economy</a></span>
<span class="date">15m</span>
<a class="comments" href="item.html#comments">5</a>
</footer>
</article>
<article>
......
</article>
<article>
......
</article>
</section>
Upvotes: 1
Views: 222
Reputation: 78
Try To select like belows and changes the properties of p tag:
section article > p:first-of-type {
text-align:center;
}
Upvotes: 0
Reputation: 1329
Try this out
section article > p:first-of-type { background: #121212; color: #fff; }
<section id="news">
<article>
<header>
<h1><a href="item.html">Quisque a dapibus magna, non scelerisque</a></h1>
</header>
<img src="http://lorempixel.com/600/300/business/" alt="">
<p>Etiam massa.</p>
<p>Dut venenatis.</p>
<p>Mauroncus lorem eget.</p>
<footer>
<span class="author">Dominic Woods</span>
<span class="tags"><a href="index.html">#politics</a> <a href="index.html">#economy</a></span>
<span class="date">15m</span>
<a class="comments" href="item.html#comments">5</a>
</footer>
</article>
<article>
......
</article>
<article>
......
</article>
</section>
Upvotes: 0
Reputation: 10879
The :first-of-type
pseudo class should do exactly what you want:
section article>p:first-of-type {
outline: solid red 1px;
}
<section id="news">
<article>
<header>
<h1><a href="item.html">Quisque a dapibus magna, non scelerisque</a></h1>
</header>
<img src="http://lorempixel.com/600/300/business/" alt="">
<p>Etiam massa.</p>
<p>Dut venenatis.</p>
<p>Mauroncus lorem eget.</p>
<footer>
<span class="author">Dominic Woods</span>
<span class="tags"><a href="index.html">#politics</a> <a href="index.html">#economy</a></span>
<span class="date">15m</span>
<a class="comments" href="item.html#comments">5</a>
</footer>
</article>
<article>
......
</article>
<article>
......
</article>
</section>
While :first-child
only matches when the element is truly the first child of its parent element, :first-of-type
will match the first occurrence even if there are other elements beforehand.
Upvotes: 2