Pseudo-klasy
Pseudoklasy pozwalają wybrać określone stany lub pozycje elementu. Na przykład :hover zaznacza element, gdy znajduje się nad nim wskaźnik myszy, :focus zaznacza element, gdy jest zaznaczony lub ma fokus, :nth-child() zaznacza określony element podrzędny w grupie.
Przykłady:
/* 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;
}
Pseudoelementy
Pseudoelementy umożliwiają tworzenie wirtualnych elementów w celu dostosowania istniejącego elementu.
Na przykład ::before utwórz ::after elementy przed i po elemencie, ::first-line a następnie ::first-letter wybierz pierwszą linię i pierwszą literę elementu.
Przykłady:
/* 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;
}
kombinatorzy
Kombinatory umożliwiają łączenie selektorów w celu wybrania elementów na podstawie ich relacji. Na przykład element1 element2 wybiera element2 wewnątrz element1, element1 > element2 wybiera bezpośrednie elementy potomne element1, element1 + element2 wybiera element2 bezpośrednio po element1.
Przykłady:
/* 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;
}
Selektory atrybutów
Selektory atrybutów umożliwiają wybieranie elementów na podstawie wartości ich atrybutów. Na przykład [attribute] wybiera elementy z atrybutem attribute, [attribute=value] wybiera elementy z atrybutem attribute równym value, [attribute^=value] wybiera elementy z atrybutem attribute zaczynającym się od value.
Przykłady:
/* 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() selektor
Selektor umożliwia wybranie elementów, które nie pasują do określonego selektora. Na przykład wybiera elementy, które nie mają klasy, wybiera elementy, które nie mają ID. :not() :not(.class) class :not(#id) id
Przykłady:
/* 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;
}
Te przykłady demonstrują zaawansowany wybór elementów w CSS. Możesz dostosowywać i stosować te techniki do stylizowania i dostosowywania elementów na swojej stronie internetowej zgodnie z potrzebami.

