Search the guide

3.1

Overview & Goals

trophy icon At the end of this part, you will be able to:
  • Understand what CSS is and its relationship with HTML.
  • Use basic CSS to modify the style and layout of your website.
  • Get a basic knowledge of Bootstrap and understand its benefits.

Contents of this part

In this third chapter you’ll learn how to style your website using CSS. You’ll also be introduced to Bootstrap which is a “library” of CSS components that you can freely use.

Do the work

For the assignment, and as part of our third workshop, you must style up your CV/résumé.

The requirements are that you must customize at least:

  1. One font colour. This can be done in two ways, either by writing CSS code, or by using Bootstrap
  2. One background colour, which can also be done both ways
  3. One font, where you will need to use an external service such as Google Fonts, Adobe Fonts or Fontshare.

Another requirement is that you must make at least one of these changes by creating a new, custom CSS class.

These skills will be essential as you start working on the group assignment, which must use styles to convey the style guide and personality of your brand.

3.2

CSS Basics

Summary
  • Learn what CSS is and why we need it for styling pages.
  • Learn the vocabulary of CSS: rules, selectors, properties.
  • Learn how to link HTML pages and CSS stylesheets together.

HTML and CSS: Separating content and style

Cascading Style Sheets (CSS) are a powerful way of modifying the look and feel of a website. Although it is tightly integrated with HTML, it uses its own computing language and syntax. This supports a very powerful pattern whereby the content and its logical structure (e.g. which item is a heading, and which one is a paragraph) is separated from its visual aspect (e.g. how big is that heading, how it is aligned, what colour is it?)

What is content without style?

Click on the button below to see what this page would look like using pure HTML and no CSS at all!

You will notice that everything on the page is still legible and visible, and that headings are still bigger than regular paragraph text.

This is because your browser has some built-in styles – but they are not visually appealing, so very few websites use them.

After you’ve had a look at the “basic” style of this page, scroll back and click on the button again

An example: The CSS Zen Garden

The CSS Zen Garden is a perfect example of what it means to separate content and style. All 218 pages of this website use the exact same HTML code, but different CSS code. See for yourself!.

screenshoot zen garden 214

CSS Zen Garden with the “Verde Moderna” Style

screenshoot zen garden 219

CSS Zen Garden with the “Steel” Style

screenshoot zen garden 220

CSS Zen Garden with the “Garments” Style

screenshoot zen garden unstyled

CSS Zen Garden unstyled

If you want to draw inspiration from these pages, don’t hesitate to “inspect” the page (as you learned in section 2.3 Use Developer Tools).

The CSS language

What does it look like?

This is a line of CSS code that turns all paragraphs pink:

css syntax

CSS and HTML are both programming languages used for the web, but they couldn’t look more different.

  • HTML uses a lot of “less-than” (<), “greater-than” (>) and “slashes” (/)
  • CSS’ hallmark are the curly braces: { and }

