HTML Style Attribute

HTML Style Attribute
HTML Style Attribute

The HTML style attribute is used to add custom styling to an element, such as colour, font, size, and more.

I am Red

I am Blue

I am Big

Style Attribute

Setting the style of an HTML element, can be done with the style attribute.

Syntax of style attribute is as follow:-

<tagname style="property:value;">

The property is a CSS property. Its also consider as key-value pair since both are using together with style attribute. The value is a CSS value.

Background Color

The CSS background-color property used to define background colour for an HTML element.

This example sets the background colour for a page to powderblue:


<body style="background-color:powderblue;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>

Text Color

The CSS color property defines the text color for an element:


<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>


The CSS font-family property used to define fonts for an element:


<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>

Text Size

The CSS font-size property used to define text size for an element:


<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>

Text Alignment

The CSS text-align property used to define horizontal text alignment for a HTML element.


<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>

Chapter Summary

  • Use the HTML style attribute for styling HTML elements
  • Use background-color for background color
  • Use color for text colors
  • Use font-family for text fonts
  • Use font-size for text sizes
  • Use text-align for text alignment