2079 2

BIT251 ยท 2079 ยท 10 marks

What is CSS? How CSS is inserted in a HTML page? Illustrate with examples.

What is CSS? How CSS is inserted in a HTML page? Illustrate with examples.[10]

Web Technology IChapter 5: CSS Styling and LayoutInline internal and external CSS methods

Quick answer

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation and visual appearance of HTML documents. It controls how HTML elements are displayed on screen, including layout, colors, fonts, spacing, and other visual properties.

CSS: Definition and Insertion Methods

What is CSS?

Key Features of CSS:

  • Separates content (HTML) from presentation (styling)
  • Allows consistent styling across multiple web pages
  • Reduces code repetition
  • Improves page load time and maintainability
  • Supports responsive design for different screen sizes

Basic CSS Syntax:

selector {
    property: value;
    property: value;
}

Example:

h1 {
    color: blue;
    font-size: 24px;
}

How CSS is Inserted in an HTML Page

There are three ways to insert CSS into an HTML page:


1. Inline CSS

CSS is applied directly to a single HTML element using the style attribute.

Syntax:

<tagname style="property: value; property: value;">

Example:

<!DOCTYPE html>
<html>
<body>
    <h1 style="color: red; font-size: 30px;">Hello World</h1>
    <p style="color: blue; text-align: center;">This is a paragraph.</p>
</body>
</html>

Advantages:

  • Quick and easy for single element styling
  • Highest specificity (overrides other styles)

Disadvantages:

  • Not reusable; must be repeated for each element
  • Mixes content with presentation
  • Hard to maintain for large projects

2. Internal (Embedded) CSS

CSS is written inside the <style> tag within the <head> section of the HTML document.

Syntax:

<head>
    <style>
        selector {
            property: value;
        }
    </style>
</head>

Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            background-color: lightyellow;
        }
        h1 {
            color: green;
            text-align: center;
        }
        p {
            font-size: 16px;
            color: gray;
        }
    </style>
</head>
<body>
    <h1>Welcome to CSS</h1>
    <p>This page uses internal CSS styling.</p>
</body>
</html>

Advantages:

  • Styles apply to the entire page
  • No need for a separate file
  • Useful for single-page websites

Disadvantages:

  • Styles cannot be shared across multiple pages
  • Increases HTML file size

3. External CSS

CSS is written in a separate .css file and linked to the HTML document using the <link> tag inside the <head> section.

HTML File (index.html):

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <h1>Welcome to External CSS</h1>
    <p>This page is styled using an external CSS file.</p>
    <a href="#">Click Here</a>
</body>
</html>

CSS File (styles.css):

body {
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
}

h1 {
    color: navy;
    text-align: center;
}

p {
    font-size: 18px;
    color: #333333;
    line-height: 1.6;
}

a {
    color: orange;
    text-decoration: none;
}

Advantages:

  • One CSS file can style multiple HTML pages
  • Clean separation of content and design
  • Easy to maintain and update
  • Reduces code duplication
  • Browser caches the CSS file for faster loading

Disadvantages:

  • Requires an extra HTTP request to load the file
  • Page may appear unstyled briefly if CSS loads slowly

Comparison Table

FeatureInline CSSInternal CSSExternal CSS
LocationInside HTML tagInside <head>Separate .css file
ScopeSingle elementSingle pageMultiple pages
ReusabilityNoNoYes
MaintenanceDifficultModerateEasy
Best UseQuick fixesSingle pageLarge projects

Cascading Order (Priority)

When multiple styles conflict, CSS follows this priority order:

Inline CSS  >  Internal CSS  >  External CSS  >  Browser Default
(Highest)                                         (Lowest)

Summary

CSS is a powerful tool for styling web pages. The three insertion methods -- inline, internal, and external -- each serve different purposes. For professional and large-scale web development, external CSS is the most preferred method as it promotes reusability, clean code structure, and easier maintenance.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.