Why is my code not working as intended? background-color of the first article must be green. How I can do it without classes or id? PS I'm sorry but my post needs more text. I'm sorry but my post needs more text.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>stack</title>
<link rel="stylesheet" href="css/style_new.css">
</head>
<body>
<section>
<h1>This is H1 header</h1>
<article>
<p>
Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet
Lorem ipsum dolor sit amet Lorem ipsum dolor sit
</p>
</article>
<article>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
</p>
</article>
<article>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
</p>
</article>
</section>
</body>
</html>
CSS code:
section article:first-child {
background-color: green;
}
section article:last-child {
background-color: red;
}
Actually first child of section tag is h1 not article. So use below code instead,
section article:first-of-type {
background-color: green;
}
article is not the first-child of section - its h1
the first/last child selector does only target if article is the first or last child of its container.
possible css:
section article{
background-color:green;
}
section article + article{
background-color:transparent;
}
section article:last-child {
background-color: red;
}
![enter image description here][1]
h1 required to remove the plug out of the section
<article>
<p>
Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet
Lorem ipsum dolor sit amet Lorem ipsum dolor sit
</p>
</article>
<article>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
</p>
</article>
<article>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
</p>
</article>
</section>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.