Full answer
There are four ways to add CSS.
- External file with
<link rel="stylesheet" href="site.css">in the head. This is the normal choice. The browser downloads it once, caches it, and shares it between pages. Several pages stay consistent with one change. - Internal
<style>element in the head. It is useful for small pages, for a small amount of critical CSS, and for HTML emails and examples. The styles cannot be cached apart from the page. - Inline
style="..."attribute on one element. It has very high specificity, so it is hard to override from a stylesheet, and it cannot use selectors such as:hoveror media queries. It is handy for values set by a script, such as a custom property or a position. Some content security policies block inline styles. @import url("more.css")inside CSS. It loads another stylesheet, but the browser finds it only after the first file is downloaded, so the files load one after the other. This slows rendering. Prefer severallinkelements, or combine files in a build step. Note that@importalso lets you place a file into a layer, for example@import url("vendor.css") layer(vendor).
CSS blocks rendering. The browser waits for stylesheets in the head before it paints, to avoid a flash of unstyled content. This is a reason to keep files small and to avoid long chains.
Order matters. When two rules have equal specificity, the later one wins, and this includes the order of link and style elements in the page.
Other tips:
- Use
mediaon alinkfor files that apply only in some cases, such asmedia="print". - Keep inline styles for values that really are dynamic.
- Cache static CSS files with a long lifetime and versioned file names.
The site.css file in this example sets .late { color: rgb(1, 2, 3); }. The class rule is more specific than p, so the first paragraph uses it. The inline style wins on the second paragraph, and the third paragraph uses the internal p rule.
Choose external files for the site, and use the other forms only where they are the better tool.
<link rel="stylesheet" href="/css/site.css">
<style>
p { color: red; }
</style>
<p class="late" id="a">External class wins over the internal element rule</p>
<p id="b" style="color: blue">Inline style</p>
<p id="c">Internal style</p>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- External files are cached and shared.
- A style element suits small pages and critical CSS.
- Inline styles have high specificity and no selectors.
- @import files load in a chain, one after the other.
- CSS blocks rendering, and order matters.
Common mistakes
- Using @import chains in production CSS.
- Putting styles inline everywhere, and then using !important to override them.
- Putting the stylesheet link at the end of the page, so users see unstyled content first.
Follow-ups
- Why is @import slower than a link element?
- When is an inline style a good choice?
Tips
- List the four ways, with one reason for or against each.
- Mention render blocking and caching.