CSS How To
CSS How-To: External, Internal, and Inline Stylesheets
You can attach CSS to an HTML page using three methods: linking an external .css file, writing an internal style block inside head, or adding an inline style attribute on an element. Picking the right method affects page speed, caching, and long-term maintainability.
Comparing the Three Approaches
- External Stylesheet: Written in a separate
styles.cssfile and loaded via alinktag withrel="stylesheet"insidehead. This is best for production sites because browsers cache the file once across every page visit. - Internal Stylesheet: Placed inside a
styleblock in the documenthead. Useful for isolated single-page prototypes or injecting small critical-path CSS. - Inline Styles: Written directly inside an element's
style="..."attribute. Highest cascade priority, ideal only for dynamic JavaScript measurements or HTML email clients that stripheadstyles.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Insertion Methods</title>
<link rel="stylesheet" href="/css/base.css">
<style>
.status-pill {
display: inline-block;
padding: 4px 10px;
background-color: #dcfce7;
color: #166534;
border-radius: 999px;
font-weight: 600;
}
</style>
</head>
<body>
<span class="status-pill">Build Passing</span>
<p style="color: #475569;">Inline styles override external and internal rules on this element.</p>
</body>
</html>
Cascade Priority Order
When rules conflict, inline style attributes override both internal and external rules. Between internal style blocks and external link tags, whichever appears last in head wins when specificity matches.
Common Mistakes to Avoid
- Wrapping rules inside
styletags inside a.cssfile: External.cssfiles must contain pure CSS only—HTML tags inside.cssbreak the parser. - Putting stylesheet
linktags at the bottom ofbody: Deferring core stylesheets causes visible layout flashes before styles paint. - Loading ten tiny CSS files separately over HTTP: Bundle or combine stylesheets during builds to minimize network round-trips.
Frequently Asked Questions
Which CSS method loads fastest for multi-page websites?
External stylesheets are fastest across repeat views because the browser downloads styles.css once and reuses it from cache on every page.
Does @import inside CSS hurt performance?
Yes. Using @import inside one CSS file forces sequential network downloads instead of parallel loading, so prefer link tags in HTML or build-time bundling.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.