Menu
Coddy logo textTech

Wybieranie ostatniego elementu potomnego

Część sekcji Mistrzowskie opanowanie CSS ścieżki HTML w Coddy. Lekcja 14 z 43.

Pseudoklasa :last-child wybiera elementy, które są ostatnim dzieckiem swojego rodzica.

Na przykład:

<div class="parent">
  <p>First paragraph</p>
  <p>Middle paragraph</p>
  <p>Last paragraph</p>
</div>

Aby nadać styl tylko ostatniemu akapitowi, użyj pseudoklasy :last-child:

p:last-child {
  color: red;
  font-weight: bold;
}

Po zastosowaniu tego CSS tylko „Last paragraph” będzie czerwony i pogrubiony, ponieważ jest ostatnim elementem potomnym nadrzędnego elementu div.

challenge icon

Wyzwanie

Łatwy

Utwórz regułę CSS, aby stylować ostatni element <p> w dowolnej grupie akapitów.

  1. Ustaw dla niego niebieski kolor tła (#046ae0) i biały tekst.
  2. Ustaw grubość czcionki ostatniego akapitu na pogrubioną.

Spróbuj swoich sił

<!DOCTYPE html>
<html>
<head>
    <title>Targeting the Last Child</title>
  <style>
    /* Write your CSS rule here */

  </style>
</head>
<body>
    <div> 
        <h2>Exploring the Universe</h2>
        <p>The universe is vast, with countless stars and galaxies waiting to be discovered.</p>
        <p>New technologies allow us to explore deeper into space, revealing its many secrets.</p>
        <p>We continue to learn more about distant planets and the possibility of life beyond Earth.</p> 
    </div>
    <div>
        <h2>The Deep Ocean</h2>
        <p>Our oceans are mysterious, covering most of Earth, yet we know so little about them.</p>
        <p>Many species live in extreme conditions, some of which are still being discovered today.</p>
        <p>Exploring the ocean depths gives us insights into life at the planet's most remote locations.</p> 
    </div>
</body>
</html>
quiz iconSprawdź się

Ta lekcja zawiera krótki quiz. Zacznij lekcję, żeby na niego odpowiedzieć i śledzić swoje postępy.

Wszystkie lekcje w sekcji Mistrzowskie opanowanie CSS

Poćwicz samodzielnie: Edytor online Web