English العربية Português Japanese

HTML Styles

HTML handles the structure of a web page, while CSS (Cascading Style Sheets) handles its appearance. You can write styles in HTML in three ways: inline styles, internal style sheets, and external style sheets.

Inline Styles

Write CSS directly in an element's style attribute — it only affects that specific element:

HTML
<p style="color: blue; font-size: 18px;">
  This is a paragraph with large blue text.
</p>
<div style="background: #f0f0f0; padding: 20px; border-radius: 8px;">
  This is a gray card with rounded corners.
</div>
▶ Try It Yourself
💡 When to Use: Inline styles have the highest priority and are great for quick testing or scenarios where JavaScript dynamically modifies styles. However, avoid them in production because they're hard to maintain.

Internal Style Sheet

Define styles inside the <head> using the <style> tag — all elements on the page can reference them:

HTML
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: Arial, sans-serif; }
    h1 { color: #333; text-align: center; }
    .card {
      background: #fff;
      border: 1px solid #ddd;
      padding: 16px;
      border-radius: 8px;
    }
  </style>
</head>
<body>
  <h1>My Page</h1>
  <div class="card">This is a card</div>
</body>
</html>
▶ Try It Yourself

External Style Sheet

Write CSS in a separate .css file and include it with <link>. This is the most recommended approach — a single CSS file can control the style of an entire website:

HTML
<head>
  <link rel="stylesheet" href="style.css">
</head>

<!-- Contents of style.css: -->
<!-- h1 { color: navy; } -->
<!-- .highlight { background: yellow; } -->
▶ Try It Yourself
📌 Priority of the Three Methods: Inline styles > Internal style sheet > External style sheet > Browser default styles. When the same element is styled by multiple methods, the one with higher priority takes effect.

Common CSS Properties at a Glance

Here are some of the most commonly-used CSS properties in HTML — master these and you can create decent-looking pages:

📖 Summary

📝 Exercises

  1. Style practice: Create an HTML page and use all three styling methods on different elements. Observe the priority effect.
  2. Card design: Use inline styles to create a card component with a title, image description, and button.
  3. External styles: Create a style.css file, include it with <link>, and apply styles to multiple elements on the page.
100%