Menu
CoddyTech

Memilih Anak Terakhir

Termasuk bagian Menguasai CSS dalam perjalanan HTML di Coddy. Pelajaran 14 dari 43.

Pseudo-class :last-child memilih elemen yang merupakan anak terakhir dari induknya.

Contohnya:

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

Untuk memberi gaya hanya pada paragraf terakhir, gunakan pseudo-class :last-child:

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

Setelah menerapkan CSS ini, hanya "Last paragraph" yang akan berwarna merah dan tebal karena merupakan anak terakhir dari div induk.

challenge icon

Tantangan

Mudah

Buat aturan CSS untuk mengatur gaya elemen <p> terakhir dalam kelompok paragraf mana pun.

  1. Atur warna latar belakangnya menjadi biru (#046ae0) dan teksnya menjadi putih .
  2. Atur ketebalan font paragraf terakhir menjadi tebal.

Coba sendiri

<!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 iconUji dirimu

Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.

Semua pelajaran di Menguasai CSS

Latihan sendiri: Playground Web