When building a website, HTML provides the structure, while CSS is what makes it beautiful. To apply styling rules from your CSS file to your HTML document, you need to link them properly.
In this blog, we’ll walk you through the 3 main ways to link CSS to HTML, complete with examples and best practices.
📁 1. External CSS – Best Practice for Most Projects
An external CSS file is stored separately from the HTML file and is linked using the <link>
tag in the <head>
section of your HTML document.
✅ Example:
📄 HTML (index.html
)
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
🎨 CSS (style.css
)
h1 {
color: blue;
text-align: center;
}
🧠 This is the preferred method because it separates content (HTML) from presentation (CSS), making your code cleaner and more maintainable.
✍️ 2. Internal CSS – Good for Small Projects or Testing
Internal CSS is written directly inside the HTML file using the <style>
tag in the <head>
.
📄 Example:
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: green;
font-size: 18px;
}
</style>
</head>
<body>
<p>This is a styled paragraph.</p>
</body>
</html>
⚠️ Use internal CSS only when external files are not practical (e.g., quick prototyping or a single-page project).
⚡ 3. Inline CSS – Directly Applied to Elements
Inline CSS is written inside an HTML tag using the style
attribute.
📄 Example:
<h2 style="color: red; font-weight: bold;">Inline Styled Heading</h2>
❗ Inline CSS should be avoided in most cases because it mixes style with structure and makes code harder to maintain.
🛠️ Common Mistakes to Avoid
- ❌ Forgetting to include
.css
in thehref
:<link rel="stylesheet" href="style"> <!-- WRONG -->
- ❌ Placing
<link>
outside the<head>
:<body> <link rel="stylesheet" href="style.css"> <!-- WRONG --> </body>
- ❌ Incorrect path to CSS file:
- Always ensure the file path in
href
is correct relative to your HTML file.
- Always ensure the file path in
🧠 Best Practices
- Use external CSS for scalability and reusability.
- Keep your CSS files organized in a
css/
orstyles/
folder. - Always include the
rel="stylesheet"
andtype="text/css"
(optional but good practice).
<link rel="stylesheet" type="text/css" href="styles/main.css">
🎯 Conclusion
Linking CSS to HTML is the first step to building a styled website. Choose the method that suits your project:
- ✅ External CSS for professional websites.
- ✅ Internal CSS for small pages.
- ❌ Avoid inline CSS except for quick tests or overrides.
Now that your CSS is connected, it’s time to start designing!