5 Css Styling And Layout

Web Technology I · Unit 5

CSS Styling and Layout

Exam-focused notes for CSS Styling and Layout (Web Technology I, BIT251): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.

CSS Styling and Layout in short

CSS controls how HTML looks. This chapter covers how to attach CSS, how to select elements, how the box model and positioning work, and how to style text.

CSS Styling and Layout: notes

CSS definition and purpose

CSS (Cascading Style Sheets) is a language that describes the presentation of an HTML document. HTML gives structure. CSS gives colour, size, spacing and layout.

A CSS rule has a selector and a declaration block. Each declaration is a property and a value.

p {
  color: blue;
  font-size: 16px;
}

Here p is the selector, color is the property and blue is the value.

  • It separates content from design.
  • One style sheet can style many pages.
  • It makes pages load faster and easier to maintain.
  • It allows different layouts for different screens.

Inline internal and external CSS methods

There are three ways to add CSS.

Inline CSS uses the style attribute on one element.

Internal CSS uses a <style> tag in the head of one page.

External CSS keeps rules in a .css file linked with <link>.

<link rel="stylesheet" href="style.css">
<style>
  h1 { color: green; }
</style>
<p style="color: red;">Inline style</p>
PointInlineInternalExternal
LocationIn the tagIn <style>In a separate file
ScopeOne elementOne pageWhole website
ReuseNoneWithin the pageMany pages
MaintenanceHardMediumEasy
PriorityHighestMiddleLowest, if equal specificity

When rules conflict, the cascade picks the winner by specificity and then by order. The last rule wins when both are equal.

What this unit covers

  • CSS definition and purpose
  • Inline internal and external CSS methods
  • CSS selectors class and id
  • CSS box model padding margin border
  • Normal box layout and basic box layout
  • CSS positioning static relative absolute fixed
  • CSS float property
  • CSS text styling and properties

CSS positioning static relative absolute fixed

2081 · 5 marks

Write HTML and equivalent CSS for some HTML elements to demonstrate relative and float positioning. [5]

In relative positioning, an element is positioned relative to its own normal position in the document flow. The element still occupies its original space, but it is visually shifted using top, bottom, left, or right properties. In float positioning, an element is removed from the normal document ...

Open this answer on its own page →

2080 · 5 marks

Write internal CSS to illustrate the static, relative, and fixed position property of an HTML element. [5]

The CSS position property controls how an element is placed in the document. The three key values are: Value Description -------------------- static Default positioning; element flows in normal document order relative Positioned relative to its own normal position fixed Positioned relative to the...

Open this answer on its own page →

0 · 5 marks

Describe the relative, float and absolute positioning with suitable examples. [5]

Since no specific curriculum notes were provided, this answer is based on standard CSS positioning concepts as taught in web technology courses. --- An element with position: relative is positioned relative to its own normal position in the document flow. The element still occupies its original s...

Open this answer on its own page →

Inline internal and external CSS methods

2081 · 5 marks

Consider an HTML file that contains three <div> elements and two <p> elements. The first <div> should have border color red with border size 1px. The next two <div> elements should have font-family Arial and text color blue. Both of the <p> elements should have background color yellow and their display property set to block type. Now write the equivalent external CSS for this case. [5]

Show solved answer

External CSS for the Given HTML Structure

HTML File (index.html)

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <div id="first-div">This is the first div</div>

    <div class="styled-div">This is the second div</div>
    <div class="styled-div">This is the third div</div>

    <p>This is the first paragraph</p>
    <p>This is the second paragraph</p>

</body>
</html>

External CSS File (styles.css)

/* Style for the first <div> - red border with 1px size */
#first-div {
    border-color: red;
    border-style: solid;
    border-width: 1px;
}

/* Style for the next two <div> elements - Arial font and blue text */
.styled-div {
    font-family: Arial;
    color: blue;
}

/* Style for both <p> elements - yellow background and block display */
p {
    background-color: yellow;
    display: block;
}

Explanation of Key Concepts

Selector UsedTarget ElementProperties Applied
#first-div (ID selector)First <div>border-color, border-style, border-width
.styled-div (Class selector)2nd and 3rd <div>font-family, color
p (Element selector)Both <p> elementsbackground-color, display

Why External CSS?

  • The CSS rules are written in a separate .css file (styles.css).
  • It is linked to the HTML file using the <link> tag inside the <head> section.
  • This is called External CSS and is preferred for maintaining separation of structure and style.

Note: border-style: solid is required alongside border-color and border-width for the border to be visible in browsers.

Open this answer on its own page →

2080 · 10 marks

How inline, internal, and external CSS are used in an HTML document. Illustrate with examples. Your CSS should include at least one class selector and id selector.[10]

CSS (Cascading Style Sheets) can be applied to an HTML document in three ways: Method Location Attribute/Tag Used -------------------------------------- Inline Inside the HTML element style attribute Internal Inside the <head section <style tag External Separate .css file <link tag --- Inline CSS...

Open this answer on its own page →

2079 · 10 marks

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

Show solved answer

CSS: Definition and Insertion Methods

What is CSS?

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.

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.

Open this answer on its own page →

2079 · 5 marks

Write an external CSS and link it to a HTML page containing HTML elements. Use your own assumptions for HTML elements and their CSS. [5]

External CSS is a separate .css file that contains all the styling rules. It is linked to an HTML page using the <link tag inside the <head section. Syntax to link external CSS: --- We create a simple student profile webpage with a heading, paragraph, a list, and a button. --- --- --- Feature Des...

Open this answer on its own page →

0 · 10 marks

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

Show solved answer

CSS: Uses and Methods of Insertion

What is CSS?

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.


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.

Open this answer on its own page →

Normal box layout and basic box layout

2079 · 5 marks

Write a CSS script to illustrate the concept of Normal Box Layout and Basic Box Layout. [5]

In CSS, the Box Model treats every HTML element as a rectangular box. Each box consists of: - Content - the actual content (text, image, etc.) - Padding - space between content and border - Border - surrounds the padding - Margin - space outside the border --- --- Property Normal Box Layout Basic...

Open this answer on its own page →

CSS box model padding margin border

2080.2 · 10 marks

What is CSS Box Model? Define the areas and edges in the CSS box model. Write CSS style for demonstrating CSS box model.[10]

The CSS Box Model is a fundamental concept in web design and layout that describes how every HTML element is rendered as a rectangular box on a webpage. According to this model, every element consists of multiple layers (areas) surrounding the actual content. These layers control spacing, borders...

Open this answer on its own page →

CSS selectors class and id

2080.2 · 5 marks

How class selector is different from id selector while defining the CSS for HTML? Support your answer with example. [5]

Feature Class Selector ID Selector --------- Syntax symbol . (dot) (hash) HTML attribute class="..." id="..." Reusability Can be applied to multiple elements Must be unique per page (only one element) Specificity Lower specificity Higher specificity (overrides class) Multiple use One element can ...

Open this answer on its own page →

0 · 5 marks

Consider a HTML page contains two divisions and one paragraph tags. The divisions have id div1 and div2 respectively. The color of text in both of divisions should be red and background color of divisions should be green. The font style in paragraph should be Arial and the size of font should be 14. Write necessary CSS for the given scenario. [5]

Selector Property Value Purpose ------------ div1, div2 color red Sets text color to red in both divisions div1, div2 background-color green Sets background color to green in both divisions p font-family Arial Sets font style to Arial in paragraph p font-size 14px Sets font size to 14 pixels in p...

Open this answer on its own page →