What are CSS selectors?

CSS selectors are patterns used to select and style HTML elements based on their attributes, structure within the HTML document, or state. They are used in Cascading Style Sheets (CSS) to apply specific styles to elements that match the provided pattern. CSS selectors are also commonly used in web scraping and in JavaScript to manipulate the Document Object Model (DOM) because they provide a convenient way to target elements within a web page.

There are several different types of CSS selectors, including:

Basic Selectors

  • Type Selector (Element Selector): Selects all elements of a given type.
   p {
     color: blue;
   }

This selects all <p> elements and applies blue color to the text.

  • Class Selector: Selects all elements that have a specific class attribute.
   .highlight {
     background-color: yellow;
   }

This selects all elements with the class highlight and gives them a yellow background.

  • ID Selector: Selects a single element with a specific id attribute.
   #header {
     font-size: 24px;
   }

This selects the element with the id header and sets its font size.

  • Universal Selector: Selects all elements in the document.
   * {
     box-sizing: border-box;
   }

This applies the box-sizing property to all elements.

Combinators

  • Descendant Selector: Selects all elements that are descendants of a specified element.
   div p {
     margin-left: 10px;
   }

This selects all <p> elements that are inside a <div> element.

  • Child Selector: Selects all elements that are direct children of a specified element.
   ul > li {
     list-style-type: none;
   }

This selects all <li> elements that are direct children of a <ul> element.

  • Adjacent Sibling Selector: Selects an element that is directly after another specific element.
   h1 + p {
     font-weight: bold;
   }

This selects the first <p> element that directly follows an <h1>.

  • General Sibling Selector: Selects all elements that are siblings of a specified element.
   h1 ~ p {
     color: red;
   }

This selects all <p> elements that are siblings of an <h1> element.

Attribute Selectors

  • Attribute Exists Selector: Selects elements with a specified attribute.
   [disabled] {
     opacity: 0.5;
   }

This selects all elements with the disabled attribute.

  • Attribute Equals Selector: Selects elements with a specified attribute and value.
   [type="text"] {
     background-color: #f0f0f0;
   }

This selects all elements with a type attribute equal to "text".

Pseudo-classes and Pseudo-elements

  • Pseudo-classes: Selects elements based on their state or position.
   a:hover {
     text-decoration: underline;
   }

This selects all <a> elements when the user hovers over them.

  • Pseudo-elements: Selects part of an element.
   p::first-line {
     font-weight: bold;
   }

This selects the first line of all <p> elements.

Using CSS Selectors in Web Scraping and JavaScript

CSS selectors are not only vital for styling web pages but also for web scraping and manipulating the DOM with JavaScript. Here are examples of how CSS selectors can be used in these contexts.

Web Scraping with Python (BeautifulSoup)

from bs4 import BeautifulSoup
import requests

url = "http://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# Use a CSS selector to find all elements with the class 'item'
items = soup.select('.item')
for item in items:
    print(item.text)

Manipulating the DOM with JavaScript

// Use a CSS selector to find the element with the ID 'header'
const header = document.querySelector('#header');

// Use a CSS selector to find and modify all elements with the class 'highlight'
const highlights = document.querySelectorAll('.highlight');
highlights.forEach(element => {
    element.style.backgroundColor = 'lightblue';
});

CSS selectors are a powerful and essential tool for front-end developers, whether for styling, web scraping, or DOM manipulation. They provide a flexible way to access and manipulate elements within a web page.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon