πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

HTML Style Attribute

P
php Guru
Β· February 20, 2023 Β· 3 min read Β· Updated February 20, 2023

πŸ“Œ Key Takeaways

  • HTML Style Attribute
  • What Is HTML Style Attribute
  • Syntax of Style Attribute
  • Text Color Example
  • Background Color Example
  • Font Size Example
Advertisement

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.

P
php Guru
← Previous Post
HTML Paragraphs
Next Post β†’
HTML Text Formatting

Leave a Reply

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

Prove your humanity: 4   +   3   =