CSS Selectors
- ★ CSS selectors are used to find/select HTML elements based on their element name, id, class, attribute, and more.
CSS element name selector
- ★ The name selector selects elements based on the element name.
- ★ This is what we leart in the previous lecture:
p{ text-align: center; color: red; }
CSS id Selector
- ★ The id selector uses the id attribute of an HTML element to select a specific element.
- ★ The id of an element should be unique within a page, so the id selector is used to select one unique element.
- ★ To select an element with a specific id, write a hash(#) character, followed by the id of the element.
- ★
Note that:
an id name can not start with a number!
#para1 { text-align: center; color: red; }
<p id="para1">Hello World!</p> <p>This paragraph is not affected by the style.</p>
CSS class Selector
- ★ The class selector selects elements with a specific class attribute.
- ★ To select elements with a specific class, write a period(.) character, followed by the name of the class.
- ★ Note that: a class name can not start with a number!
.center { text-align: center; color: red; }
<h1 class="center">Red and center-aligned heading</h1> <p class="center">Red and center-aligned paragraph.</p>
CSS Grouping Selector
- ★ The grouping selector selects all the HTML elements with the same style definitions.
- ★ Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h1 { text-align: center; color: red; } h2 { text-align: center; color: red; } p { text-align: center; color: red; }
h1, h2, p { text-align: center; color: red; }
multiple Selector
- ★ It is possible to nest HTML elements inside one another
<div> <div> <p>This is a paragraph in nested divisions.</p> </div> </div>
div div p { color:red; }
Parents, children, siblings
- ★ If you think of the <html> tag as the trunk of the tree, you can think of its immediate branches <head> and <body> as its children.
- ★ Both tags are children of <html>, and <html> is their parent element. Because they are both immediate children of <html> (that is, they are both only one element away), they are siblings.
first child selector
- ★ It’s used to apply styling to only the elements that are the first children of their parents.
p:first-child{ color: red; }
Example:
✅ Works (First Child)<div> <p>This paragraph will be red.</p> <p>This paragraph will NOT be red.</p> </div>Explanation: The first <p> inside the <div> is the first child, so it turns red.
❌Won't Work (Not First Child)
<div> <h2>Heading</h2> <p>This paragraph will NOT be red.</p> <p>Neither will this one.</p> </div>
Explanation: The <h2> is the first child of <div>, so the first <p> is not affected by p:first-child.
nth child selector
- ★ You can actually select any child of an element after the first child with the selector nth-child; you just add the child’s number in parentheses after the pseudo-class selector.
- ★ For example, the following would turn every paragraph that is the second child of its parent element red.
p:nth-child(2){ color: red; }
Example:
✅ Works (Second Child)<div> <p>First paragraph (not red).</p> <p>Second paragraph (red).</p> <p>Third paragraph (not red).</p> </div>Explanation: The second <p> is the second child of <div>, so it turns red.
❌Won't Work (Not Second Child)
<div> <p>First paragraph (not red).</p> <h1>Heading (not red).</h1> <p>Third paragraph (not red).</p> </div>Explanation: The <h1> is the second child of <div>, so the first <p> and third <p> will not be affected by p:nth-child(2).
Styling HTML Table with nth-child
Country | Capital | Continent |
---|---|---|
United States | Washington, D.C. | North America |
France | Paris | Europe |
China | Beijing | Asia |
Brazil | Brasília | South America |
Nigeria | Abuja | Africa |
Australia | Canberra | Oceania |
Antarctica | None | Antarctica |