CSS (Cascading Style Sheets) can be connected to an HTML page in three main ways: inline, internal, and external.
style attribute.<style> tag in the <head>..css file and linked using <link>.Choosing the right linking method helps keep your code clean, reusable, and easy to maintain—especially in larger projects.
Below is a quick view of how the three CSS linking methods look in HTML:
// Inline CSS: style directly inside the tag
<h1 style="color: blue;">Hello</h1>
// Internal CSS: <style> block inside the <head>
<head>
<style>
h1 {
color: green;
}
</style>
</head>
// External CSS: linked stylesheet file
<head>
<link rel="stylesheet" href="style.css">
</head>
Here is a small HTML snippet that uses all three CSS linking methods in one page:
// Example HTML showing all three CSS linking methods
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Linking Demo</title>
// Internal CSS in a <style> block
<style>
.internal {
font-weight: bold;
color: green;
}
</style>
// External CSS file for reusable styles
<link rel="stylesheet" href="style.css">
</head>
<body>
// Inline CSS applied directly to a paragraph
<p style="color: red;">This is red using inline CSS.</p>
<p class="internal">This uses internal CSS.</p>
<p class="external">This is styled by external CSS.</p>
</body>
</html>
The above code would display something like:
This is red using inline CSS.
This uses internal CSS.
This is styled by external CSS.
Note: The exact external styling (like italic and purple) comes from the rules defined inside style.css.
External styles are placed in a separate file (for example, style.css) and linked into the HTML. This is the preferred approach for real-world projects.
/* External stylesheet: style.css */
body {
background-color: #f0f0f0;
}
h2 {
color: darkblue;
text-align: center;
}
When the HTML page links to style.css, the background becomes light grey, and any <h2> headings turn dark blue and centered.
style attribute directly to an element. Example: <p style="color: red;">Hello</p><style> block in the <head> of the HTML. Good for single-page styling or quick prototypes..css file and attached with a <link rel="stylesheet" href="style.css"> tag. This is best for multi-page sites and team projects.rel="stylesheet" in your <link> tag.href so that style.css is correctly found.style.css file and link it correctly.home.html and about.html) and reuse the same external stylesheet across both pages./* Layout Styles */) to keep sections well organized.