CSS selectors
Created By: chatGPT
In CSS, selectors are patterns used to select elements you want to style. There are several types of selectors, each serving different purposes. Here's a breakdown:
Type Selector selects all elements of a given type. For example, to style all elements:
p {
color: blue;
font-size: 16px;
}
Class Selector selects all elements with a specific class. Prefix the class name with a dot (
.
):.my-class {
background-color: yellow;
padding: 10px;
}
ID Selector selects an element with a specific ID. Prefix the ID with a hash (
#
):#my-id {
border: 1px solid black;
margin: 20px;
}
Attribute Selector selects elements based on an attribute or attribute value. For instance, to style all elements of type
text
:input[type='text'] {
border-radius: 5px;
padding: 8px;
}
Descendant Selector selects elements that are inside another element. For example, to style all elements inside a :
div span {
color: red;
}
Child Selector selects elements that are direct children of another element, using the
>
operator:ul > li {
list-style-type: none;
}
Pseudo-classes allow you to select elements based on their state. For instance, to style a link when hovered:
a:hover {
text-decoration: underline;
}
Pseudo-elements allow you to style a specific part of an element. For example, to style the first line of a paragraph:
Understanding these selectors is essential for effective CSS design. By combining different selectors, you can create powerful and flexible styles for your HTML.p::first-line {
font-weight: bold;
}