HTML Styles – A Complete Guide

What is HTML Styling?

HTML styles allow developers to enhance the appearance of web pages using CSS (Cascading Style Sheets). Styling can be applied using:

  1. Inline CSS – Directly in an element.
  2. Internal CSS – Inside the <style> tag within the <head>.
  3. External CSS – In a separate CSS file.

Example (Without Styling)

<p>This is a simple paragraph.</p>

Types of CSS Styling in HTML

1. Inline CSS (Applied Inside an Element)

Inline styles are added directly using the style attribute inside an HTML tag.

Example:

<p style="color: blue; font-size: 20px;">This is a styled paragraph.</p>

2. Internal CSS (Inside the <head> Section)

Internal CSS is defined inside the <style> tag within the <head> of the document.

Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            color: green;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <p>This paragraph is styled using internal CSS.</p>
</body>
</html>

Best Practice: Use internal CSS only for small projects or when you don’t need to reuse styles across multiple pages.


3. External CSS (Linked from an External File)

External CSS is stored in a separate .css file and linked using the <link> tag.

Example (HTML File):

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <p>This paragraph is styled using an external CSS file.</p>
</body>
</html>

Example (styles.css File):

p {
    color: red;
    font-size: 16px;
}

Common CSS Properties Used in HTML Styling

PropertyDescriptionExample
colorChanges text colorcolor: blue;
background-colorSets background colorbackground-color: yellow;
font-sizeAdjusts text sizefont-size: 20px;
font-familySets font typefont-family: Arial, sans-serif;
text-alignAligns text (left, center, right)text-align: center;
borderAdds a borderborder: 1px solid black;
marginSets space outside an elementmargin: 10px;
paddingSets space inside an elementpadding: 10px;

Styling Text in HTML

<p style="color: purple; font-size: 24px; font-weight: bold;">Styled Text Example</p>

Explanation:

  • color: purple; → Sets text color.
  • font-size: 24px; → Increases text size.
  • font-weight: bold; → Makes text bold.

Styling Backgrounds

Change background colors or add images.

Example (Background Color):

<body style="background-color: lightgray;">

Example (Background Image):

body {
    background-image: url('background.jpg');
    background-size: cover;
}

CSS Selectors in HTML Styling

SelectorDescriptionExample
*Selects all elements* { margin: 0; padding: 0; }
pSelects all <p> elementsp { color: red; }
.classnameSelects elements with a class.highlight { color: blue; }
#idnameSelects an element with a specific ID#header { text-align: center; }
element, elementSelects multiple elementsh1, h2 { font-size: 20px; }

Best Practices for HTML Styling

✔️ Use external CSS for better organization.
✔️ Avoid inline styles unless necessary.
✔️ Use classes and IDs instead of styling individual elements.
✔️ Optimize styles for mobile responsiveness.