Universal Selector
Exercise
- In the "index.html" file, replace the code with the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS</title> <link rel="stylesheet" href="./css/styles.css"> </head> <body> <div> <h1>Section 1</h1> <p>This is the first paragraph of section 1</p> </div> <div> <h1>Section 2</h1> <p>This is the first paragraph of section 2</p> </div> <div> <h1>Section 3</h1> <p>This is the first paragraph of section 3</p> </div> </body> </html>
- Save.
- If "Live Server" is not already running, right-click anywhere in the editor and select "Open with Live Server" from the context menu.
- In the "styles.css" file, replace the code with the following:
* { color: green; }
- Save.
- In the browser, you should see the following:
Code Walkthrough and Explanation
* { color: green; }
The universal selector in CSS is represented by an asterisk (*) and matches any element on a page. This selector has the least specificity so any other selector with conflicting declarations will override it. The universal selector is typically used for overriding the default browser styles.
Experiment with the Code
- Try adding a
body
selector to thecss
before the universal selector and making the color violet. - Try adding a
h1
selector to thecss
after the universal selector and making the color blue.
Video and Code References
Questions? Subscribe and ask in the video comments: