CSS Documentation

Selectors

Selectors

A selector is a component that selects HTML elements. You can use a selector to target HTML elements on a web page.

Example

Here is an example of a selector:

<style>
{Element} {
ㅤㅤ{Property}: {Value};
ㅤㅤ{Property}: {Value};
}
</style>

Types of Selectors

There are several types of CSS selectors:

  • Element Selector: Selects all elements of a specified type.
  • ID Selector: Selects an element with a specific ID.
  • Class Selector: Selects elements with a specific class.
  • Attribute Selector: Selects elements based on an attribute or attribute value.
  • Pseudo-class Selector: Selects elements based on a certain state.
  • Pseudo-element Selector: Selects and styles a part of an element.

Examples of Different Selectors

/* Element Selector */
p {
color: blue;
}

/* ID Selector */
#header {
background-color: black;
}

/* Class Selector */
.highlight {
background-color: yellow;
}

/* Attribute Selector */
[type="text"] {
border: 1px solid gray;
}

/* Pseudo-class Selector */
a:hover {
color: red;
}

/* Pseudo-element Selector */
p::first-line {
font-weight: bold;
}

These are just a few examples of the many selectors available in CSS. Understanding and using selectors effectively is key to writing efficient and maintainable CSS code.