HTML style Tag: Inline CSS

html style tag

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>?

Yes, but it may cause delays or conflicts. Use <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?

Yes. All modern browsers support <style> and apply the CSS rules correctly.

Similar Reads

HTML time Tag: Representing Dates and Times

The HTML time Tag marks time values in a document. Use it to show dates or times that machines and…

HTML Link Attributes Guide for Beginners

HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…

The Button Tag in HTML: How it Works with Examples

The HTML button tag creates interactive controls on a web page. You can use this element to trigger actions or…

HTML Span Tag: Inline Container Explained

The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…

HTML ARIA Attributes for Accessibility

The HTML ARIA attributes help users with disabilities use websites. These attributes describe roles and states for tools. Understand the…

HTML kbd Tag: How to Show Keyboard Input with examples

The HTML<kbd> tag shows the keys that a user must press. This helps you to give instructions for commands or…

HTML Element: Everything You Need To Know with Examples

An HTML element builds every part of a web page. It holds the text or block you want the browser…

HTML template Tag: How it Works with Examples

The HTML <template> tag lets you store HTML content that won’t show up right away. JavaScript can pull it in…

What is HTML for Beginners and How it Works for Beginners

HTML builds the base for every web page. It sets structure and meaning so the browser knows what to show.…

HTML Document Structure Guide for Beginners

A web page must follow a clear structure so browsers can read and show it correctly. This guide helps you…

Previous Article

HTML noscript Tag: How to Display Fallback When JS Is Disabled

Next Article

HTML link Tag: Linking Stylesheets

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.