CSS can be attached to a page in three ways. Each way has a different reach, and choosing the right one keeps a project easy to maintain.
Inline Style
An inline style is written in the style attribute of a single element, for example <p style="color: red;">. It affects only that element. It is quick for a test, but the rules cannot be reused, and a page full of inline styles is hard to change.
Definition: Inline style. A declaration written in the style attribute of a single element.
Internal Style Sheet
An internal style sheet is written in a <style> element inside the <head> of the page. Its rules apply to the whole page. This method suits a page whose style is used nowhere else, or a short demonstration. The rules cannot be shared with other pages.
External Style Sheet
An external style sheet is a separate file with the extension .css. It contains only CSS rules. Each page that wants the style links to the file using a <link> element in the head, with rel="stylesheet" and an href that gives the file's path.
Definition: External style sheet. A separate .css file that many pages can link to.
A website with fifty pages can share one external style sheet. A change to a colour in that file shows on all fifty pages. For this reason external style sheets are the normal choice for real websites.
Table 2.2: The three ways of adding CSS
| Method | Where written | Reach | Best use |
|---|---|---|---|
| Inline | style attribute | One element | Quick tests |
| Internal | style element in head | One page | Single-page styling |
| External | Separate .css file | Whole site | Real websites |
Which Rule Wins
When two rules set the same property on one element, the browser must choose. An inline style overrides the style from a sheet. When rules have equal weight, the one that comes later in the file wins. The next topic explains the weight of selectors in detail.
Example 2.2: The same colour in three ways
Inline: the style applies to one paragraph only. Internal: the rule in the style element applies to every paragraph on this page.
<style>
p { color: green; }
</style>
<p>Green, from the internal style sheet.</p>
<p style="color: red;">Red, because inline wins.</p>
Output in the browser:
External: place the rule p { color: green; } in style.css and link it:
<link rel="stylesheet" href="style.css">
Common mistakes
- Placing the link element in the body instead of the head.
- Writing the wrong file path in href, so that the style sheet is not found.
- Including HTML tags such as style inside the external .css file.
- Using inline styles everywhere, which makes later changes slow and error-prone.
Key takeaways
- Inline styles affect one element, internal styles one page, and external style sheets a whole site.
- An external sheet is linked from the head with rel="stylesheet" and an href.
- An inline style overrides the style from a sheet.
- With equal weight, the later rule wins.
Practice questions
- Which element links an external style sheet to a page?
- Which method of adding CSS is best for a large website?
- Where is an internal style sheet written?
Answers to the practice questions
- The link element, placed in the head.
- An external style sheet.
- In a style element inside the head.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II
