- have when rendered in the browser?
ul li:first-child { color: red; } ul li:last-child { color: blue; } <ul> <li>Apple</li> <li>Banana</li> <li>Cherry</li> </ul>
The selector ul li:first-child targets the first
- . It sets its color to red. So the first list item 'Apple' will be red.
div p:last-child { background-color: blue; } <div> <p>Paragraph 1</p> <p>Paragraph 2</p> <span>Span element</span> </div>
The last child inside the div is the span element, not a p. Since the selector is div p:last-child, it looks for a p that is the last child. There is none, so no p is selected. The span is last child but not a p.
:first-child and :first-of-type selectors?:first-child matches an element only if it is the very first child of its parent, no matter what type it is. :first-of-type matches the first sibling of its own type, even if it is not the first child overall.
nav ul {
display: flex;
list-style: none;
padding: 0;
margin: 0;
}
nav ul li:first-child {
padding-left: 2rem;
}
nav ul li:last-child {
padding-right: 2rem;
}The flex container is the ul. Its direct children li are flex items. Using :first-child and :last-child on li targets the first and last menu items for styling.
:first-child and :last-child, what accessibility concern might arise?Using color alone to differentiate elements can cause issues for users with color blindness or low vision. Keyboard users might not notice visual differences if focus styles are missing. It's important to ensure sufficient contrast and provide multiple cues beyond color.