0 2

BIT251 ยท 0 ยท 10 marks

What is the use of CSS? Describe various methods of inserting CSS in HTML. Give proper examples.

What is the use of CSS? Describe various methods of inserting CSS in HTML. Give proper 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, paper, or in other media.

CSS: Uses and Methods of Insertion

What is CSS?


Uses of CSS

  • Styling and Layout: Controls colors, fonts, sizes, spacing, borders, and backgrounds of HTML elements.
  • Separation of Content and Presentation: Keeps HTML structure separate from visual design, making code cleaner and easier to maintain.
  • Consistency: A single CSS file can style multiple HTML pages uniformly across an entire website.
  • Responsiveness: CSS enables responsive web design so pages look good on all screen sizes (mobile, tablet, desktop).
  • Reduced Redundancy: Instead of repeating style attributes in every HTML tag, CSS applies styles globally.
  • Animations and Effects: CSS supports transitions, animations, and hover effects without JavaScript.
  • Accessibility: Proper CSS improves readability and user experience.

Methods of Inserting CSS in HTML

There are three main methods to insert CSS into an HTML document:


1. Inline CSS

CSS is applied directly inside an HTML element using the style attribute.

Syntax:

<tagname style="property: value;">

Example:

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

Advantages:

  • Quick and easy for a single element.
  • Useful for testing or overriding other styles.

Disadvantages:

  • Hard to maintain for large projects.
  • Mixes content with presentation.
  • Cannot reuse styles across multiple elements.

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: darkblue;
        text-align: center;
      }
      p {
        font-size: 18px;
        color: green;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to My Page</h1>
    <p>This paragraph is styled using internal CSS.</p>
  </body>
</html>

Advantages:

  • Styles apply to the entire page.
  • No need for a separate file.

Disadvantages:

  • Styles cannot be shared across multiple HTML 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.

Syntax (in HTML file):

<head>
  <link rel="stylesheet" type="text/css" href="filename.css">
</head>

Example:

File: style.css

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

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

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

File: index.html

<!DOCTYPE html>
<html>
  <head>
    <title>External CSS Example</title>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <h1>My Website</h1>
    <p>This page is styled using an external CSS file.</p>
  </body>
</html>

Advantages:

  • One CSS file can style multiple HTML pages.
  • Clean separation of content and design.
  • Easy to maintain and update.
  • Browser caches the CSS file, improving load speed.

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 <style> tagSeparate .css file
ScopeSingle elementSingle pageMultiple pages
ReusabilityNoNoYes
MaintenanceDifficultModerateEasy
Best UseQuick fixesSingle page stylingLarge websites

Cascading Priority

When multiple methods are used together, CSS follows this priority order (highest to lowest):

Inline CSS  >  Internal CSS  >  External CSS

This means inline styles override internal styles, which override external styles.


Conclusion

CSS is an essential technology for web development. Among the three methods, External CSS is the most preferred for professional web development because it promotes reusability, maintainability, and clean code structure.

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