Techoceanhub logo

Inline, internal, and external stylesheets

Inline, internal, and external stylesheets are three ways of applying CSS (Cascading Style Sheets) to HTML documents to control the appearance and layout of web pages.

  1. Inline Styles: Inline styles are applied directly to individual HTML elements using the style attribute. The CSS rules are defined within double quotes or single quotes and are directly added to the specific HTML tag. For example:
<p style="color: red; font-size: 16px;">This is a paragraph with inline styles.</p>

Using inline styles is not recommended for large-scale styling because it mixes presentation with content and can make the code hard to maintain.

  1. Internal Stylesheet: Internal stylesheets are defined within the <head> section of an HTML document using the <style> element. The CSS rules are placed between the <style> tags, affecting the entire document or specific sections of it. For example:
<!DOCTYPE html>
<html>
<head>
    <title>Internal Stylesheet Example</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }
        h1 {
            color: blue;
        }
        p {
            font-size: 18px;
        }
    </style>
</head>
<body>
    <h1>Hello, this is a heading!</h1>
    <p>This is a paragraph with internal styles.</p>
</body>
</html>

Internal stylesheets are more organized than inline styles but still apply to a single HTML document only.

  1. External Stylesheet: An external stylesheet is a separate CSS file that is linked to an HTML document using the <link> element. This allows you to keep the CSS code in a separate file, making it easy to manage and apply to multiple HTML documents. For example:

Create a file named “styles.css” with the following content:

/* styles.css */
body {
    font-family: Arial, sans-serif;
    background-color: #f0f0f0;
}
h1 {
    color: blue;
}
p {
    font-size: 18px;
}

Then, link the external stylesheet in the HTML file:

<!DOCTYPE html>
<html>
<head>
    <title>External Stylesheet Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Hello, this is a heading!</h1>
    <p>This is a paragraph with external styles.</p>
</body>
</html>

Using an external stylesheet is the most recommended method as it promotes better code organization, reusability, and maintainability across multiple HTML documents. Changes to the CSS will apply universally without needing to modify each HTML file individually.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .