Table of Content
The style tag in HTML defines CSS inside a web page. Click to see how it works with syntax and examples.
What Is the <style> Tag in HTML?
The <style> tag lets you write CSS inside an HTML file. It applies custom design rules to elements in the same document.
Here is the basic tag:
<style>
/* CSS rules go here */
</style>Use this format when you write your code. Write all style rules between the open tag and the close tag.
The tag links visual design to HTML elements. It sets styles and layout. It keeps all style rules in the same file.
You can place the <style> tag in different parts of the HTML document.
This is the standard location. It helps the browser load styles before it shows content.
<head>
<style>
body { font-family: Arial; }
</style>
</head>This also works, but it can delay styles or create conflicts.
<body>
<style>
p { color: red; }
</style>
</body>You can write device-specific rules. This targets screen size changes.
<style>
@media screen and (max-width: 600px) {
body { background-color: lightgray; }
}
</style>Use the <style> tag if:
- You test quick changes
- You create small pages
- Don’t link extra files
- You add dynamic styles to server pages
The browser reads the <style> tag early. It loads all rules before the page appears. It applies each rule to match elements.
The <style> tag keeps CSS local. It does not scale to a large website and does not share rules across pages.
The Difference Between <style> Tag and Inline Styles
The <style> tag stores CSS in one place. It keeps content and design separate. Inline styles go inside each element. Each style stays with its HTML tag.
Here are the key differences:
- The <style> tag sits in the document head or body
- Inline styles go inside the element <style> tag
- The <style> tag groups all rules together
- Inline styles repeat CSS for every element
You use it in the following cases:
- Use the <style> tag for shared page rules
- Use inline styles for one-time changes
- Use the <style> tag for a readable structure
- Use inline styles in emails or templates
Examples
Set Page Font:
<head>
<style>
body {
font-family: Arial;
font-size: 16px;
}
</style>
</head>This sets a global font family and size. It controls all body text from one place.
Style a Navigation Bar:
<head>
<style>
nav {
background-color: #333;
color: white;
padding: 10px;
}
</style>
</head>This adds layout and color to the navigation bar. It defines basic styles.
Use Media Query for Mobile:
<head>
<style>
@media screen and (max-width: 600px) {
h1 {
font-size: 20px;
}
}
</style>
</head>This reduces title size for smaller screens. It shows how to target the screen width.
Color Tables:
<head>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid black;
padding: 5px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>This applies to consistent table styles. It adds borders, padding, and header background color.
Wrapping Up
In this article, you learned what the tag does and where to place it. You saw how it works with CSS. You also learned when to use it and what limits it has.
Here is a quick recap:
- Use it for local CSS
- Don’t place it in unless needed
- Use it for small pages or tests
- Write complete rules inside the <style> tag
- Do not use it for large websites
FAQs
What is the correct syntax for the <style> tag?
<style>
selector {
property: value;
}
</style>
The tag wraps CSS rules. Each rule uses a selector and a property-value pair.
Can I use the <style> tag inside the <body>?
<head> instead for better order and speed.
How is the <style> tag different from inline CSS?
<style> holds grouped CSS rules. Inline CSS puts styles inside every element tag.Does the <style> tag work in all browsers?
<style> and apply the CSS rules correctly.
Similar Reads
The HTML a tag allows you to create links that move users from one page or resource to another. Understand…
The HTML em tag shows stress or emphasis in text. It changes meaning for readers and screen readers. What is…
Use the HTML object tag to embed different types of external files into your webpage. It works for videos and…
The HTML sup tag puts text above the normal line. Writers use it for math power and marks. So, readers…
You use the <aside> tag to mark extra content in HTML. It does not replace the main text and shows…
Use the HTML b tag to make text bold without giving it extra meaning. It adds visual weight only. Understand…
The title tag in HTML shows the page name in the browser tab and helps search engines know the topic.…
HTML builds the base for every web page. It sets structure and meaning so the browser knows what to show.…
This article shows you how to open a link in a new tab. Read the code and follow the examples…
The <ins> tag in HTML shows text that the author added after the original content. This helps readers know what…