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
Property | Description |
---|---|
color | Text color |
background-color | Element background color |
font-size | Size of text |
font-family | Typeface for text |
text-align | Horizontal alignment |
border | Adds border around the element |
margin | Space outside the element |
padding | Space 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.
Advertisement Slot 1
Advertisement Slot 2