HTML Style Attribute

Subject: html

HTML style Attribute

The style attribute in HTML allows developers to apply inline CSS styles directly to an element. This means you can control how individual elements lookβ€”such as their color, font, background, and alignmentβ€”right within the HTML tag itself.

While external stylesheets and internal <style> blocks are preferred for larger projects, inline styles are great for quick edits or one-off formatting.


🧩 Syntax of the style Attribute

  • tagname – Any valid HTML tag (e.g., <p>, <h1>, <div>)
  • property – A CSS property (e.g., color, font-size, background-color)
  • value – The value for that property (e.g., red, 20px, center)

βœ… Example: Basic Inline Styling

This demonstrates how different styles can be applied to each element using the style attribute.


🎨 Setting Background Color

Example – Background for Entire Page:

Example – Background for Specific Elements:


🎨 Changing Text Color

The color property defines the color of the text.


βœ’οΈ Changing Font Family

Use web-safe fonts like Arial, Verdana, Times, and include fallbacks: font-family: Arial, sans-serif;


πŸ”  Adjusting Font Size

You can use units like %, px, em, or rem for font sizing.


🧭 Aligning Text

Other values for text-align include left, right, and justify.


βš™οΈ Summary of Common Inline CSS Properties

PropertyDescription
colorText color
background-colorElement background color
font-sizeSize of text
font-familyTypeface for text
text-alignHorizontal alignment
borderAdds border around the element
marginSpace outside the element
paddingSpace inside the element

βœ”οΈ Best Practices for Inline Styles

  • βœ… Use for quick testing or one-time formatting
  • ❌ Avoid using inline styles for full pages or large apps
  • βœ… Keep styles readable and consistent
  • βœ… Combine with semantic HTML for accessibility

πŸ”‘ Key Takeaway

The style attribute is a simple and effective way to apply CSS directly to HTML elements. It’s great for small projects or quick adjustments. But as your website grows, move toward using CSS classes and external stylesheets for better organization, reusability, and maintainability.