INSIGHTS
Software Development

Types of CSS Explained: A Complete Guide to Using CSS in HTML

In this article
  1. Inline CSS: a declaration attached to one element
  2. Internal CSS: rules embedded in the document
  3. External CSS: reusable rules in a separate file
  4. Which declaration takes effect when rules conflict?
  5. Responsive styling and browser inspection
  6. Decision guide for real projects

CSS can be placed directly on an HTML element, inside a document-level <style> element, or in a separate stylesheet. These are delivery approaches, not different languages and not three separate cascade engines. They all produce CSS declarations that the browser evaluates using origin, importance, cascade layers, specificity, scoping and source order. Choosing where to keep a rule is primarily a maintainability decision.

Inline CSS: a declaration attached to one element

<p style="color: #163a70; font-weight: 600;">Important update</p>

The style attribute affects that element only. It can be useful for a tightly scoped email template or a value generated dynamically, but it makes site-wide changes harder and mixes content with presentation. Normal inline styles outweigh normal author stylesheet rules for the same property. They do not automatically beat every possible !important declaration, browser rule or transition. Avoid teaching “inline always wins” as an unconditional rule.

Internal CSS: rules embedded in the document

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <style>
    .notice { color: #163a70; font-weight: 600; }
  </style>
</head>
<body>
  <p class="notice">Important update</p>
</body>
</html>

A <style> block belongs in the document, ordinarily its <head>. It is useful for a self-contained demonstration or a one-off page. Unlike an inline declaration, a selector can apply to many elements at once. Embedded styles become repetitive if the same design system must appear on dozens of pages.

External CSS: reusable rules in a separate file

<!-- index.html -->
<link rel="stylesheet" href="/css/site.css">
<p class="notice">Important update</p>

/* /css/site.css */
.notice { color: #163a70; font-weight: 600; }

This arrangement separates content from presentation and lets multiple pages share the same class definition. Browsers can cache the stylesheet across requests. Ensure the file URL resolves under the deployed site, set appropriate cache policies, and use the browser network panel when a rule appears missing. A wrong path or a selector typo is a more common cause of “CSS does not work” than a complicated cascade issue.

Which declaration takes effect when rules conflict?

/* site.css */
.notice { color: teal; }
#message { color: navy; }

<!-- HTML -->
<p class="notice" id="message" style="color: purple">Status</p>

For the same property, with ordinary author rules and no layering complications, the normal inline declaration makes the text purple. Without it, the ID selector is more specific than the class selector, so the text would be navy. Specificity is considered only after the cascade determines which origin, importance and layer applies. A common mistake is to compare selector specificity before checking !important or whether a rule belongs to a layer.

For larger applications, use modest selector specificity and build components around classes. Avoid repeatedly overriding one rule with another !important; the effect becomes difficult to reason about, particularly with themes, CSS modules and third-party widgets. The MDN cascade explanation and MDN specificity guide provide the exact precedence model.

Responsive styling and browser inspection

@media (max-width: 40rem) {
  .notice { font-size: 0.95rem; }
}

@media (prefers-reduced-motion: reduce) {
  .animated { transition: none; }
}

Media queries are independent of where the stylesheet is stored. A rule may be present but inactive because its media condition is false. Use browser developer tools to inspect matched selectors, computed values, inherited properties and crossed-out declarations. Test narrow and wide viewports, keyboard focus indicators and text contrast rather than assuming that the page looks correct because one desktop screenshot did.

Decision guide for real projects

  • Use external stylesheets for shared visual systems, reusable components and production sites.
  • Use a small internal stylesheet for an isolated page or a self-contained demonstration when separate file management would add no value.
  • Use inline declarations sparingly for values that truly belong to one element; do not use them as a substitute for classes.
  • Keep HTML semantics separate from styling: <strong> and <em> signal emphasis, while CSS handles visual presentation.

The existing article on HTML emphasis and visual formatting helps distinguish semantic tags from CSS typography. The practical principle is consistency: choose a maintainable stylesheet location, then debug actual cascade precedence rather than treating inline, internal and external styles as competing technologies.

Filed under Software Development