Contents  >

Styling Web Pages with CSS

Learning Goals

At the end of this Tutorial you will be able to:

You can view finished versions of the four sample web pages you style with four sample CSS files in this Tutorial by clicking the links below. The finished samples will each open in a new tab of your web browser.

Sample image page-1.html
Sample image page-2.html
Sample image page-3.html
Sample image page-4.html

Contents

The common CSS properties

Downloading your four CSS files

Styling your first sample web page

Updating the CSS style rules: sample page one

Styling your second sample web page

Updating the CSS style rules: sample page two

Styling your third sample web page

Updating the CSS style rules: sample page three

Styling your fourth sample web page

Updating the CSS style rules: sample page four

Setting the web page width

Uploading your files to GitHub

The common CSS properties

In the previous Introduction to CSS tutorial you were introduced the CSS properties most commonly used by web designers to format the content of web pages.

Styling Web Pages with CSS

In this Tutorial you will apply these CSS properties to the four web pages you created in the Introduction to HTML Tutorial.

Downloading your four CSS files

Your first task is to download four stylesheet files, one for each of your four sample HTML pages. Here are the steps.

  1. In Google Chrome or Mozilla Firefox Developer Edition, click the following web address:   https://2n2l.com/it4women/exercises/final-styling-css/before/style-1.css.   This first stylesheet file will open in a new tab of your web browser.
  2. Right-click anywhere in the browser window, and from the context menu, choose Save as... (Chrome) or Save Page As... (Firefox). Styling Web Pages with CSS
  3. Save the style-1.css stylesheet file in your websites folder.
  4. Repeat the above steps for the following three other stylesheet files.   https://2n2l.com/it4women/exercises/final-styling-css/before/style-2.css.   https://2n2l.com/it4women/exercises/final-styling-css/before/style-3.css.   https://2n2l.com/it4women/exercises/final-styling-css/before/style-4.css.
  5. Check that your websites folder contains the four sample HTML files from the Introduction to HTML Tutorial and the four downloaded CSS files. Styling Web Pages with CSS

In the remainder of this Tutorial you will work with these four CSS files along with the four corresponding HTML web page files.

Styling your first sample web page

You will use the style-1.css stylesheet file to style the sample page-1.html web page.

  1. In Visual Studio Code, open the page-1.html file.
  2. Before you can apply styles from a stylesheet to a web page, you first need to link the stylesheet flle to that page.   VS Code provides the following shortcut to help you do this.   In the head section of the page-1.html file, at the end of the description details, click with the mouse and press the Enter key to open a new, blank line. Tutorial: Styling HTML with CSS Click at the beginning of this new line, press the letter l key to display a pop-up menu with the link:css option already selected. Tutorial: Styling HTML with CSS
  3. Click this link:css option with the mouse, or press either the Enter key to select it.   VS Code adds the stylesheet link code to your web page. Tutorial: Styling HTML with CSS
  4. As you can see, VS Code gives the default name of style.css to the linked stylesheet file.   Edit the default name to style-1.css.  
    Styling Web Pages with CSS DO NOT type the name of stylesheet file with an upper-case ‘S’ as in Style-1.css.
    And DO NOT use some other mixture of upper and lower-case letters, such as STYLE-1.CSS or style-1.CSS or whatever.
    Also, DO NOT enter any blank spaces in the name of your file: type style-1.css NOT style- 1.css or style -1.css.
    The head section of your web page should now look as shown below. Styling Web Pages with CSS
  5. Save your page-1.html file.

Your page-1.html web page is now linked to your style-1.css stylesheet file.

In your web browser, display the page-1.html web page.

It should look like the web page at the following web address:

page-1.html (before CSS style updates).

You can close your page-1.html web page in VS Code.

Format of the HTML link code

Note that the link from a web page to a stylesheet has two parts: a ref part and a href part.

Either is allowed.

So, this is correct HTML code:

<link rel="stylesheet" href="dark-theme-style.css">  

And so too is this.

<link href="dark-theme-style.css" rel="stylesheet">  

Updating the CSS style rules: sample page one

