Three Ways to Apply CSS
Learning Objectives
- Use inline styles.
- Write an internal stylesheet.
- Connect an external CSS file.
- Choose a maintainable styling approach.
1. Inline CSS
<p style="color: red; font-size: 18px;">Styled paragraph</p>Inline CSS uses the HTML style attribute. It is useful for quick demonstrations but can make large projects harder to maintain.
2. Internal CSS
<head>
<style>
p { color: blue; }
</style>
</head>An internal stylesheet uses the style element, normally in the document head. It is useful for a single page or small demonstrations.
3. External CSS
HTML file:
<link rel="stylesheet" href="styles.css">styles.css:
body {
font-family: Arial, sans-serif;
}
h1 {
color: darkblue;
}External stylesheets separate presentation from HTML and can be reused across pages. Correct paths and server responses are important.
Which Approach Should You Use?
- Inline: small, exceptional cases and demonstrations.
- Internal: page-specific styles or learning exercises.
- External: recommended default for maintainable multi-page websites.
Combining CSS Sources
A page can use more than one CSS source. The cascade resolves conflicts according to origin, importance, cascade layers, specificity, scope, and source order where applicable. Inline styles do not universally override every possible declaration.
Practice
Create index.html and styles.css. Link them correctly and style a heading, paragraph, and button using the external stylesheet.
Quiz
- Where is inline CSS written? (A) style attribute (B) CSS file only (C) SQL file (D) URL fragment. Answer: A. Inline declarations are placed in the style attribute.
- Which element contains an internal stylesheet? (A) script (B) style (C) title (D) footer. Answer: B. The style element contains CSS.
- How do you link an external stylesheet? (A) link rel=stylesheet (B) img src (C) script type=text/css (D) form action. Answer: A. The link element connects a stylesheet.
- What file extension is commonly used for CSS? (A) .html (B) .css (C) .php (D) .sql. Answer: B. CSS files use the .css extension.
- Which approach is generally best for a large website? (A) External CSS (B) Inline styles everywhere (C) No stylesheets (D) Comments only. Answer: A. External CSS improves reuse and maintainability.
- Which attribute specifies the external stylesheet path? (A) href (B) alt (C) action (D) value. Answer: A. The href attribute points to the stylesheet.
- Can a page use multiple stylesheets? (A) Yes (B) No (C) Only on mobile (D) Only with PHP. Answer: A. Multiple stylesheets can be linked.
- What is a common problem with an external CSS file? (A) Incorrect file path (B) Too many HTML tags always (C) Missing database (D) Missing SQL table. Answer: A. An incorrect URL can prevent the stylesheet from loading.
- Does inline CSS always override every other CSS declaration? (A) Yes (B) No (C) Only in Chrome (D) Only on phones. Answer: B. Cascade origin, importance, and other rules matter.
- Why separate CSS from HTML? (A) Reuse and easier maintenance (B) To remove semantics (C) To eliminate browsers (D) To create databases. Answer: A. Separation makes styles easier to manage.