Adding CSS to HTML

It is best practice to create a separate document for your CSS code. Here is how to get started:

  1. Create a new file in the folder where your index.html file lives.
  2. Name the new file styles.css.
  3. In the <head> of your index.html file, include the following, then save the file:
    <head>
        …
        <link rel="stylesheet" href="styles.css">
    </head>
  4. Add the following code to your styles.css file, then save the file:
    body {
      background: yellow;
    }
  5. When you open the index.html file, it should have a yellow background!

Basics of CSS

CSS stands for Cascading Style Sheets. CSS defines the styling for webpages written in HTML.

You can add CSS styles to HTML elements according to their tag, or to a selection of HTML elements by defining a class.

tag

To define a CSS style for all elements of a certain tag type, write the tag name (in CSS syntax, this is called the selector), then, inside curly brackets, the CSS styles separated by semi-colons, as follows:

/* comments in CSS look like this */
/* the following CSS makes all h1 elements purple with a monospace font */
h1 {
  color: purple;
  font-family: monospace;
}

class

To define a style for a more specific selection of HTML elements, you can define a class. This requires adding code to both your .html and .css files.

In your .html file, add a class to the desired element(s) by adding a class="" attribute inside of their opening tag(s):

<h1 class="className">I want this heading to be underlined.</h1>
<h2 class="className">I also want this heading to be underlined.</h2>

The className can be any string. I recommend picking a name that describes the function of the class (so for this example, class="underline" might be a better choice).

In your .css file, add a style for the class by typing a period and then the class name as a selector, as follows:

/* the following CSS underlines text in all HTML elements with class="className" */
.className {
    text-decoration: underline;
}

<span> and <div>

For more fine-tuned application of CSS styles, you can use the "invisible" <span> and <div> elements. These elements don't make any changes on their own, but you can use them as containers for adding classes.

<span>

For example, if you want only a small section of text to be green, you can use <span> to add a class without changing any of the surrounding formatting:

<p>
   I only want <span class="greenText">these three words</span> to be green.
</p>

The accompanying CSS could be:

.greenText {
    color: green;
}
and the result would look like this:

I only want these three words to be green.

<div>

If you want to apply a style to a larger section of HTML, <div> is very useful. For example, you might want to combine multiple elements in a <div> and give the entire <div> a pink background:

<div class="pinkBackground">
    <h3>I want these headings...</h3>
    <h4>...in a single pink block.</h4>
</div>

The accompanying CSS could be:

.pinkBackground {
    background: pink;
}
and the result would look like this:

I want these headings...

...in a single pink block.

You can add multiple classes to an HTML element by writing the class names separated by spaces:

<div class="greenText pinkBackground">
    <h3>I want these headings to be green...</h3>
    <h4>...and to be in a single pink block.</h4>
</div>

I want these headings to be green...

...and to be in a single pink block.

Practice

Add style to your index.html webpage by adding to your styles.css file.

Search online to discover styles you can use. Here are some general documentation resources:

More CSS topics

color

You can define color using HEX codes, RGB numbers, or HTML color names.

font

There are a few built-in font styles that you can access via the font-family property (the browser contains defaults for these styles, so they will load quickly).

You also can use the name of a font installed on your computer, or you can add a link to Google fonts (or other online font databases) to get custom fonts.

Here is documentation for all of the CSS font properties.

spacing

Create spacing around HTML elements using margin or padding: margin adds space around the outside of the border of an HTML element, and padding adds space around the content of an HTML element (still within the border).

The relationship between margin, padding, content, and borders is called the "CSS box model" -- here is more documentation.

positioning

The grid and flex CSS display style options offer ways of arranging your content in adjustable rows and columns.

Arranging elements beyond rows and columns can sometimes be tricky. Here is a thorough guide to the position style and the different ways you can instruct HTML elements to interact with each other (are they inline with each other? are they centered relative to the page or to each other? do they overlap each other? do they move as you scroll? etc. etc. etc.)

Meme of Gru from Despicable Me learning to make a website, add HTML, add CSS, and then see the positioning get all messed up

variables

To avoid duplicating code (and so, e.g., you can update a color in just one place instead of re-type every instance in your file), you can use root variables.

inheritance

The "cascading" part of "Cascading Style Sheets" refers to the way that styles are inherited between selectors. Read more here if you're interested in really optimizing your CSS code.