Back to Home

🔸 1. Inline CSS

Definition:

Inline CSS is used to apply a unique style directly to a single HTML element using the style attribute.

Deep Dive into Inline CSS:

Inline CSS works by directly injecting styles into HTML elements through the style attribute. It has the highest specificity in the CSS cascade, meaning it will override any other CSS rules targeting the same element. The specificity value for inline styles is 1000, compared to 100 for IDs and 10 for classes.

Important: Inline styles are parsed and applied immediately during HTML rendering, which can impact initial page load performance if overused.

Syntax:

<p style="color: blue; font-size: 16px;">This is an inline styled paragraph.</p>

Common Inline CSS Properties:

Use Cases:

Pros:

Cons:


🔸 2. Internal CSS

Definition:

Internal CSS is defined within a <style> tag in the <head> section of an HTML document.

Deep Dive into Internal CSS:

Internal CSS follows the standard CSS cascade rules and specificity calculations. It allows for the use of all CSS selectors, pseudo-classes, and media queries. The styles are parsed when the HTML document loads and remain in the browser's memory for that page only.

Internal CSS supports advanced CSS features like:

Syntax:

<!DOCTYPE html>
<html>
<head>
  <style>
    p {
      color: green;
      font-size: 18px;
    }
  </style>
</head>
<body>
  <p>This paragraph is styled using internal CSS.</p>
</body>
</html>

Use Cases:

Pros:

Cons:


🔸 3. External CSS

Definition:

External CSS is defined in a separate .css file and linked to the HTML document using the <link> tag.

Deep Dive into External CSS:

External CSS represents the most scalable approach to styling web applications. It implements the separation of concerns principle by completely separating structure (HTML) from presentation (CSS).

Key concepts:

Syntax (HTML):

<head>
  <link rel="stylesheet" href="styles.css">
</head>

Syntax (styles.css):

p {
  color: red;
  font-size: 20px;
}

Use Cases:

Pros:

Cons:


🔹 Comparison Table

Feature Inline CSS Internal CSS External CSS
Location Inside HTML element Inside <style> tag in <head> Separate .css file
Reusability ❌ No ❌ No ✅ Yes
Maintainability ❌ Poor ⚠️ Medium ✅ Good
Overrides ✅ Highest ⚠️ Medium ❌ Lowest
Use Case Quick fixes, JS apps Single-page apps Large websites