HTML Style Attribute

The HTML Style Attribute is used to apply CSS styles directly to HTML elements.
It allows you to change the color, size, alignment, and appearance of content.

In this tutorial, you will learn how to use the style attribute, with examples and output.

What Is HTML Style Attribute

The style attribute is used to add CSS directly inside an HTML tag.

It helps:

  • Change text color
  • Set background color
  • Adjust font size
  • Align content

Syntax of Style Attribute

Example

<tagname style="property: value;">Content</tagname>

Explanation

PartDescription
propertyCSS property (color, font-size, etc.)
valueValue of the property

Text Color Example

Example Code

<p style="color: blue;">This is blue text.</p>

Output

This is blue text. (in blue color)


Background Color Example

Example Code

<p style="background-color: yellow;">This has a background color.</p>

Output

This has a background color. (yellow background)


Font Size Example

Example Code

<p style="font-size: 20px;">Large text</p>

Output

Large text (bigger size)


Text Alignment Example

Example Code

<p style="text-align: center;">Centered text</p>

Output

Centered text (center aligned)


Multiple Styles in One Element

You can apply multiple styles using a semicolon ;.

Example Code

<p style="color: red; font-size: 18px; text-align: right;">
Styled paragraph
</p>

Output

Styled paragraph (red, larger, right aligned)


Styling Headings

Example Code

<h1 style="color: green;">Styled Heading</h1>

Output

Styled Heading (green color)


Common CSS Properties Used

Style Attribute Properties

PropertyDescription
colorText color
background-colorBackground color
font-sizeText size
text-alignAlignment
font-familyFont style

Font Family Example

Example Code

<p style="font-family: Arial;">This is Arial font.</p>

Output

This is Arial font.


HTML Style Attribute Summary

FeatureDescription
TypeInline CSS
LocationInside HTML tag
Multiple StylesYes
Syntaxproperty: value

Advantages of Style Attribute

AdvantageExplanation
Easy to UseSimple for beginners
Quick StylingApply styles instantly
No External FileNo CSS file needed

Disadvantages of Style Attribute

DisadvantageExplanation
Not ReusableCannot reuse styles
Hard to MaintainNot good for large projects
Messy CodeReduces readability

HTML style attribute, inline CSS HTML, HTML styling, HTML color, HTML font style


Best Practices

PracticeDescription
Use for small stylingGood for simple pages
Avoid overuseUse CSS for large projects
Keep code cleanWrite readable styles

Common Mistakes

MistakeResult
Missing semicolonStyle may break
Wrong property nameNo effect
No quotesInvalid HTML
Overusing inline CSSPoor structure

Learn this


Frequently Asked Questions (FAQ)

1. What is the HTML style attribute?

It is used to apply CSS directly to HTML elements.

2. Can I use multiple styles?

Yes, separate them using semicolons.

3. Is style attribute better than CSS?

No, it is only for small styling.

4. Where is style attribute written?

Inside the opening tag.

5. Can I style all elements?

Yes, most HTML elements support styling.

Related Article
Introduction to HTML

HTML Introduction – Learn the Basics of HTML HTML is the starting point of web development. Every website you see Read more

What is HTML

What Is HTML – Complete Beginner Explanation HTML is the foundation of the web. Every website you visit, from simple Read more

HTML Basic Examples

HTML Basic Examples – Learn HTML by Practice The best way to learn HTML is by writing and testing simple Read more

HTML text Editors

HTML Text Editors – Learn How to Write HTML Code To write HTML code, you need a text editor. A Read more

HTML Tags

HTML Tags are the basic units used to create HTML elements.They define how content is displayed in a web browser. Read more

HTML Elements

HTML Elements are the building blocks of every web page.Each element defines a part of the webpage, such as headings, Read more

HTML Attributes

HTML Attributes provide additional information about HTML elements.They help control how elements behave, display, or store extra data. In this Read more

HTML Headings

HTML Headings are used to define titles and subtitles on a web page.They help organize content and improve readability for Read more