The <style> tag in HTML is used to define internal CSS (Cascading Style Sheets) that apply styling rules to HTML elements. It is placed inside the <head> section of an HTML document and affects the appearance of elements on the page.
Basic Syntax:
<head>
<style>
p {
color: blue;
}
</style>
</head>
Purpose of <style> Tag:
Element-Specific Attributes of <style>:
| Attribute |
Description |
type |
Specifies the MIME type of the style sheet. Commonly set to text/css. (Optional in HTML5) |
media |
Specifies what media/device the styles are designed for (e.g., screen, print). |
scoped |
(Deprecated) Limits the scope of the styles to the parent element. Not widely supported. |
Attribute Details:
1. type (optional in HTML5)
<style type="text/css">
body {
background-color: lightgray;
}
</style>
2. media
<style media="screen">
body {
font-size: 16px;
}
</style>
<style media="print">
body {
font-size: 12px;
}
</style>
<section>
<style scoped>
p { color: red; }
</style>
<p>This text would be red if `scoped` were supported.</p>
</section>
-
Use the <style> tag for quick prototypes or when external stylesheets are not practical.
-
For large projects, prefer external CSS files using the <link> tag.