Tutorial CSS

RUMAH CSS Pengenalan CSS Sintaks CSS Pemilih CSS CSS Bagaimana caranya? Komentar CSS Warna CSS Latar Belakang CSS Batas CSS Margin CSS Lapisan CSS Tinggi/Lebar CSS Model Kotak CSS Garis Besar CSS Teks CSS Font CSS Ikon CSS Tautan CSS Daftar CSS Tabel CSS Tampilan CSS CSS Max-lebar Posisi CSS indeks Z CSS CSS Meluap CSS Float CSS Inline-blok Perataan CSS Penggabung CSS Kelas Pseudo CSS CSS Pseudo-elemen Opasitas CSS Bilah Navigasi CSS Dropdown CSS Galeri Gambar CSS Sprite Gambar CSS Pemilih Attr CSS Formulir CSS Penghitung CSS Tata Letak Situs Web CSS Unit CSS Kekhususan CSS CSS !penting Fungsi Matematika CSS

CSS Lanjutan

Sudut Bulat CSS Gambar Perbatasan CSS Latar Belakang CSS Warna CSS Kata Kunci Warna CSS Gradien CSS Bayangan CSS Efek Teks CSS Font Web CSS Transformasi 2D CSS Transformasi 3D CSS Transisi CSS Animasi CSS Tips Alat CSS Gambar Gaya CSS Refleksi Gambar CSS Kesesuaian objek CSS Posisi objek CSS Penyamaran CSS Tombol CSS Paginasi CSS CSS Beberapa Kolom Antarmuka Pengguna CSS Variabel CSS Ukuran Kotak CSS Kueri Media CSS Contoh MQ CSS CSS Flexbox

CSS Responsif

Perkenalan RWD Area Pandang RWD Tampilan Kotak RWD Kueri Media RWD Gambar RWD Video RWD Kerangka RWD Template RWD

Kotak CSS

Pengenalan kisi-kisi Wadah Kotak Item Kotak

CSS SASS

Tutorial SASS

Contoh CSS

Template CSS Contoh CSS kuis css Latihan CSS Sertifikat CSS

Referensi CSS

Referensi CSS Pemilih CSS Fungsi CSS Referensi CSS Aural Font Aman Web CSS CSS Animasi Unit CSS Konverter CSS PX-EM Warna CSS Nilai Warna CSS Nilai Default CSS Dukungan Peramban CSS

CSS Pseudo-elemen


Apa itu Elemen Pseudo?

Elemen pseudo CSS digunakan untuk menata bagian tertentu dari suatu elemen.

Misalnya, dapat digunakan untuk:

  • Gaya huruf pertama, atau baris, dari suatu elemen
  • Sisipkan konten sebelum, atau setelah, konten elemen

Sintaksis

Sintaks elemen pseudo:

selector::pseudo-element {
  property: value;
}

::elemen Pseudo baris pertama

Elemen ::first-linepseudo digunakan untuk menambahkan gaya khusus ke baris pertama teks.

Contoh berikut memformat baris pertama teks di semua elemen <p>:

Contoh 

p::first-line {
  color: #ff0000;
  font-variant: small-caps;
}

Catatan: Elemen ::first-linesemu hanya dapat diterapkan ke elemen level blok.

Properti berikut berlaku untuk ::first-line elemen semu:

  • properti font
  • sifat warna
  • properti latar belakang
  • spasi kata
  • spasi huruf
  • dekorasi teks
  • vertikal-align
  • transformasi teks
  • tinggi garis
  • jernih

Perhatikan notasi titik dua ganda - ::first-line versus :first-line

Titik dua ganda menggantikan notasi titik dua tunggal untuk elemen semu di CSS3. Ini adalah upaya dari W3C untuk membedakan antara kelas semu dan elemen semu .

Sintaks titik dua tunggal digunakan untuk kelas semu dan elemen semu di CSS2 dan CSS1.

Untuk kompatibilitas mundur, sintaks titik dua tunggal dapat diterima untuk elemen semu CSS2 dan CSS1.



::elemen Pseudo huruf pertama

Pseudo - ::first-letterelement digunakan untuk menambahkan gaya khusus pada huruf pertama dari sebuah teks.

Contoh berikut memformat huruf pertama teks di semua elemen <p>: 

Contoh

p::first-letter {
  color: #ff0000;
  font-size: xx-large;
}

Catatan: Elemen ::first-lettersemu hanya dapat diterapkan ke elemen level blok.

