CSS Introduction Methods

CSS styles can be introduced into HTML documents in three ways: inline styles, internal styles, and external styles. Each method has its appropriate use cases.

✅ Best practice: For production environments, external stylesheets (<link>) are recommended. They completely separate HTML and CSS, allow browsers to cache CSS files, and improve loading speed when shared across multiple pages.

1. Inline Styles

Inline styles write CSS directly in the HTML tag's style attribute.

Example

HTML
<p style="color: red; font-size: 18px;">This text is red and 18 pixels</p>

<div style="width: 200px; height: 100px; background: blue;">
  This is a blue box
</div>
▶ Try it Yourself

Characteristics:

Use cases: Rarely used. May be used in scenarios requiring dynamic style changes via JavaScript.

2. Internal Styles

Internal styles write CSS inside the HTML document's <head> tag, wrapped in a <style> tag.

Example

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Internal Style Example</title>
  <style>
    p {
      color: red;
      font-size: 16px;
    }
    h1 {
      color: blue;
      text-align: center;
    }
    .highlight {
      background-color: yellow;
    }
  </style>
</head>
<body>
  <h1>Heading</h1>
  <p>Paragraph text</p>
  <p>This text has a highlighted background</p>
</body>
</html>
▶ Try it Yourself

Characteristics:

3. External Styles (External Stylesheets)

External styles write CSS in a separate .css file, then use <link> tags to include it in HTML. This is the most commonly used method in actual development.

HTML file:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>External Style example</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <p>This text's style comes from an external CSS file</p>
</body>
</html>

style.css file:

CSS
p {
  color: red;
  font-size: 16px;
  line-height: 1.8;
}

Characteristics:

@import Method (Just for awareness)

⚠️ Note: @import loads referenced files after the CSS file finishes loading, causing "flash of unstyled content" on the page. This method is largely obsolete in modern projects, replaced by <link>.

Besides the <link> tag, you can also use @import in CSS files to import other CSS files:

Example

HTML
<style>
@import url("reset.css");
@import url("layout.css");

p {
  color: red;
}
</style>
<p>This is a paragraph example.</p>
▶ Try it Yourself

But this method has worse performance than <link> tags and is not recommended.

Comparison of Three Methods

Feature Inline Styles Internal Styles External Styles
Reusability Not reusable Single-page reuse Multi-page sharing
Structure/Style Separation ❌ Completely mixed ✅ Separated (same file) ✅ Completely separated
Priority Highest Medium Lowest
Loading Performance No extra requests No extra requests Requires extra HTTP request
Recommended Level ⭐⭐ ⭐⭐⭐⭐⭐

Priority Rules

When all three methods appear simultaneously, the priority rule is:

Inline Styles > Internal Styles > External Styles

Note: This refers to the priority of inclusion methods, not selector specificity. Selector specificity is compared "within the same inclusion method," while inclusion method priority is only considered when styles from different methods conflict.


❓ FAQ

Q What scenarios are the three CSS inclusion methods suitable for?
A External stylesheets are the standard for production (multiple pages share one .css file, browser caching is fast); internal styles are suitable for single-page applications or homepage above-the-fold styles; inline styles are only recommended for temporary debugging, not for extensive use in formal projects.
Q What's the difference between link and @import?
A link is an HTML tag, @import is CSS syntax. link loads simultaneously with HTML, @import waits for the CSS file to finish loading before loading referenced files, making it slower. link is the recommended approach.
Q Do inline styles have the highest priority?
A Inline styles do have very high priority, but `!important` can override them. It's recommended to use selector specificity for style management rather than relying on !important, otherwise maintenance becomes painful later.

📖 Summary

📝 Exercises

  1. Create an HTML file using inline, internal, and external methods to set different styles.
  2. Create an external style.css file defining paragraph colors and heading styles, then include it in HTML.
  3. On the same element, use all three methods to set the same property (like color), observe which one takes effect, and verify the priority rules.
  4. Modify colors in the style.css file, refresh the browser to see changes, and experience the maintenance convenience of external styles.
100%

🙏 帮我们做得更好

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

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