CSS (Cascading Style Sheets) selectors are patterns used to select elements on a webpage to style them with CSS. An ID selector is a type of CSS selector that selects an element based on its unique id
attribute. In CSS, an ID selector is prefixed with a hash symbol (#
).
CSS ID Selector Syntax
#elementID {
/* CSS rules here */
}
Where elementID
is the value of the id
attribute of the element you want to style.
Example of Using ID Selector in CSS
HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ID Selector Example</title>
<style>
/* CSS ID selector */
#uniqueElement {
color: blue;
font-size: 20px;
}
</style>
</head>
<body>
<p id="uniqueElement">This paragraph will have blue text and a font size of 20px.</p>
<p>This paragraph will display with default styling.</p>
</body>
</html>
In this example, the paragraph (<p>
) with the id
of uniqueElement
will have its text colored blue and its font size set to 20 pixels.
Using ID Selector in JavaScript
You can use the ID selector in JavaScript to select and manipulate an element with a specific ID.
// Select the element with the ID "uniqueElement"
var element = document.getElementById('uniqueElement');
// Use the ID selector with querySelector (CSS-like syntax)
var sameElement = document.querySelector('#uniqueElement');
// Manipulate the element, for example, by changing its text content
element.textContent = 'The text has been changed!';
Using ID Selector in Python (with BeautifulSoup)
Web scraping is a common application of Python where you might use an ID selector. The BeautifulSoup library is a popular choice for parsing HTML and extracting information.
First, install BeautifulSoup if you haven't already:
pip install beautifulsoup4
Then, you can use BeautifulSoup to select an element by ID:
from bs4 import BeautifulSoup
# Assume 'html_doc' contains the HTML content
html_doc = """
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p id="uniqueElement">Some interesting text!</p>
</body>
</html>
"""
# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
# Use the ID selector to find the element with the ID 'uniqueElement'
element = soup.find(id='uniqueElement')
# Print the text of the element
print(element.get_text())
This Python code will output:
Some interesting text!
The find
function in BeautifulSoup allows you to select an element by its ID directly. You could also use a CSS selector with the select_one
method:
element = soup.select_one('#uniqueElement')
The select_one
method is useful when you want to use more complex CSS selectors to find elements. It will return the first element that matches the CSS selector. If you need to find all elements that match the selector, you can use the select
method which returns a list of all matching elements.
In summary, an ID selector is a powerful tool in CSS, JavaScript, and web scraping with Python to select and manipulate elements that have a unique identifier. Remember that IDs should be unique per page, meaning no two elements should have the same ID.