Styling HTML with CSS
CSS stands for Cascading Style Sheets.
CSS describes how HTML elements are to be displayed on the screen, paper, or in other media.
CSS saves a lot of work. It can control the layout of multiple web pages all at once.
CSS can be added to HTML elements in 3 ways:
- Inline - by using the style attribute in HTML elements
- Internal - by using an
<style>
element in the<head>
section - External - by using an external CSS file
The most common way to add CSS is to keep the styles in separate CSS files. However, here we will use inline and internal styling because this is easier to demonstrate and easier for you to try it yourself.
Tip: You can learn much more about CSS in our CSS Tutorial.
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
An inline CSS uses the style attribute of an HTML element.
This example sets the text color of the
<h1>
element to blue:
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;">This is a Blue Heading</h1>
</body>
</html>
Output:
This is a Blue Heading
Internal CSS
An internal CSS is used to define a style for a single HTML page.
An internal CSS is defined in the
<head>
section of an HTML page, within a <style>
element:
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
External CSS
An external style sheet is used to define the style for many HTML pages.
With an external style sheet, you can change the look of an entire web site, by changing one file!
To use an external style sheet, add a link to it in the
<head>
section of the HTML page:
<!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>
Output:
This is a heading
This is a paragraph.
An external style sheet can be written in any text editor. The file must not contain any HTML code and must be saved with a .css extension.
Here is how the "styles.css" looks:
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
CSS Fonts
The CSS
color
property defines the text color to be used.
The CSS
font-family
property defines the font to be used.
The CSS
font-size
property defines the text size to be used.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p {
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
CSS Padding
The CSS
padding
property defines a padding (space) between the text and the border:
<!DOCTYPE html>
<html>
<head>
<style>
p {
border: 1px solid powderblue;
padding: 30px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
This is a paragraph.
This is a paragraph.
CSS Margin
The CSS
margin
property defines a margin (space) outside the border:
<!DOCTYPE html>
<html>
<head>
<style>
p {
border: 1px solid powderblue;
margin: 50px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
This is a paragraph.
This is a paragraph.
The id Attribute
To define a specific style for one special element, add an
id
attribute to the element:
<p id="p01">I am different</p>
Then define a style for the element with the specific id:
<!DOCTYPE html>
<html>
<head>
<style>
#p01 {
color: blue;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p id="p01">I am different.</p>
</body>
</html>
Output:
This is a paragraph.
This is a paragraph.
I am different.
Note: The id of an element should be unique within a page, so the id selector is used to select one unique element!
The class Attribute
To define a style for special types of elements, add a
class
attribute to the element:
<p class="error">I am different</p>
Then define a style for the elements with the specific class:
<!DOCTYPE html>
<html>
<head>
<style>
p.error {
color: red;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p class="error">I am different.</p>
<p>This is a paragraph.</p>
<p class="error">I am different too.</p>
</body>
</html>
Output:
This is a paragraph.
This is a paragraph.
I am different.
This is a paragraph.
I am different too.
External References
External style sheets can be referenced with a full URL or with a path relative to the current web page.
This example uses a full URL to link to a style sheet:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://www.w3schools.com/html/styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
This example links to a style sheet located in the html folder on the current web site:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/html/styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is a heading
This is a paragraph.
This example links to a style sheet located in the same folder as the current page:
<!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>
Output:
This is a heading
This is a paragraph.
No comments