Let’s update the linked style-1.css stylesheet file and see the effect on your web page.

  1. In VS Code, open the style-1.css file.
  2. For the h1 selector, inside the curly braces, copy-and-paste these extra new style rules:
       text-align: center;
       font-weight: normal;
       letter-spacing: -2px;
       margin-top: 0;
       margin-bottom: 32px;
    
    Your h1 selector and its six style rules should now look as shown below. sample CSS
  3. For the p selector, inside the curly braces, copy-and-paste these extra new style rules:
       font-family: sans-serif;
       line-height: 1.6;
       margin-top: 0;
    
    Your p selector and its four style rules should now look as shown below. sample CSS
  4. Save your style-1.css stylesheet file.

Display your page-1.html web page in your web browser.

It should now look like the web page at the following web address:

page-1.html (after CSS style updates).

You can now close your style-1.css file in VS Code.

Styling your second sample web page

Next, you will apply the style-2.css stylesheet file to the sample page-2.html web page.

  1. In VS Code, open the page-2.html file.
  2. In the <head> of your web page, just before the closing </head> tag, press the Enter key to open a new, blank line, and then copy-and-paste the following stylesheet link to this new line.
       <link rel="stylesheet" href="style-2.css"> 
    
    The <head> section of your web page should now look as shown below. Styling Web Pages with CSS
  3. Save your page-2.html file.

Your page-2.html web page is now linked to your style-2.css stylesheet file.

In your web browser, display the page-2.html web page.

It should look like the web page at the following web address:

page-2.html (before CSS style updates).

You can close your page-2.html web page in VS Code.

Updating the CSS style rules: sample page two

Let’s update the linked style-2.css stylesheet file and see the effect on your web page.

  1. In VS Code, open the style-2.css file.
  2. For the h1 selector, inside the curly braces, copy-and-paste these extra new style rules:
       text-align: center;
       letter-spacing: 4px;
       font-family: sans-serif;
    
    Your h1 selector and its six style rules should now look as shown below. sample CSS
  3. For the h2 selector, inside the curly braces, copy-and-paste these extra new style rules:
       font-family: sans-serif;
       text-align: center;
       line-height: 1.4;
       text-transform: uppercase;
       font-weight: normal;
    
    Your h2 selector and its seven style rules should now look as shown below. sample CSS
  4. For the p selector, inside the curly braces, copy-and-paste this extra new style rule:
       line-height: 1.6;
    
    Your p selector and its three style rules should now look as shown below. sample CSS
  5. Save your style-2.css stylesheet file.

Display your page-2.html web page in your web browser.

It should now look like the web page at the following web address:

page-2.html (after CSS style updates).

You can close your style-2.css file in VS Code.

Styling your third sample web page

Next, you will apply the style-3.css stylesheet file to the sample page-3.html web page.

  1. In VS Code, open the page-3.html file.
  2. In the <head> of your web page, just before the closing </head> tag, press the Enter key to open a new, blank line, and then copy-and-paste the following stylesheet link to this new line.
       <link rel="stylesheet" href="style-3.css"> 
    
    The <head> of your web page should now look as shown below. Styling Web Pages with CSS
  3. Save your page-3.html file.

Your page-3.html web page is now linked to your style-3.css stylesheet file.

In your web browser, display the page-3.html web page.

It should look like the web page at the following web address:

page-3.html (before CSS style updates).

You can close your page-3.html web page in VS Code.

Updating the CSS style rules: sample page three

Let’s update the linked style-3.css stylesheet file and see the effect on your web page.

  1. In VS Code, open the style-3.css file.
  2. For the h1 selector, inside the curly braces, copy-and-paste these extra new style rules:
       text-align: center;
       font-weight: normal;
       letter-spacing: 4px;
    
    Your h1 selector and its five style rules should now look as shown below. sample CSS
  3. For the h2 selector, inside the curly braces, copy-and-paste this extra new style rule:
       text-transform: uppercase;
    
    Your h2 selector and its four style rules should now look as shown below. sample CSS
  4. For the p selector, inside the curly braces, copy-and-paste these extra new style rules:
       font-family: sans-serif;
       line-height: 1.6;
    
    Your p selector and its four style rules should now look as shown below. sample CSS
  5. Save your style-3.css stylesheet file.

Display your page-3.html web page in your web browser.

It should now look like the web page at the following web address:

