Home » HTML Styles – A Complete Guide

HTML style Tag – A Complete Guide for Beginners

Introduction

When you first start learning web development, you’ll quickly come across two core technologies: HTML and CSS. HTML is used to create the structure of a webpage, while CSS is used to style and design it.

Think of HTML as the skeleton of a website and CSS as the clothing and design that makes it look attractive. Without CSS, a webpage looks plain and unappealing.

There are different ways to apply CSS in HTML, and one of the most important methods is using the HTML <style> tag. This tag allows you to write CSS directly inside your HTML file.

If you’re just starting out, understanding the <style> tag is a perfect first step before moving to advanced styling techniques.

 Learn HTML basics here: /html-basics
Learn CSS fundamentals here: /css-basics

What is style Tag?

The <style> tag in HTML is used to define internal CSS styles within a webpage. It is placed inside the <head> section and allows developers to control the design, layout, colors, and fonts of HTML elements without using external stylesheets.

The <style> tag is one of the most commonly used HTML tags when working with internal CSS in HTML. It allows developers to write CSS rules directly inside the HTML document instead of using a separate file.

This is especially useful for:

  • Small projects
  • Quick testing
  • Single-page designs

Basic Syntax of <style>

<head>
  <style>
    body {
      background-color: lightblue;
    }
  </style>
</head>

Line-by-Line Explanation

  • <head> → This section contains metadata and styles for the webpage
  • <style> → This is where CSS code is written
  • body {} → This is a CSS selector targeting the entire page
  • background-color: lightblue; → Changes the background color
  • </style> → Ends the style block

This simple style tag example shows how easy it is to apply design to a webpage.

Where to Use <style> Tag

The <style> tag is used inside the <head> section of an HTML document to apply internal CSS styles. It allows developers to define the design, layout, and appearance of webpage elements directly within the same HTML file.

The <style> tag is mainly used in the following situations:

1. Inside <head> Section

This is the recommended and most common placement.

2. Internal CSS Usage

Used when styling a single HTML page

3. Small Projects or Testing

Perfect for beginners and quick experiments

Internal CSS vs Inline vs External CSS

Understanding the difference is very important for beginners.

TypeLocationUse Case
InlineInside elementSmall changes
Internal<style> tagSingle page
ExternalCSS fileLarge projects

Explanation

  • Inline CSS → Directly applied to an element
  • Internal CSS → Written inside <style> tag
  • External CSS → Stored in separate .css file

 For large websites, always prefer external CSS.

Real-World Examples

Example 1: Styling Text

<style>
  h1 {
    color: blue;
  }
</style>

Explanation

This code changes all <h1> headings to blue. It’s simple and effective for beginners.

Example 2: Styling Multiple Elements

<style>
  body {
    background-color: #f0f0f0;
  }

  h1 {
    color: darkblue;
  }

  p {
    font-size: 18px;
    color: #333;
  }
</style>

Explanation

  • body → controls background
  • h1 → styles headings
  • p → styles paragraphs

This shows how internal CSS in HTML can style multiple elements at once.

CSS Selectors in <style>

Selectors help you target elements in HTML.

Learn more: /css-selectors

Element Selector

p {
  color: red;
}

Targets all <p> elements.

Class Selector

.box {
  background: yellow;
}

Used with:

<div class="box"></div>

ID Selector

#header {
  font-size: 24px;
}

Used with:

<div id="header"></div>

Common CSS Properties Used in HTML Styling

PropertyDescriptionExample
colorChanges text colorcolor: blue;
background-colorSets background colorbackground-color: yellow;
font-sizeAdjusts text sizefont-size: 20px;
font-familySets font typefont-family: Arial, sans-serif;
text-alignAligns text (left, center, right)text-align: center;
borderAdds a borderborder: 1px solid black;
marginSets space outside an elementmargin: 10px;
paddingSets space inside an elementpadding: 10px;

Styling Text in HTML

<p style="color: purple; font-size: 24px; font-weight: bold;">Styled Text Example</p>

Explanation:

  • color: purple; → Sets text color.
  • font-size: 24px; → Increases text size.
  • font-weight: bold; → Makes text bold.

Styling Backgrounds

Change background colors or add images.

Example (Background Color):

<body style="background-color: lightgray;">

Example (Background Image):

body {
    background-image: url('background.jpg');
    background-size: cover;
}

CSS Selectors in HTML Styling

SelectorDescriptionExample
*Selects all elements* { margin: 0; padding: 0; }
pSelects all <p> elementsp { color: red; }
.classnameSelects elements with a class.highlight { color: blue; }
#idnameSelects an element with a specific ID#header { text-align: center; }
element, elementSelects multiple elementsh1, h2 { font-size: 20px; }

Best Practices for HTML Styling

✔️ Use external CSS for better organization.
✔️ Avoid inline styles unless necessary.
✔️ Use classes and IDs instead of styling individual elements.
✔️ Optimize styles for mobile responsiveness.

Common Mistakes

Avoid these beginner errors:

Placing <style> outside <head>

This can break structure

Mixing inline and internal CSS

Leads to confusion

Overwriting styles unintentionally

CSS follows priority rules

SEO Benefits of Using <style> Tag

Even though CSS is mostly about design, it also impacts SEO indirectly.

Better Page Structure

Clean code improves readability

Faster Styling for Small Pages

No need to load external files

Improved User Experience

Good design keeps users engaged

FAQs Section

What is the style tag in HTML?

The style tag is used to add internal CSS styles inside an HTML document.

 Where should the <style> tag be placed?

It should be placed inside the <head> section.

 Can I use multiple <style> tags?

Yes, but it’s better to keep styles in one place.

 Is internal CSS good for large websites?

No, external CSS is better for large projects.

What is the difference between style and link tag?

<style> is for internal CSS, while <link> is used for external CSS file

Scroll to Top