Here are the basic CSS selectors:
Universal Selector:
*Element Selector:
elementNameClass Selector:
.classNameID Selector:
#idNameAttribute Selector:
[attributeName="value"]Descendant Selector:
ancestor descendantChild Selector:
parent > childAdjacent Sibling Selector:
element1 + element2General Sibling Selector:
element1 ~ element2Pseudo-classes:
:pseudo-classPseudo-elements:
::pseudo-element
These are the essential CSS selectors that you'll frequently use to target and style elements on your web pages. Each selector has its own specific syntax and purpose. Mastering these selectors will give you a strong foundation in CSS styling.
Universal Selector: Selects all elements. ----> * {
/* Styles applied to all elements */ }Element Selector: Selects elements based on their tag name. --------> p {
/* Styles applied to all <p> elements */ }Class Selector: Selects elements based on their class attribute. --------> .classname {
/* Styles applied to all elements with class="classname" */ }ID Selector: Selects a single element based on its ID attribute. --------> #idname {
/* Styles applied to the element with id="idname" */ }Attribute Selector: Selects elements based on the presence or value of their attributes. --------> [attribute="value"] {
/* Styles applied to elements with the specified attribute value */ }Descendant Selector: Selects elements that are descendants of another element. --------> parent descendant {
/* Styles applied to descendant elements */ }Child Selector: Selects elements that are direct children of another element. ------------------------->parent > child {
/* Styles applied to direct child elements */ }Adjacent Sibling Selector: Selects an element that is immediately preceded by a sibling element. ----------> sibling1 + sibling2 {
/* Styles applied to sibling2 immediately preceded by sibling1 */ }General Sibling Selector: Selects elements that are siblings of another element. -----> sibling1 ~ sibling2 {
/* Styles applied to sibling2 that follows sibling1 */ }Pseudo-classes: Select elements based on their state or position. -----------> selector:pseudo-class {
/* Styles applied to elements with the specified pseudo-class */ }Common pseudo-classes include
:hover,:active,:focus,:first-child,:last-child,:nth-child(), and more.Pseudo-elements: Select portions of an element based on specific criteria. ------------> selector::pseudo-element {
/* Styles applied to a specific part of an element */ }Common pseudo-elements include
::before,::after,::first-line, and::first-letter.
These selectors provide you with a powerful toolkit for targeting and styling elements within your HTML documents. Experimenting with different selectors and understanding their specificity will help you become more proficient in CSS styling.
Here's an example demonstrating various CSS selectors:
HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Selector Example</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <h1 class="title">Welcome to CSS Selector Example</h1> <p class="intro">This is a demonstration of various CSS selectors.</p> <ul class="list"> <li>Item 1</li> <li class="special">Item 2</li> <li>Item 3</li> </ul> </div> </body> </html>
In this example:
- We have various HTML elements such as
<h1>,<p>,<ul>, and<li>with different classes and IDs. - In the CSS, we're applying styles using different types of selectors:
- Tag selectors (
h1) - Class selectors (
.intro) - ID selectors (
#special-heading) - Attribute selectors (
[class="special"]) - Descendant selectors (
.container li) - Child selectors (
.list > .special) - Pseudo-class selectors (
li:nth-child(2))
- Tag selectors (
Here's what each selector does:
h1: Selects all<h1>elements and changes their color to blue..intro: Selects elements with the classintroand applies italic font style.#special-heading: Selects the element with the IDspecial-headingand adds underline text decoration.[class="special"]: Selects elements with the classspecialand changes their color to red..container li: Selects<li>elements inside elements with the classcontainerand makes their text bold..list > .special: Selects<li>elements with the classspecialthat are direct children of elements with the classlistand sets their background color to yellow.li:nth-child(2): Selects the second<li>element and increases its font size to 18 pixels.
This example demonstrates how different CSS selectors can be used to target and style specific elements in an HTML document.
0 Comments