⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

CSS Selectors

CSS Selectors point to the HTML element we want to style, the declaration block contains statements separated by semicolons. Each declaration includes a CSS property and value, separated by a semicolon. While declarations end with a semicolon, blocks end with curly braces. Selectors serve to "find" HTML elements we want to transform, and we divide them into five categories.

EXAMPLE
RESULT

Above, I show you an example webpage that combines different types of CSS selectors in practical use. The example shows a simple company webpage where you can see the operation of simple selectors (elements, classes, IDs), combinators (descendant, child, adjacent, general sibling), pseudo-classes and pseudo-elements. The page contains a heading, navigation, and content sections where you can observe how different selectors affect element styling. Particularly interesting are hover effects on links, first letter styling of paragraphs, and special marking of important text.

Selector Example Description
Simple Selectors
** { } Universal selector – selects all elements
elementp { } Selects all elements with the specified name
.class.heading { } Selects all elements with the specified class
#id#header { } Selects the element with the specified ID
element.classp.description { } Selects elements with the specified class
element, elementh1, p { } Grouping selector – selects multiple elements
Attribute Selectors
[attribute][href] { } Selects elements with the specified attribute
[attribute="value"][type="text"] { } Selects elements with the exact attribute value
[attribute~="value"][class~="active"] { } Selects elements whose attribute contains the word
[attribute|="value"][lang|="en"] { } Selects elements with attribute starting with the word
[attribute^="value"][href^="https"] { } Selects elements whose attribute starts with value
[attribute$="value"][href$=".pdf"] { } Selects elements whose attribute ends with value
[attribute*="value"][href*="tutorial"] { } Selects elements whose attribute contains the value
Combinators
element elementdiv p { } Descendant – selects all p inside div
element > elementdiv > p { } Child – selects only direct children
element + elementdiv + p { } Adjacent sibling – selects element immediately after
element ~ elementdiv ~ p { } General sibling – selects all siblings after
Pseudo-classes
:activea:active { } Selects an active link
:checkedinput:checked { } Selects checked input elements
:disabledinput:disabled { } Selects disabled elements
:emptydiv:empty { } Selects elements without content
:enabledinput:enabled { } Selects enabled elements
:first-childp:first-child { } Selects the first child of a parent
:first-of-typep:first-of-type { } Selects the first element of its type
:focusinput:focus { } Selects the element with focus
:hovera:hover { } Selects element on mouse hover
:in-rangeinput:in-range { } Selects elements with value in range
:invalidinput:invalid { } Selects elements with invalid value
:lang()p:lang(sl) { } Selects elements with specified language
:last-childp:last-child { } Selects the last child of parent
:last-of-typep:last-of-type { } Selects the last element of its type
:linka:link { } Selects unvisited links
:not():not(p) { } Selects all elements except specified
:nth-child()p:nth-child(2) { } Selects nth child of parent
:nth-last-child()p:nth-last-child(2) { } Selects nth child from the end
:nth-last-of-type()p:nth-last-of-type(2) { } Selects nth element of type from end
:nth-of-type()p:nth-of-type(2) { } Selects nth element of specified type
:only-childp:only-child { } Selects the only child of parent
:only-of-typep:only-of-type { } Selects the only element of its type
:optionalinput:optional { } Selects optional form elements
:out-of-rangeinput:out-of-range { } Selects elements with value out of range
:read-onlyinput:read-only { } Selects read-only elements
:read-writeinput:read-write { } Selects read-write elements
:requiredinput:required { } Selects required form elements
:root:root { } Selects the root element of document
:target:target { } Selects the current active URL target
:validinput:valid { } Selects elements with valid value
:visiteda:visited { } Selects visited links
Pseudo-elements
::afterp::after { } Inserts content after the element
::beforep::before { } Inserts content before the element
::first-letterp::first-letter { } Selects the first letter of text
::first-linep::first-line { } Selects the first line of text
::selection::selection { } Selects user-highlighted text