Smal SEO Tool

How To Add CSS

There are three ways of inserting a style sheet: External CSS, Internal CSS, and Inline CSS.

1. External CSS

With an external style sheet, you can change the look of an entire website by changing just one file! Each HTML page must include a reference to the external style sheet file inside the <link> element, which goes inside the <head> section. This is the most common and recommended method.

Example (HTML File)

html
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

The external stylesheet (e.g., "styles.css") would look like this:

Example (styles.css)

css
body {
  background-color: powderblue;
}
h1 {
  color: blue;
}

2. Internal CSS

An internal style sheet may be used if one single HTML page has a unique style. The internal style is defined inside a <style> element, inside the <head> section of an HTML page.

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-color: linen;
}
h1 {
  color: maroon;
  margin-left: 40px;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

3. Inline CSS

An inline style may be used to apply a unique style for a single element. To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.

Example

html
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>

Test Yourself with an Exercise

Which method is generally considered the best practice for applying CSS to a large website?