- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Selectors
Selectors in CSS are patterns used to select and style HTML elements based on various criteria. Think of selectors as the "address" for your styles; they tell the browser exactly which part of the HTML structure should receive specific colors, fonts, or layouts. Mastering selectors is the first major step toward writing efficient, maintainable CSS that doesn't feel like a constant battle against the browser.
- Element Selector (element): Targets all HTML elements of a specific type. This is the broadest way to apply styles. For example, p selects every <p> tag on your page, while h1 applies to every primary heading. These are perfect for setting "global" defaults, like the base font-family for your body or the margin for all paragraphs.
p {
color: #333;
line-height: 1.6;
font-size: 16px;
}
- Class Selector (.class-name): Targets elements with a specific class attribute value. Unlike element selectors, classes are reusable and precise. You can apply the same class to a variety of different HTML elements to give them a consistent look.
.highlight {
background-color: #fff3cd;
padding: 2px 5px;
border-radius: 4px;
font-weight: bold;
}
- ID Selector (#id-name): Targets a single element with a specific ID attribute value. IDs are meant to be unique there should only be one element with a particular ID on an entire webpage.
#header {
background-color: #2c3e50;
color: white;
padding: 20px;
display: flex;
}
- Attribute Selector ([attribute=value]): Targets elements based on the presence or value of an HTML attribute. This is incredibly powerful for styling forms or links without adding extra classes to your HTML.
/* Styles only the "Submit" buttons */
[type="submit"] {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
/* Styles links that point to a PDF */
a[href$=".pdf"] {
color: red;
font-weight: bold;
}
Pseudo-classes and Pseudo-elements: These are "special" selectors that target states or specific parts of an element. Pseudo-classes (starting with one colon, like :hover) target elements based on their state for example, when a user moves their mouse over a link. Pseudo-elements (starting with two colons, like ::before) allow you to style specific parts of an element, like the first letter of a paragraph or adding decorative icons.
/* State: Change link color when hovered */
a:hover {
color: #ff4500;
text-decoration: underline;
}
/* Position: Style the first item in a list differently */
li:first-child {
font-weight: bold;
color: #2ecc71;
}
/* Part: Add a bullet point using CSS */
li::before {
content: "→ ";
color: #3498db;
}
- Combining Selectors: You can chain selectors together to create highly specific rules. This is useful when you want to style an element only if it exists inside a specific container.
/* Targets only h1 tags that also have the .title class */
h1.title {
font-size: 2rem;
border-bottom: 2px solid #ccc;
}
/* Targets only paragraphs that are descendants of a .container div */
.container p {
margin-bottom: 1.5em;
color: #555;
}
Selectors play a crucial role in CSS as they allow you to apply styles precisely to the elements you want, making your web pages visually appealing and well-structured. Understanding the balance between broad element selectors and specific class selectors is fundamental to building professional websites. As you grow, you'll find that clever use of selectors can significantly reduce the amount of HTML code you need to write.