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:

Example

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:

Example

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:

Example

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:

❓ FAQ

Q What's the difference between inline, internal, and external styles?
A Three approaches: 1) Inline styles (style attribute) only affect the current element, written inside HTML tags; 2) Internal styles (<style> tag) affect the current page, written in <head>; 3) External styles (.css file via <link>) affect all pages that reference it. Priority: inline > internal > external. In practice, use external stylesheets 95% of the time.
Q Why are external stylesheets recommended?
A External stylesheets have four core advantages: 1) Reuse — multiple pages share one CSS file, change once, update everywhere; 2) Caching — browsers cache CSS files, making subsequent page loads faster; 3) Separation — HTML handles structure, CSS handles styling, clear responsibilities, easier maintenance; 4) Collaboration — front-end developers can work in parallel without conflicts. Inline styles are only for debugging and edge cases.
Q How is style priority calculated?
A Priority from highest to lowest: 1) !important (use sparingly — disrupts priority); 2) Inline styles (style attribute); 3) ID selectors (#id); 4) Class selectors (.class), attribute selectors, pseudo-classes; 5) Element selectors (p, div), pseudo-elements; 6) Universal selector (*), inherited styles. Same priority: later rules override earlier ones. Don't abuse !important; use selector specificity to control priority.
Q When should I use inline styles?
A Inline styles have very limited use cases: 1) JavaScript dynamically modifying styles (element.style.color = 'red'); 2) Quick debugging (temporary preview); 3) Email templates (email clients poorly support <style>). For everything else, use internal or external stylesheets. Inline style drawbacks: hard to maintain, can't reuse, too-high priority makes overriding difficult.

📖 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%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