
How to Link CSS to Html
To link CSS (Cascading Style Sheets) to an HTML document, you can use the <link>
tag within the <head>
section of your HTML code. Here’s the basic syntax:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<!-- HTML content goes here -->
</body>
</html>
Let’s break down the above code:
- The
<link>
tag is used to establish a link between the HTML document and the CSS file. - The
rel
attribute specifies the relationship between the HTML document and the linked file. In this case, it’s set to"stylesheet"
to indicate that the linked file is a style sheet. - The
type
attribute specifies the type of the linked file. It should be set to"text/css"
to indicate that it’s a CSS file. - The
href
attribute specifies the path or URL of the CSS file. In the example above, it’s set to"styles.css"
. Make sure to adjust the path if your CSS file is in a different location.
By using the <link>
tag, you can connect an external CSS file to your HTML document. This allows you to separate the style definitions from the HTML code, making it easier to maintain and update your website’s design.
You can create the CSS file separately and save it with a .css
extension (e.g., styles.css
). Inside the CSS file, you can define various styles, such as font properties, colors, margins, and more, that will be applied to the HTML elements.
For example, in styles.css
:
h1 {
color: blue;
font-size: 24px;
}
In the above example, the CSS rule selects the <h1>
elements and applies the specified styles, such as setting the text color to blue and the font size to 24 pixels.
Ensure that the CSS file and the HTML file are located in the same directory or specify the correct relative or absolute path to the CSS file in the href
attribute.
By linking CSS to HTML, you can control the visual appearance and styling of your web page, separate from the HTML structure and content.