HTML Styles
You can change the look of an HTML element with the style attribute. This is called inline CSS. The value is made of CSS property and value pairs.
<tagname style="property: value;">
Text colour and background
<h2 style="color: blue;">Blue heading</h2>
<p style="background-color: lightyellow;">Paragraph with a light yellow background.</p>
Output in the browser
Blue heading
Paragraph with a light yellow background.
Font family and size
<p style="font-family: Verdana, sans-serif; font-size: 20px;">This text is in Verdana, 20 pixels.</p>
Output in the browser
This text is in Verdana, 20 pixels.
Text alignment
<p style="text-align: center;">Centered text</p>
<p style="text-align: right;">Right aligned text</p>
Output in the browser
Centered text
Right aligned text
Border
<p style="border: 2px solid green; padding: 10px;">Paragraph with a green border.</p>
Output in the browser
Paragraph with a green border.
Several styles together
Separate the styles with a semicolon.
<p style="color: white; background-color: darkred; padding: 10px;">White text on dark red.</p>
Old styling tags are removed
In old HTML you might see tags and attributes like <font color="red">, <center> and bgcolor. They are not valid in HTML5. Use CSS instead.
Note:
Inline styles are fine for learning and quick tests. In real websites put the CSS in a separate .css file or in a <style> tag, and use classes.
Summary
- Use the
styleattribute with CSS properties to change the look. - Separate several styles with semicolons.
- Do not use the old
fontandcentertags. - Next: HTML Formatting.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |