CSS Selectors: Targeting Content

CSS selectors are patterns used to select the HTML elements you want to style. Mastering them is crucial for applying styles precisely.

Understanding Selectors

p { color: blue; }
.my-class { font-weight: bold; }
#unique-id { text-align: center; }
Selectors identify which HTML elements to style based on their:
- Type (e.g., <p>)
- Class (e.g., class="my-class")
- ID (e.g., id="unique-id")
- Relationship to other elements

Selectors are the "who" in CSS rules. They determine which elements the declared styles will apply to.

Universal Selector (*)

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
Applies styles to every single HTML element on the page. Often used for resetting default browser styles.

The universal selector selects every element on the page. Use it cautiously, as it can affect performance and unintended elements.

Element (Type) Selector

h1 {
color: #2c3e50;
}

p {
font-family: sans-serif;
}
Targets all instances of a specific HTML tag, like all <h1> tags or all <p> tags.

This is the simplest selector, directly using the HTML tag name.

Class Selector (.)

.button {
background-color: #3498db;
padding: 10px 15px;
}

<button class="button">Click Me</button>
Targets all elements with a specific class attribute. Multiple elements can share the same class.

Defined with a dot (.) followed by the class name. Classes are reusable and flexible.

ID Selector (#)

#header {
background-color: #f0f0f0;
border-bottom: 1px solid #ccc;
}

<div id="header">Website Header</div>
Targets a single, unique element with a specific id attribute. An ID must be unique on a page.

Defined with a hash (#) followed by the ID name. IDs should only be used once per page.

Grouping Selector (,)

h1, h2, h3 {
font-family: Georgia, serif;
text-align: center;
}
Applies the same styles to multiple different selectors, separated by commas. Reduces code repetition.

Used to apply the same declaration to different elements.

Descendant Selector (Space)

div p {
line-height: 1.5;
margin-bottom: 1em;
}

<div class="container">
<p>This paragraph is styled.</p>
</div>
<p>This paragraph is not.</p>
Targets elements that are descendants (anywhere inside) of another element.

A space between two selectors indicates that the second element is inside the first.

Child Selector (>)

ul > li {
list-style-type: square;
}

<ul>
<li>Direct child</li>
<li><span>Nested</span> child</li>
</ul>
Targets elements that are direct children of another element.

The > symbol selects elements that are immediate children of the first element.

Common Selector Errors

/* Missing dot for class */
card { /* Should be .card */ }

/* Missing hash for ID */
main-header { /* Should be #main-header */ }

/* Overly specific (can be simplified) */
body main div .card {
/* .card is usually enough */
}

Pay close attention to syntax (., #) and specificity when choosing selectors.

Practical Use: Page Layout

* {
box-sizing: border-box;
}

body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}

#main-header {
background-color: #333;
color: white;
padding: 20px;
text-align: center;
}

.card {
background-color: white;
margin: 15px;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.card h2 {
color: #007bff;
}

ul > li.important {
font-weight: bold;
color: red;
}
A styled webpage demonstrating:
- Universal box-sizing reset
- Basic body and header styling
- Reusable card styles
- Specific styling for an important list item

Your Task: Selector Practice

Apply CSS to the provided HTML using various selectors:

  1. Use the universal selector (*) to set margin and padding to 0 on all elements.
  2. Style all <h1> and <h2> elements with a color of #0056b3 and text-align: center;.
  3. Give all elements with the class .card a background-color of #e9ecef and border-radius: 5px;.
  4. Style the element with the ID #main-header to have a background-color of #343a40 and color: white;.
  5. Make any <p> element that is a descendant of a .card have font-style: italic;.
  6. Make only the direct child <li> elements of a <ul> have a list-style-type of circle.
  7. Style the <li> with the class .important to have color: orange; and text-decoration: underline;.

Remember: Choose the most appropriate selector for each styling task.

Section 1/11Understanding Selectors

Understanding Selectors

Selectors are the "who" in CSS rules. They determine which elements the declared styles will apply to.

CSS Selectors: Targeting Content

CSS selectors are patterns used to select the HTML elements you want to style. Mastering them is crucial for applying styles precisely.

Understanding Selectors

p { color: blue; }
.my-class { font-weight: bold; }
#unique-id { text-align: center; }
Selectors identify which HTML elements to style based on their:
- Type (e.g., <p>)
- Class (e.g., class="my-class")
- ID (e.g., id="unique-id")
- Relationship to other elements

Selectors are the "who" in CSS rules. They determine which elements the declared styles will apply to.

Preview