Lesson 3.2: Inline, Internal & External CSS
CSS can be applied to HTML documents in three main ways:
-
Inline CSS
-
Internal CSS
-
External CSS
Each method has its own advantages and disadvantages. Choosing the right approach depends on the size of the project and the level of control you need.
1. Inline CSS
-
Definition: CSS written directly inside an HTML element’s
styleattribute. -
Syntax:
-
Pros:
-
Quick and easy for small changes.
-
Useful for testing styles.
-
-
Cons:
-
Not reusable.
-
Makes HTML messy and hard to maintain.
-
Not recommended for large projects.
-
2. Internal CSS
-
Definition: CSS written inside the
<style>tag within the<head>section of an HTML document. -
Syntax:
-
Pros:
-
Keeps CSS and HTML in the same file (easy for small projects).
-
Styles can be applied to multiple elements in one place.
-
-
Cons:
-
Not reusable across multiple HTML files.
-
Increases file size when used in large websites.
-
3. External CSS
-
Definition: CSS written in a separate
.cssfile and linked to the HTML document using the<link>tag. -
Syntax:
HTML File (index.html)
CSS File (style.css)
-
Pros:
-
Styles are reusable across multiple HTML files.
-
Cleaner and more organized code.
-
Best practice for medium and large projects.
-
-
Cons:
-
Requires additional HTTP request to load the CSS file (though modern browsers cache this).
-
Comparison Table
| Method | Location | Reusability | Best For |
|---|---|---|---|
| Inline CSS | Inside HTML element | ❌ No | Quick testing, small changes |
| Internal CSS | <style> in <head> |
⚠️ Limited | Small projects, single page |
| External CSS | Separate .css file |
✅ Yes | Large projects, multiple pages |
Best Practice
-
Avoid inline CSS (except for testing).
-
Use internal CSS only for small, single-page projects.
-
Prefer external CSS for scalability, maintainability, and cleaner structure.
✨ Quick Exercise:
-
Create a paragraph with inline CSS that makes the text blue.
-
Create an HTML file with internal CSS that makes the background light gray and headings red.
-
Create a separate
style.cssfile that changes the font of all paragraphs to Arial, 16px, dark green and link it to your HTML file.