Properti berikut berlaku untuk elemen semu ::first-letter: 

  • properti font
  • sifat warna 
  • properti latar belakang
  • properti margin
  • properti bantalan
  • properti perbatasan
  • dekorasi teks
  • vertical-align (hanya jika "float" adalah "none")
  • transformasi teks
  • tinggi garis
  • mengambang
  • jernih

Elemen semu dan Kelas HTML

Elemen semu dapat digabungkan dengan kelas HTML: 

Contoh

p.intro::first-letter {
  color: #ff0000;
  font-size: 200%;
}

Contoh di atas akan menampilkan huruf pertama paragraf dengan class="intro", berwarna merah dan dalam ukuran yang lebih besar.


Beberapa elemen Pseudo

Beberapa elemen semu juga dapat digabungkan.

Dalam contoh berikut, huruf pertama paragraf akan berwarna merah, dengan ukuran font xx-besar. Sisa dari baris pertama akan berwarna biru, dan dalam huruf kecil. Sisa paragraf akan menjadi ukuran dan warna font default:

Contoh

p::first-letter {
  color: #ff0000;
  font-size: xx-large;
}

p::first-line {
  color: #0000ff;
  font-variant: small-caps;
}

CSS - The ::before Pseudo-element

Elemen ::beforesemu dapat digunakan untuk menyisipkan beberapa konten sebelum konten elemen.

Contoh berikut menyisipkan gambar sebelum konten setiap elemen <h1>:

Contoh

h1::before {
  content: url(smiley.gif);
}

CSS - ::setelah Pseudo-element

Elemen ::aftersemu dapat digunakan untuk menyisipkan beberapa konten setelah konten elemen.

Contoh berikut menyisipkan gambar setelah konten setiap elemen <h1>:

Contoh

h1::after {
  content: url(smiley.gif);
}

CSS - The ::marker Pseudo-element

Elemen ::markersemu memilih penanda item daftar.

Contoh berikut memberi gaya pada penanda item daftar:

Contoh

::marker {
  color: red;
  font-size: 23px;
}

CSS - The ::selection Pseudo-element

Elemen ::selectionsemu cocok dengan bagian elemen yang dipilih oleh pengguna.

Properti CSS berikut dapat diterapkan ke ::selection: color, background, cursor, dan outline.

Contoh berikut membuat teks yang dipilih menjadi merah pada latar belakang kuning:

Contoh

::selection {
  color: red;
  background: yellow;
}

Uji Diri Anda Dengan Latihan

Olahraga:

Atur warna latar belakang menjadi merah, dari baris pertama paragraf.

<style>
 {
  background-color: red;
}
</style>

<body>

<p class="intro">
In my younger and more vulnerable years
my father gave me some advice that I've
been turning over in my mind ever since.
'Whenever you feel like criticizing anyone,' he told me,
'just remember that all the people in this world
haven't had the advantages that you've had.'
</p>

</body>


Semua Elemen Pseudo CSS

Selector Example Example description
::after p::after Insert something after the content of each <p> element
::before p::before Insert something before the content of each <p> element
::first-letter p::first-letter Selects the first letter of each <p> element
::first-line p::first-line Selects the first line of each <p> element
::marker ::marker Selects the markers of list items
::selection p::selection Selects the portion of an element that is selected by a user

Semua Kelas Pseudo CSS

Selector Example Example description
:active a:active Selects the active link
:checked input:checked Selects every checked <input> element
:disabled input:disabled Selects every disabled <input> element
:empty p:empty Selects every <p> element that has no children
:enabled input:enabled Selects every enabled <input> element
:first-child p:first-child Selects every <p> elements that is the first child of its parent
:first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent
:focus input:focus Selects the <input> element that has focus
:hover a:hover Selects links on mouse over
:in-range input:in-range Selects <input> elements with a value within a specified range
:invalid input:invalid Selects all <input> elements with an invalid value
:lang(language) p:lang(it) Selects every <p> element with a lang attribute value starting with "it"
:last-child p:last-child Selects every <p> elements that is the last child of its parent
:last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent
:link a:link Selects all unvisited links
:not(selector) :not(p) Selects every element that is not a <p> element
:nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent
:nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child
:nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent
:only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent
:only-child p:only-child Selects every <p> element that is the only child of its parent
:optional input:optional Selects <input> elements with no "required" attribute
:out-of-range input:out-of-range Selects <input> elements with a value outside a specified range
:read-only input:read-only Selects <input> elements with a "readonly" attribute specified
:read-write input:read-write Selects <input> elements with no "readonly" attribute
:required input:required Selects <input> elements with a "required" attribute specified
:root root Selects the document's root element
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor name)
:valid input:valid Selects all <input> elements with a valid value
:visited a:visited Selects all visited links