The ::after pseudo-element allows you to insert content after the content of an element, which is often used for decorative or additional styling.
p::after {
content: " (Read more)";
color: #007bff;
}
Similar to ::after, the ::before pseudo-element inserts content before the content of an element, useful for creating labels or icons.
button::before {
content: "\f054"; /* Unicode for a font-awesome icon */
font-family: FontAwesome;
}
The ::first-letter pseudo-element targets the first letter of a block-level element, enabling unique styling for initial characters.
p::first-letter {
font-size: 1.5em;
color: #FF5733;
}
The ::first-line pseudo-element selects the first line of text within a block-level element for customized styling.
.quote::first-line {
font-style: italic;
}
The ::marker pseudo-element styles list item markers, such as bullets or numbers, allowing customization of list styles.
ul::marker {
color: #3498db;
}
The ::selection pseudo-element targets the portion of text selected by the user, permitting unique styling for selected text.
::selection {
background-color: #ffa500;
color: white;
}
Applies styles to an element when it is actively being clicked.
button:active {
background-color: blue;
}
Targets checked input elements, like checkboxes or radio buttons.
input[type="checkbox"]:checked {
border: 2px solid green;
}
Styles elements that are disabled.
input:disabled {
opacity: 0.5;
}
Selects elements with no children or text content.
p:empty {
display: none;
}
Targets elements that are enabled (opposite of :disabled).
button:enabled {
background-color: #00FF00;
}
Selects the first child of a parent element.
ul li:first-child {
font-weight: bold;
}
Targets the first element of its type within a parent.
p:first-of-type {
font-style: italic;
}
Styles an element when it gains focus.
input:focus {
border-color: red;
}
Applies styles when the mouse hovers over an element.
a:hover {
text-decoration: underline;
}
Targets input elements with a valid value within a specified range.
input[type="number"]:in-range {
background-color: lightgreen;
}
Styles input elements with invalid data.
input:invalid {
border-color: red;
}
Selects elements with a specific language attribute.
p:lang(fr) {
font-family: 'Helvetica', sans-serif;
}
Targets the last child of a parent element.
ul li:last-child {
font-weight: bold;
}
Selects the last element of its type within a parent.
div p:last-of-type {
color: purple;
}
Styles unvisited links.
a:link {
text-decoration: none;
}
Selects elements that do not match a specific selector.
p:not(.special) {
font-size: 16px;
}
Targets elements based on their position within a parent.
li:nth-child(2n) {
background-color: #ECECEC;
}
Selects elements based on their position from the end within a parent.
div > p:nth-last-child(3) {
font-weight: bold;
}
Styles elements based on their position from the end of a parent element with the same type.
ul > li:nth-last-of-type(odd) {
color: orange;
}
Selects elements based on their position within a parent element with the same type.
ul > li:nth-of-type(3n) {
text-decoration: underline;
}
Styles elements that are the only one of their type within a parent.
p:only-of-type {
font-style: italic;
}
Selects elements that are the only child of their parent.
div:only-child {
border: 1px solid #999;
}
Styles optional input elements.
input:optional {
border-color: lightgray;
}
Targets input elements with values outside a specified range.
input[type="number"]:out-of-range {
border-color: red;
}
Styles input elements that are read-only.
input:read-only {
background-color: #F2F2F2;
}
Styles input elements that are read-write (opposite of :read-only).
input:read-write {
border-color: blue;
}
Selects required input elements.
input:required {
border-color: green;
}
Targets the root element of the document (usually <html>).
:root {
--main-bg-color: lightgray;
}
body {
background-color: var(--main-bg-color);
}
Styles the target element when navigating to an anchor link.
#section-1:target {
background-color: yellow;
}
Selects input elements with valid data (opposite of :invalid).
input:valid {
border-color: green;
}