HTML Styles
π What Are HTML Styles?
HTML styles are used to enhance the appearance of web content. You can change colors, fonts, layout, spacing, and much more using styling.
>>βWithout styles, the web would be functional but flat. Styles bring design to life.β β¨
π― Ways to Apply Styles in HTML
There are three main ways to apply styles in HTML:
- π¨ Inline Styles β directly in the HTML element via the style attribute
- π§Ύ Internal CSS β within a <style> tag in the <head>
- π External CSS β linking to a separate .css file
π 1. Inline Styles
Use the style attribute to apply CSS properties directly to an element.
Inline Style Example
<p style="color: green; font-size: 20px;">This is a green paragraph.</p>Note
β οΈ Inline styles are useful for quick tweaks but not ideal for maintaining large projects.
π§© 2. Internal CSS
Internal styles are defined in the <head> section using the <style> tag.
Internal CSS Example
<html>
<head>
<style>
p {
color: navy;
font-family: Arial;
}
</style>
</head>
<body>
<p>This paragraph is styled with internal CSS.</p>
</body>
</html>Note
β
Use internal styles when working with single HTML pages or demos.
π 3. External CSS
External styles are written in a separate file and linked using the <link> tag. This is the preferred way for real-world projects.
HTML File with External CSS
<head>
<link rel="stylesheet" href="styles.css" />
</head>styles.css
p {
color: teal;
font-size: 18px;
}Note
π External styles make your HTML cleaner and your styling reusable across pages.
π¨ CSS Properties You Can Use
| Property | Purpose | Example Value |
|---|---|---|
| color | Text color | red |
| font-size | Text size | 16px |
| background-color | Background fill | lightgray |
| margin | Spacing outside the element | 10px |
| padding | Spacing inside the element | 20px |
π§ Styling Best Practices
- π― Use external stylesheets for scalable projects
- π Keep styles organized in a /css or /assets folder
- π§Ή Avoid using inline styles unless absolutely necessary
- βΏ Use readable color combinations for accessibility
π Learn More
>>βCode is structure, but style is expression. Master both to create exceptional web experiences.β π§βπ¨