HTML <style> Tag โ Internal CSS
๐ Introduction
The <style> tag is used to write CSS (Cascading Style Sheets) directly inside your HTML file.
It allows you to style your webpage without using an external CSS file.
The <style> tag is placed inside the <head> section.
๐งฑ Basic Syntax
<style>
selector {
property: value;
}
</style>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: black;
color: white;
}
h1 {
color: lightblue;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<p>This is styled using the style tag.</p>
</body>
</html>
๐ฅ๏ธ Output Explanation
- The background becomes black
- Text becomes white
- The heading
<h1>appears in light blue
๐ฏ How It Works
Inside <style>, you write CSS rules:
selector {
property: value;
}
Example:
bodyโ selects the whole pagecolorโ changes text color
โ๏ธ <style> vs <link>
| Method | Description | Use Case |
|---|---|---|
<style> |
Internal CSS inside HTML | Small projects / testing |
<link> |
External CSS file | Real projects (recommended) |
โ ๏ธ Tips & Best Practices
- Place
<style>inside<head> - Use it for small projects or quick testing
- For large projects, use external CSS (
<link>) - Keep your CSS organized and readable
๐ซ Common Mistakes
- Writing CSS outside
<style>โ - Mixing too much CSS inside HTML (hard to maintain) โ
- Forgetting proper CSS syntax โ
๐งช Mini Exercise
Create a page and:
- Add a
<style>tag - Change:
- Background color
- Text color
- Heading color
Try different colors and see the result.