Pseudo-kelas
Pseudo-kelas ngijini sampeyan kanggo milih negara tartamtu utawa posisi saka unsur. Contone, :hover
milih unsur nalika pointer mouse liwat, :focus
pilih unsur nalika dipilih utawa fokus, :nth-child()
pilih unsur anak tartamtu ing grup.
Tuladha:
/* Select all links when hovered over and change the text color */
a:hover {
color: red;
}
/* Select the <input> element when it is focused and change the border */
input:focus {
border: 2px solid blue;
}
/* Select the second element in a group of <li> elements and change the text color */
li:nth-child(2) {
color: green;
}
Pseudo-elemen
Pseudo-elemen ngidini sampeyan nggawe unsur virtual kanggo ngatur unsur sing ana.
Contone, ::before
lan ::after
nggawe unsur sadurunge lan sawise unsur, ::first-line
lan ::first-letter
pilih baris pisanan lan aksara pisanan saka unsur.
Tuladha:
/* Add content before each <p> element and style it */
p::before {
content: ">> ";
font-weight: bold;
color: gray;
}
/* Style the first letter of <h1> element */
h1::first-letter {
font-size: 2em;
font-weight: bold;
color: red;
}
Kombinator
Kombinator ngidini sampeyan nggabungake pamilih kanggo milih unsur adhedhasar hubungane. Contone, element1 element2
milih element2
nang element1
, element1 > element2
milih unsur anak langsung saka element1
, element1 + element2
milih element2
langsung sawise element1
.
Tuladha:
/* Select <span> elements inside an element with class "container" */
.container span {
color: purple;
}
/* Select <li> elements that are direct children of <ul> */
ul > li {
list-style-type: square;
color: blue;
}
Pamilih atribut
Pamilih atribut ngidini sampeyan milih unsur adhedhasar nilai atribute. Contone, [attribute]
milih unsur kanthi atribut attribute
, [attribute=value]
milih unsur kanthi atribut attribute
sing padha karo value
, [attribute^=value]
milih unsur kanthi atribut attribute
sing diwiwiti karo value
.
Tuladha:
/* Select all elements with the attribute data-type */
[data-type] {
font-weight: bold;
color: orange;
}
/* Select all <a> elements with the href attribute starting with "https://" */
a[href^="https://"] {
color: blue;
text-decoration: underline;
}
:not()
pamilih
Pamilih ngidini sampeyan milih unsur sing ora cocog karo pamilih tartamtu. Contone, pilih unsur sing ora duwe kelas, pilih unsur sing ora duwe ID. :not()
:not(.class)
class
:not(#id)
id
Tuladha:
/* Select all <div> elements that do not have the class "hidden" */
div:not(.hidden) {
display: block;
background-color: lightgray;
}
/* Select all <input> elements that do not have the ID "email-input" */
input:not(#email-input) {
border: 1px solid gray;
}
Conto iki nduduhake pilihan unsur lanjutan ing CSS. Sampeyan bisa ngatur lan ngetrapake teknik kasebut kanggo gaya lan ngatur unsur ing kaca web kaya sing dikarepake.