Important Definitions

  • A Rule is a piece of CSS code (one or more lines) which tells the browser to give specific styles to specific HTML elements
  • A Selector is the part of the rule which says which elements the rule applies to.
  • A Property is an element of style that should be modified, for example the colour of the text, or the size of the font.
  • A Value specifies how this element is modified. It can be a specific colour (pink, #ffcccc, etc.), a size (12pt), etc.

Linking CSS Files

Although it is possible to put CSS code in HTML files, we will follow the best practices and we won’t “mix and match”.

We will therefore put our CSS code in a separate file with the .css extension. This brings several benefits:

  • Styles from one HTML page can be reused in another one.
  • It uses less bandwidth and disk space because the styles will be only downloaded once for all the pages.
  • It makes it easier for teams of multiple developers to work on different parts of the code (while keeping style consistency across the website).

Because the CSS rules are “elsewhere”, our browser needs to know where to find them when it loads the HTML file. This is why we add a <link> element in the head of our HTML file.

ilustration html referencing css

Write a <link>

The HTML file needs to have reference the CSS file using the <link> tag. The “href” attribute uses the same logic as in <a> links or the “src” of an image, as explained in part 2.

index.html

<!doctype html>

<html>

<head>

<title>This is the page's title</title>

<link rel="stylesheet" type="text/css" href="css/styles.css">

</head>

<body>

<p class="myclass"> This is a paragraph</p>

</body>

</html>

styles.css

p {

color: pink;
font-size: 12px;

}

Match HTML elements with CSS Rules

How can you define to which elements a set of CSS rules apply? This is done through a CSS selector.

There are different types of selectors, in the table you can see the most commonly used and how they work.

While each type of selector is relevant due to its characteristics, class selectors are very useful since you can reuse them when you need a specific style (different html elements can use the same class attribute). For example, this paragraph has the class “my-class” (you will see the implication very soon).

The basic selector types are:

  • HTML Element: selects all HTML elements of a given type
  • Class: selects all HTML elements with a given class attribute. Prefix the class name with a dot (.) when using this selector in the CSS file. This list item has the class “my-class”
  • ID: selects an HTML tag with a given ID attribute. An ID can only be used once in a document. Prefix it with a # when using this selector in the CSS file. This item has the id “my-id”

You can combine these selectors for more complex effects (e.g. selecting the first paragraph of each div with a certain class), but the three above should be sufficient for the assignment.

Selector examples

Selector type CSS code HTML code that gets selected
HTML Element

p { color: green; background: pink; }

<p>A paragraph</p>
Class

.my-class { color: green; background: pink; }

<h2 class="my-class" >A heading</h2>
<p class="myclass other-class" >A paragraph</p>
ID

#my-id { color: green; background: pink; }

<p id="my-id" >A paragraph</p>

3.3

Practice: Edit styles.css

Summary
  • Our templates already contain custom styles, you can modify them easily.
  • To add another style, add a class, then change its CSS properties.

Overview

You will change the style of several elements in your CV

In this example, we will create a class that makes the text purple, italic, aligned at the centre and with an orange underline.

If you are using the prj4-cv template, you do not need to link the HTML and CSS files, we’ve already done it for you!

To do so, we will follow these steps

  1. Open the relevant HTML and CSS files.
  2. Create a new class in CSS.
  3. Apply this class where we want to use it.
  4. Edit the properties of these text.

Step 1: Open the files

Start by opening the files in your copy of the CV/résumé template

You will be modifying two files in parallel:

  • index.html to add classes where you want them.
  • css/styles.css to edit the CSS properties of the classes.

Dreamweaver will let you see the results in real time, but if you are using another text editor, or if you want to check the result in a browser, it’s important to save both files to see the changes you make.

Step 2: Create a new class

Let’s open the CSS file and create a new, empty class. I’ll call it “funky-text” but you can choose any name that you find relevant.

css/styles.css

.funky-text {

}

Please note that the dot at the start of “.funky-text” is only used in CSS to indicate that this is a class selector.

In the HTML file (see step 3), you will use the class attribute without the dot!

Step 3: Add the class where you want it

At the moment, we haven’t changed the properties of the class, but we can already decide where we want to use it. For example, in our CV template, this could be your own name, or the title of a job description.

Locate the HTML element whose style you want to change, and add the class. There are two possibilities:

  • The element doesn’t have a class yet, then add the class="funky-text" attribute.
  • The element already has one or more classes, then add funky-text to the list of classes (classes are separated by spaces).
index.html

<h1 class="fst-bolder funky-text">Firstname Lastname</h1>

<h2 class="funky-text">Professional Experience</h2>

Step 4: Add CSS properties

Now it’s time to play around and decide the style you want to give to your funky text!

We are using 4 properties here: color for the font colour, text-align to centre the text horizontally, font-style for the italics, and text-decoration for the underline.

css/styles.css

.funky-text {

color: #8020cc;

text-align: center;

font-style: italic;

text-decoration: underline #ffcc00;

}

You will notice that colours are given using ‘hex codes’ combining 6 numbers and/or letters from A to F. Hex Codes are short representations of the Red, Green and Blue values for each colour. If you create a colour palette using Adobe Color, you will be able to copy the Hex Codes directly.

Congratulations!

Save all your files, open them in the browser, see if this works!

The properties we’ve used so far are just a few examples, you can find more on the Mozilla Developer Network or on W3Schools.

When adding styles to your document, you will notice that there are already many classes being used, that are not even part of our styles.css. That’s because they come from Bootstrap, which is explained in the next section.

Final note: the Box Model

One important use of CSS is to align elements, and to provide sufficient spacing between elements to improve legibility.

To do so, CSS relies on a concept named the box model, where each element can have three layers around it which are named, from the centre to the periphery, the padding, the border and the margin.

The difference between these three is that the padding is linked to the content and will share the same background, the border can have its own colour, and the margin is considered to be outside and will use the background of its container.

Margin

Border

Padding

Content

You can adjust the size of each of these layers, on all four sides or independently

Example code:

css/styles.css

.my-box {

background: #f0ccff;

padding-top: 10px;

padding-bottom: 30px;

border: 4px dashed #20cc20;

margin: 20px;

}

Hello! This element has symmetrical margins of 20px on all sides, a dashed border 4px wide, and asymmetrical padding (10px on top, 30px on bottom, no padding horizontally).

3.4

Bootstrap Basics

Summary
  • Bootstrap is a library of CSS classes, with a focus on making your website mobile-friendly.
  • Rather than coming up with your own CSS code, rest on the “shoulders of giants”
  • Use Bootstrap classes to change colours, add margins and borders, create buttons, etc.
  • Check part 4.4 for advanced uses of Bootstrap

What’s a library?

Bootstrap is an open-source software (OSS) library, originally developed by Twitter, and now maintained by an international team.

Bootstrap is currently used on thousands of websites across the world, as it makes the styling of web pages quick and convenient.

Using third-party libraries is an extremely common pattern in the software world, because developing software is so complex and expensive that even big companies prefer joining forces and develop open source software together.

You use Open Source Software everyday without even noticing. For example:

  • The Android operating system is based on the open-source kernel ‘Linux”
  • The Chrome
  • The most common database library, SQLite, is used in thousand of everyday applications, from Adobe software to car satellite navigation systems

What does Bootstrap do?

Bootstrap provides three things that are incredibly useful for web developers:

  1. It offers standard styles for common HTML elements (<p>, <h1>, etc.) that look nicer than default browser styles.
  2. You can choose from hundreds of css classes to add colours, font sizes, alignments, margins
  3. It provides complex components such as buttons, cards, alerts, etc.

Bootstrap’s approach is to help you build mobile-friendly responsive websites. Remember that the majority of your audience will read your website on a mobile!

To learn about Bootstrap’s capacities and find examples you can copy, check the official Bootstrap documentation.

Adding Bootstrap to a project

To use Bootstrap’s styles, you must link the Bootstrap CSS file in your HTML file. This can be done in two ways:

  • You download bootstrap.css from the Bootstrap website and put it in your CSS folder. You then make a link to that file based on your folder structure.

    <link rel="stylesheet" href="css/bootstrap.css">

    This is called a “local installation”
  • You link towards a copy of that file that is hosted on a Content Distribution Network (CDN).

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css">

In addition to the link to the CSS file, you need a link to a JavaScript file which provides support for dynamic interactive behaviour, such as collapsible menus (e.g. see what happens here when using the search function).

You won’t need to worry about this, Bootstrap is pre-installed (using the CDN method) in your CV template.

Bootstrap’s default styles

See below how simple elements look like with built-in browser styles and with Bootstrap styles.

Default Browser styles on Chrome
Default Bootstrap styles

The main differences are that Bootstrap uses a sans-serif font, and that it removes some of the spacing between the elements on the page and the frame of the browser.

Colours and text

Bootstrap uses a default palette where colours are named based on their function:

  • Primary
  • Secondary
  • Success
  • Danger
  • Warning
  • Info
  • Light
  • Dark

You can use each of these colours as a text colour, by using a class which is text- followed by one of these colours, e.g. text-danger for red or text-primary for blue.

You can also use them for backgrounds, by using a class starting with bg- such as bg-danger or bg-primary

Use classes fw-bold for bold text, fst-italic for italic text, text-decoration-underline for underlined text. These classes have the same effect as <strong>, <em>, <u>, with the advantage that they can be combined and/or added to existing elements.

Text alignment can be controlled by classes text-start, text-center and text-end.

text-end means right-aligned (or left-aligned in Arabic, Hebrew, Persian, etc.).

Find more about text classes in the Text section of the Bootstrap docuentation.

In practice:

Code:
<p class="text-center text-success">This paragraph is centred (<em>text-center</em>) and has green text (<em>text-success</em>). It contains a <span class="bg-danger text-white fw-bold">span with a red background (<em>bg-danger</em>) and white (<em>text-white</em>), bold (<em>fw-bold</em>) text.</span></p>
Result:

This paragraph is centred (text-center) and has green text (text-success). It contains a span with a red background (bg-danger) and white (text-white), bold (fw-bold) text.

Box model classes

As we discussed in the previous part, CSS relies on a box model to manage spacing between elements. Spacing is essential and makes your pages more legible and the their structure clearer.

Bootstrap provides a series of classes to create margins, paddings and borders.

For margins and padding, class names are a combination of:

  • m for margin, p for padding
  • …followed by information about the position: no letter for margins/paddings on all sides, x for horizontal (left and right), y for vertical (top and bottom), s for start (left), e for end (right), t for top and b for bottom
  • …followed by an hyphen/dash
  • …followed by a number from 0 to 5 corresponding to the size of the margin/padding.

For example, m-1 gives a small margin on all four sides, my-5 gives a large margin on the vertical axis, px-3 gives a medium padding on the horizontal axis and pt-0 removes padding at the top.

Borders can be given a specific colour and/or be rounded. In that case, you need to combine multiple classes: border to add a border, border-danger to make it red and rounded to make it rounded.

Margins and paddings don’t have a colour of their own — paddings take the colour of the element and margins take the colour of the parent.

In practice:

In the examples below, check the position of the blue box within the red one. Borders are identical in all examples, but the paddings and margin of the blue box vary.

Code:
Result:

<div class="bg-danger border border-3 border-dark">

<div class="bg-primary border border-3 border-info rounded text-white">

No margin, no padding.

</div>

</div>

No margin, no padding.

<div class="bg-danger …">

<div class="bg-primary … m-3">

Size 3 margins on all sides, no padding.

</div>

</div>

Size 3 margins on all sides, no padding.

<div class="bg-danger …">

<div class="bg-primary … p-2">

No margin, size 2 padding on all sides.

</div>

</div>

No margin, size 2 padding on all sides.

<div class="bg-danger …">

<div class="bg-primary … mt-2 mb-5 ms-4 me-1 px-3 py-1">

Margins and paddings of different sizes on different sides.

</div>

</div>

Margins and paddings of different sizes on different sides.

Components

Bootstrap provides a comprehensive library of UX patterns called components, that you can reuse just by copying, pasting and customizing blocks of code.

Components include cards, carousels, navigation bars, accordions, badges, and many more…

Find out more by checking:

Don’t forget that all of the above are open-source resources. Copying open-source code is not cheating, it’s actually good practice.

Example, a button:

This example shows the code of a button that I copied from the buttons page in Bootstrap’s documentation.

It needs two classes: btn to style it as a button and btn-primary to make it a blue button.

Code:
<button type="button" class="btn btn-primary">Click me!</button>
Result:

Yay!

3.5

Combine Bootstrap and your own CSS

Summary
  • Learn when you should use Bootstrap and when you should use your own CSS classes.
  • Learn how to do “mix-and-match” Bootstrap and custom styles.

When to use what?

The basic rule is that you should use Bootstrap when it does what you want to do, and add your own styles when you need more customization.

Use cases When to use Bootstrap classes? When to create your own CSS?
Colours (including background and text colours) When one of Bootstrap’s built-in colours suits you. When you want to use specific colours, such as those defined in your brand identity.
Fonts Bootstrap only gives you access to a default sans-serif font. Use Bootstrap classes for effects such as bold type, italics, underlines When you want to use any font that suits your brand identity, follow the instructions below in part 3.6.
Layout Use a combination of the margin/padding and alignment classes (see above) as well as the Bootstrap grid (see part 4.5) Layout is one of the most complex parts of CSS. We do not recommend using your own CSS.
UX Components (e.g. buttons, navigation bars, carousels) Bootstrap provides a wide library of UX Components. Check part 4.4 for examples. Add your own CSS to adjust the colours and sizes of these components to your needs.

How to combine?

Try following these steps:

  1. Start by looking for Bootstrap classes that do what you want to do.
  2. Add these classes to the element where you need them.
  3. Create an additional class for the additional styles you need (see section 3.3 for details of that step).
Avoid conflicts

Some instructions in your own CSS file may be contradicting the ones from Bootstrap. For example, a Bootstrap class gives one colour to the text and yours gives another one. Your browser will follow complex rules to decide which style should be applied and which should be ignored.

To avoid this type of conflict, you should:

  • Remove any Bootstrap class that you don't need. For example, if you have a class which is "bg-danger" (red background) and you're adding your own class which also changes the background, you no longer need the Bootstrap one.
  • Make sure that your own CSS file is called after the Bootstrap CSS file. This is the case for all templates we provide.
  • If this isn’t sufficient, you can increase the priority of your CSS styles by adding !important after the CSS properties.

To achieve this result…

Welcome

to a world of endless possi­bilities in style

Combine Bootstrap and CSS this way

This is a simple HTML structure, with one <div> element for the frame, containing a <h4> heading and a <p> paragraph.

The following styles are done with Bootstrap
  • On all 3 elements:
    • Margins and paddings, with classes such as m-1 py-5.
  • On the main frame:
  • On the heading:
    • Text colour (text-white) and size (display-5)
The following styles need specific CSS styles
  • On the main frame:
    • Border styles, using the border CSS property.
    • The use of a background image, with background-image
  • On the heading:
    • Text rotation, with writing-mode and transform.
    • The use of a web font called Chillax (see section 3.6 about fonts)
  • On the paragraph:
    • The pink text colour (using the color property with a value of #ff80c0)
    • The neon glow effect (with the text-shadow property).

HTML Code

<div class="m-1 py-5 d-flex align-items-center justify-content-center owncss-div">

<h4 class="m-0 text-white display-5 owncss-heading">Welcome</h4>

<p class="m-0 fs-2 lh-1 w-50 text-center owncss-paragraph">to a world of endless possi­bilities in <strong>style</strong></p>

</div>

This HTML code contains references to many Bootstrap classes:

  • m-1 py-5 d-flex align-items-center justify-content-center on the <div>
  • m-0 text-white display-5 on the <h4>
  • m-0 fs-2 lh-1 w-50 text-center on the <p>

In addition, three CSS classes were created for the purpose of styling these elements: owncss-div, owncss-heading and owncss-paragraph

CSS Code

.owncss-div {

background-image: url(../images/common/hero-bg.jpg);

background-position: center;

border: .5em double #80ff80;

border-radius: 1em;

}

.owncss-heading {

font-family: 'Chillax';

writing-mode: vertical-lr;

transform: rotate(180deg);

}

.owncss-paragraph {

font-family: 'Plein';

color: #ff80c0;

text-shadow: 0 0 5px #ff80c0;

}

3.6

Use Web Fonts

Summary
  • Learn how to style Fonts using CSS.
  • Learn how to install Web Fonts.
  • Learn how to style hyperlinks.

Fonts in CSS

Fonts in CSS are defined with the font-family property. If you only use this property, you have to be careful, because you only have access to fonts that are installed. If you want to use additional fonts, you need to embed a web font in your page

It's common practice to start with your desired font and then add a couple of family font values as a "fallback" to ensure maximum compatibility across browsers and devices (in the case your desired font is not available the browser will use the next font listed in the property values). These "fallbacks" are usually generic font families such as: Serif, Sans-serif, Monospace, Cursive, Fantasy.

Installing fonts

A popular method of installing fonts is by using the HTML <link> tag to embed them in your website. As you already know, the <link> elements should go in the <head> of your document and they are used to reference external resources. In the case of fonts it will reference the resources providing the font. It's good practice to put the <link>s to the font resources AFTER the <link> to the CSS.

example <link> to embed Hanalei font

You then can use the installed font by using font-family property in your CSS to specify the font you have installed.

example CSS font family Hanalei

Installing Google fonts

Google Fonts is the most popular service for Web Fonts. To use one of their fonts:

  • Go to https://fonts.google.com/ and choose a font you want to use.
  • Click on “Select this style” (do not Download).
  • A sidebar opens where you can see the selected font(s) and instructions.
  • Click on “link” and follow the instructions.
  • Google gives you one line of code to put in your HTML file, and one to put in your CSS file.
installing google fonts

Please note that having Google Fonts activated on your page means that Google can track who is visiting your web site. Nothing is ever completely free!

Styling fonts: an example

Let's check an example to see different things that we can do to modify the look of fonts.

This is the HTML code of the website that will be styled. You can see that lines 7, 8, 9 and 10 have <link> tags embedding Google Fonts.

example fonts styled

This is the CSS code

css/styles.css

h1 {

font-family: "Roboto Mono", monospace;

font-size: 60px;

font-weight: bold;

text-transform: uppercase;

text-shadow: 5px 5px pink;

}

p {

font-family: "Nunito Sans", sans-serif;

color: grey;

}

p.myclass {

font-family: "Nunito Sans", sans-serif;

font-style: italic;

text-align: center;

color: grey;

}

As you can see the example uses two Google fonts that have been installed: Roboto Mono and Nunito Sans (and each one of these fonts has a "fallback": monospace and sans-serif respectively). It also makes use of different properties to modify the text, some are self explanatory like font-size, text-align and color, but others are a bit more interesting like:

  • text-transform: this property will transform any lower case text into uppercase automatically.
  • text-shadow: This propery creates a shadow of the text. And as you can see I have used three values: 5px correspods to the h-shadow, 5px to the v-shadow and pink to the color of the shadow.

This is how the website in the example would look like:

example fonts styled

Styling links

While learning about fonts, let's take the opportunity to see how can we style links.

By default, unvisited links are styled as blue, and previously visited links are purple, so let's check how can you style them as you please.

In order to do it we first need to understand Pseudo-Class selectors which are those that specify a special state of the selected element. So for example, in the case of a hyperlink (<a>), you can specify how you want it to look when:

  • The link has been visited
  • The link has not been visited
  • The user hovers the mouse over it

This is the syntax of pseudo clases, which as you can see uses a colon (:)

selector:pseudo-class { property: value; }

Styling links: an example

In the previous example you can see that in line 15 of the HTML code there is a <a> element with a hyperlink that has been visited but has not been styled (it's using the default purple color of visited links).

Let's style the hyperlink to make it fit with the overall style of the website by using pseudo-class selectors in the CSS code.

css/styles.css

a:link {

color: black;

}

a:visited {

color: grey;

}

a:hover {

color: black;

background-color: pink;

}

As you can see the example uses three pseudo-classes:

  • a:link styles the link that has not been visited (black).
  • a:visited styles the link that has been visited (grey).
  • a:hover styles how the link looks when the user hovers the mouse over it (black with a pink background).

This is how the link in website would look like now:

example fonts styled

Fonts in Bootstrap

Bootstrap comes with a series of classes that allow you to easily control the alignment, weight, size and other characteristics of your text and fonts.

In section 3.4 Bootstrap Basics - Colours and Text of this guide you can find an overview of classes that are very useful to modify text.

In the Text Utilities section of the Bootrap website you can find detailed Documentation and examples of other classes including Bootstrap's custom font sizing classes.