How to Add CSS Link to HTML

Adding CSS links to HTML
Published on: (Updated on: )
CSS is the magic that beautifies web pages. Here, we'll cover adding CSS to HTML with code examples. CSS is vital for colors, layouts, and more. But first, connect your HTML to CSS.
The <link> Element in HTML
To link an external CSS file, use the <link> element in the <head> section.. This is often placed within the <head> section of your HTML page. Here's the basic syntax:
<link rel="stylesheet" type="text/css" href="path-to-your-css-file.css">
- "rel" attribute set to "stylesheet" connects HTML and the stylesheet.
- "type" attribute set to "text/css" specifies the file's type.
- "href" attribute points to the CSS file's location.
Below are sample codes showing how to add CSS link to HTML
Let's assume you have an HTML file named index.html and a CSS file named styles.css in the same directory.
Here’s how you would link them together:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Web Page</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>This is a sample paragraph.</p>
</body>
</html>
styles.css:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
h1 {
color: #333;
}
p {
color: #666;
}