There are three major locations where we can type CSS code. As CSS adds visual design to HTML elements, the CSS code has to be recognized by the HTML document. CSS code can be typed within an HTML file or in a separate file. When you use a separate file for CSS coding, you need to add a link (file path) to the CSS file in the HTML file.
The most common approach to CSS coding is to create a separate CSS document and specify the file path to the CSS file in the HTML document.
Here are the key points for this approach:
<link>
tag in the head section of the HTML document.You can add a link to the same CSS file in multiple HTML documents. When you have more than one web page with the same style, this approach is the most effective as it allows you to maintain design consistency across the website.
For the external CSS approach, you need to make sure that the CSS file path is properly written. In the example above, we were assuming that the test.css file is located under the css directory in the same directory where the HTML file is located like shown in the illustration below.
The second approach is to type CSS code in the <head>
section of the HTML document using the <style>
tag. In this approach, the CSS code is only applied to the HTML document with the <style>
tag. When you want to customize the design of a specific HTML document, this approach may be effective.
The last approach is to type CSS code directly in the start tag of the HTML element that you want to style using the style
attribute. In this approach, the CSS code is only applied to the specific HTML element.
As we have already practiced the second approach (type CSS code in the head section of the HTML document) in the previous practice, we'll cover the first and third approaches in this practice.
<h1>
tag like in the code below.
<h1><span style="color:red">Chapter 8.</span> CSS Basics</h1>
body{
background-color: azure;
}
Add a link to the practice.css file in the head section of chapter8.html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/practice.css">
<title>8. CSS Basics</title>
Open chapter8.html with a browser (make sure that both html and css files are saved).
You can see that the background of the document changed to azure (light blueish color).
There are three major locations where we can type CSS code. As CSS adds visual design to HTML elements, the CSS code has to be recognized by the HTML document. CSS code can be typed within an HTML file or in a separate file. When you use a separate file for CSS coding, you need to add a link (file path) to the CSS file in the HTML file.
The most common approach to CSS coding is to create a separate CSS document and specify the file path to the CSS file in the HTML document.
Here are the key points for this approach:
<link>
tag in the head section of the HTML document.You can add a link to the same CSS file in multiple HTML documents. When you have more than one web page with the same style, this approach is the most effective as it allows you to maintain design consistency across the website.
For the external CSS approach, you need to make sure that the CSS file path is properly written. In the example above, we were assuming that the test.css file is located under the css directory in the same directory where the HTML file is located like shown in the illustration below.
The second approach is to type CSS code in the <head>
section of the HTML document using the <style>
tag. In this approach, the CSS code is only applied to the HTML document with the <style>
tag. When you want to customize the design of a specific HTML document, this approach may be effective.
The last approach is to type CSS code directly in the start tag of the HTML element that you want to style using the style
attribute. In this approach, the CSS code is only applied to the specific HTML element.
As we have already practiced the second approach (type CSS code in the head section of the HTML document) in the previous practice, we'll cover the first and third approaches in this practice.
<h1>
tag like in the code below.
<h1><span style="color:red">Chapter 8.</span> CSS Basics</h1>
body{
background-color: azure;
}
Add a link to the practice.css file in the head section of chapter8.html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/practice.css">
<title>8. CSS Basics</title>
Open chapter8.html with a browser (make sure that both html and css files are saved).
You can see that the background of the document changed to azure (light blueish color).