page-3.html (after CSS style updates).

You can close your style-3.css file in VS Code.

Styling your fourth sample web page

Next, you will apply the style-4.css stylesheet file to the sample page-4.html web page.

  1. In VS Code, open the page-4.html file.
  2. In the <head> of your web page, just before the closing </head> tag, press the Enter key to open a new, blank line, and then copy-and-paste the following stylesheet link to this new line.
       <link rel="stylesheet" href="style-4.css"> 
    
    The <head> of your web page should now look as shown below. Styling Web Pages with CSS
  3. Save your page-4.html file.

Your page-4.html web page is now linked to your style-4.css stylesheet file.

In your web browser, display the page-4.html web page.

It should look like the web page at the following web address:

page-4.html (before CSS style updates).

You can close your page-4.html web page in VS Code.

Updating the CSS style rules: sample page four

Let’s update the linked style-4.css stylesheet file and see the effect on your web page.

  1. In VS Code, open the style-4.css file.
  2. For this web page, the task is to display all the headings, sub-headings and paragraphs of text with a font-family of sans-serif.   You could add the following style rule to each one of the CSS selectors:
       font-family: sans-serif;
    
    But because all the styles on the web page are to have the same font-family value, a quicker option is to add just one new rule to the body selector at top of the stylesheet file.
       body { font-family: sans-serif; }  
    
  3. Save your style-4.css stylesheet file.

Display your page-4.html web page in your web browser.

It should now look like the web page at the following web address:

page-4.html (after CSS style updates).

You can now close your style-4.css file in VS Code.

Setting the web page width

Below are four typical examples of modern web pages that have all their content – both text and images – laid out in a single column.

Tutorial: Styling HTML with CSS

As you can see, all have lots of spacing at the left and right edges of the content. This is called ‘white space’ or ‘negative space’. Its purpose is to make the text more inviting and easier to read.

By default, web browsers add a small amount of ‘white space’ at the left and right edges of the web browser window.

You can see this in the four sample web pages you have created and styled. One example is shown below.

white space in left and right margins

Let’s add some wider, more user-friendly left and right spacing to your four sample web pages.

  1. In VS Code, open all four of the sample stylesheet files:   style-1.css
    style-2.css
    style-3.css
    style-4.css
  2. At the top of each stylesheet file, copy-and-paste this comment line, followed by a new body selector with two style rules.
    /* ==== PAGE WIDTH ==== */
    body {
       padding-left: 20%;
       padding-right: 20%
    }
    
  3. Save your four CSS files.

Display your four web pages, one after the other, in your web browser.

They should look similar to the web pages at the following web addresses:

page-1.html (after left/right spacing added).

page-2.html (after left/right spacing added).

page-3.html (after left/right spacing added).

page-4.html (after left/right spacing added).

In every case, you can see that:

Uploading your files to GitHub

After completing your web pages and stylesheets, your next step is to upload them to your account on GitHub.

  1. Open a new tab in your web browser and go to GitHub.com. If you are not already signed in to your GitHub account, sign in now. github-signin
  2. On your GitHub home page, click the name of the repository (‘repo’) that holds your web pages. Its name will look as follows, where username is your chosen username on GitHub.   username.github.io github-signin
  3. On the next GitHub screen displayed, near the right of the screen, you can see a button named Add file. Click on it. github-upload-portfolio
  4. From the dropdown list displayed, choose the option Upload files. Project Animation Google Fonts
  5. In File Explorer (Windows 10) or Windows Explorer (Windows 7), select or drag-and-drop the following eight files to upload them to your GitHub account.   page-1.html
    page-2.html
    page-3.html
    page-4.html
    style-1.css
    style-2.css
    style-3.css
    style-4.css
  6. Finally, scroll down to the bottom of the GitHub screen, enter a short message in the Commit changes box click the Commit changes button, and wait for the upload to complete. Project Animation Google Fonts

Your web pages are now published on GitHub at web addresses similar to the following, where username is the username you have chosen for your GitHub account:

https://username.github.io/page-1.html
https://username.github.io/page-2.html
https://username.github.io/page-3.html
https://username.github.io/page-4.html

It may take a few minutes for your uploaded files to appear on GitHub.



Return to Contents.