Linking CSS File to HTML Document
Linking a CSS (Cascading Style Sheets) file to an HTML document is a fundamental technique in web development. CSS is used to control the presentation and styling of an HTML document, allowing you to separate the content (HTML) from the presentation (CSS). This separation of concerns makes it easier to maintain and update the website's appearance.
Here's how you can link a CSS file to an HTML document:
-
Create a CSS File:
- Start by creating a new file with a
.css
extension, for example,styles.css
. - In this file, you can define your CSS rules, such as setting the background color, font styles, and layout of your web page.
- Start by creating a new file with a
-
Link the CSS File to the HTML Document:
- In your HTML document, locate the
<head>
section, which is where you'll add the link to the CSS file. - Inside the
<head>
section, add the following line of code:
<link rel="stylesheet" href="styles.css">
- The
rel="stylesheet"
attribute specifies that the linked resource is a stylesheet. - The
href="styles.css"
attribute specifies the path to the CSS file. In this example, the CSS file is located in the same directory as the HTML file.
- In your HTML document, locate the
Here's an example of an HTML document with a linked CSS file:
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is some content on my web page.</p>
</body>
</html>
In this example, the styles.css
file is linked to the HTML document using the <link>
element in the <head>
section.