There are two common ways to add CSS to our HTML:
We can add internal CSS directly in the <head>
section by enclosing the code in <style>
tags.
<head>
<style>
h2 {
color: white;
background-color: black;
}
</style>
</head>
A better way to add CSS is to use an external style sheet where we have a separate CSS file and link it to our HTML.
In our head
section, we add:
<head>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
A comment is a useful way to leave notes to ourselves or others if we are working on a project.
A CSS comment starts with /*
and ends with */
/* Comment here*/
Shortcut
We can use a shortcut for comments:
ID is an individual, while Class is a group.
In CSS, the class attribute is referenced using a dot.
.class-name {
/* CSS code */
}
We can include the element before the class name, but we don’t need to.
div.hello {
/* CSS code */
}
ID is unique, so we can only have 1 ID of this name on a page.
In the CSS file, we use hashtag # before the ID name.
#element-id {
/* CSS code */
}
If there are two or more CSS rules that apply to the same element, the selector with the highest specificity will display.
It helps resolve conflicts arising from multiple rules.
Here is the specificity hierarchy
<p style="color: pink;"></p>
.#form-input
..book, :hover, [href]
.h1, :before
.