CSS Selectors are used to find or select HTML element that you want to style
These are divided into categories
Universal Selectors
Selects all elements on the page
Example:
* {
margin: 0;
padding: 0;
}
Type Selector (element)
Selects all elements of the specified type.
Example:
p { color: blue; }
Class Selector (.classname)
Selects all elements with a specific class.
Example:
.box { border: 1px solid black; }
ID Selector (#idname)
Selects the element with the specific ID.
Example:
#header { font-size: 20px; }
Grouping Selector (A, B)
Applies the same styles to multiple selectors.
Example:
h1, h2, p { color: green; }
Descendant Selector (A B)
Selects elements inside another element.
Example:
div p { font-style: italic; }
Child Selector (A > B)
Selects direct child elements only.
Example:
ul > li { list-style-type: circle; }
Adjacent Sibling Selector (A + B)
Selects the element immediately after the specified one.
Example:
h2 + p { color: red; }
General Sibling Selector (A ~ B)
Selects all siblings after a specified element.
Example:
h2 ~ p { font-weight: bold; }
Attribute Selector ([attr])
Selects elements with the given attribute.
Example:
input[required] { border: 1px solid red; }
Attribute with Value ([attr="value"])
Selects elements with a specific attribute value.
Example:
input[type="text"] { width: 150px; }
Pseudo-class Selector (:pseudo-class)
Selects elements in a specific state or condition.
Example:
a:hover { color: orange; }
Pseudo-element Selector (::pseudo-element)
Styles specific parts of an element.
Example:
p::first-letter { font-size: 30px; }
:first-child Selector
Selects the first child of its parent.
Example:
li:first-child { color: blue; }
:last-child Selector
Selects the last child of its parent.
Example:
li:last-child { color: red; }
:nth-child(n) Selector
Selects the nth child of its parent.
Example:
li:nth-child(2) { background: yellow; }
:not(selector) Selector
Selects elements that don’t match the given selector.
Example:
p:not(.info) { color: black; }
:checked Selector
Selects checked checkboxes or radio buttons.
Example:
input:checked { outline: 2px solid green; }
:disabled and :enabled
Selects form elements based on whether they are disabled or enabled.
Example:
input:disabled { background: #eee; }
:empty Selector
Selects elements with no children or text.
Example:
div:empty { display: none; }
The above content is just an introduction to the CSS selectors I know. If I learn anything new, I will share it later. Thank you for reading and for your patience.
Top comments (0)