Differentiate between inline, internal, and external CSS.

Started by Megon, Apr 29, 2024, 02:43 PM

Previous topic - Next topic

Megon

 Differentiate between inline, internal, and external CSS.

Lerok

Inline CSS, internal CSS, and external CSS are three different methods used to apply Cascading Style Sheets (CSS) to HTML documents. Here's how they differ:

1. **Inline CSS**:
   - Inline CSS involves styling HTML elements directly within the HTML document using the `style` attribute.
   - CSS rules are applied directly to individual HTML elements, affecting only those specific elements.
   - Inline CSS is typically used for styling individual elements with unique styles or for overriding styles applied by external or internal CSS.
   - Example:
     ```html
     <p style="color: blue; font-size: 16px;">This is a paragraph with inline CSS.</p>
     ```

2. **Internal CSS**:
   - Internal CSS involves defining CSS rules within the `<style>` element in the `<head>` section of an HTML document.
   - CSS rules specified within the `<style>` element apply to the entire HTML document or to specific sections defined by HTML elements.
   - Internal CSS is useful for applying styles consistently across multiple elements within a single HTML document.
   - Example:
     ```html
     <!DOCTYPE html>
     <html>
     <head>
         <style>
             p {
                 color: red;
                 font-size: 18px;
             }
         </style>
     </head>
     <body>
         <p>This is a paragraph with internal CSS.</p>
     </body>
     </html>
     ```

3. **External CSS**:
   - External CSS involves defining CSS rules in a separate CSS file and linking it to an HTML document using the `<link>` element.
   - CSS rules specified in the external CSS file apply to the HTML document or multiple HTML documents linked to it.
   - External CSS promotes separation of concerns, allowing for easier management and maintenance of styles across multiple HTML documents.
   - Example (external.css):
     ```css
     /* external.css */
     p {
         color: green;
         font-size: 20px;
     }
     ```
   - Example (HTML document):
     ```html
     <!DOCTYPE html>
     <html>
     <head>
         <link rel="stylesheet" type="text/css" href="external.css">
     </head>
     <body>
         <p>This is a paragraph with external CSS.</p>
     </body>
     </html>
     ```

In summary, inline CSS applies styles directly to individual HTML elements using the `style` attribute, internal CSS defines styles within the `<style>` element in the `<head>` section of an HTML document, and external CSS defines styles in a separate CSS file linked to an HTML document using the `<link>` element. Each method offers different advantages and is suited to different scenarios based on the requirements of the project.

Didn't find what you were looking for? Search Below