CSS Comments
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> <h1>CSS Intro</h1> <p>This is my first time using CSS</p> <p>CSS makes the website look good</p> <p>List of things I'm learning</p> <ul> <li>HTML</li> <li>CSS</li> </ul> <a href="https://www.google.com">Link to Google</a> </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, add the following code:
h1 { color: blue; }
- Save.
- In the "styles.css" file, add a comment above the h1 selector and some comments inside the declaration area so that it looks like the following:
/* This is a comment in CSS */ h1 { /* Commenting out the color blue */ /* color: blue; */ color: red; }
- Save.
- In the browser, you should see the following:
Code Walkthrough and Explanation
/* This is a comment in CSS */
In CSS, comments are used to add annotations or explanations within the stylesheet code. They allow you to document your code and make it easier for others to understand and maintain. Comments are ignored by the browser and do not affect the styling of the web page. You can add comments on a single line or multiple lines. Adding comments to your CSS code can help you keep track of important information about your styles or temporarily disable certain styles for testing purposes.
Experiment with the Code
- Try adding a few comments after the h1 selector.
- Try commenting out the entire block of css.
Video and Code References
Questions? Subscribe and ask in the video comments: