Menu
CoddyTech

Selector Saudara Sebelah

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

Selector saudara yang berdekatan (+) digunakan untuk memberi gaya pada elemen yang muncul tepat setelah elemen tertentu. Keduanya harus memiliki parent yang sama, dan "berdekatan" berarti "tepat berdampingan."

Contohnya:

<h2>Heading</h2>
<p>This paragraph is styled</p>

Dengan CSS ini:

h2 + p {
  font-weight: bold;
}

Paragraf ini diberi gaya karena langsung mengikuti elemen h2.

challenge icon

Tantangan

Mudah

Buat selector CSS yang hanya menargetkan paragraf (<p>) yang langsung mengikuti elemen <h2> yang menjelaskan pertandingan basket. Atur paragraf tersebut agar memiliki warna font biru dan ukuran font 18px.

Coba sendiri

<html>
<head>
    <title>Adjacent Sibling Selector</title>
    <style>
        /* CSS solution goes here */
        
    </style>
</head>
<body>
    <h2>Basketball Game Overview</h2>
    <p>The basketball game was an exciting match between the **Los Angeles Lakers** and the **Golden State Warriors**, featuring intense offense and solid defense from both teams.</p>
    
    <p>During the first half, the Lakers held a slight lead, with LeBron James making key plays, while Stephen Curry of the Warriors kept the game tight with impressive three-pointers.</p>
    
    <p>This paragraph is not directly after an h2, so it should not be affected.</p>
    
    <p>In the second half, the Warriors fought back, with Klay Thompson hitting critical shots, but the Lakers secured their win with solid defense and a game-winning dunk by Anthony Davis.</p>
</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