3 Cascading Style Sheets

Web Technology · Unit 3 · 8 hrs

Cascading Style Sheets

Exam-focused notes for Cascading Style Sheets (Web Technology, CSC329): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.

Cascading Style Sheets in short

Once HTML supplies the structure of a page, CSS decides how it looks. This unit covers the CSS syntax, its selectors and core properties, the box model that governs layout, and the media queries and Bootstrap basics that make a page responsive.

Cascading Style Sheets: notes

Introduction to Cascading Style Sheets (CSS)

CSS (Cascading Style Sheets) is the language used to describe how HTML elements should be displayed on screen. Rather than repeating formatting instructions inside every page, CSS lets one set of rules control the layout and appearance of many pages at once, which is what makes a site-wide redesign a matter of editing one file instead of hundreds. The word "cascading" refers to the way rules from different sources (the browser's defaults, an external stylesheet, an internal stylesheet, an inline style) combine and override one another by specificity and order.

CSS Syntax

A CSS rule has two parts: a selector, which points to the HTML element to be styled, and a declaration block, wrapped in curly braces, containing one or more declarations separated by semicolons. Each declaration pairs a property with a value, separated by a colon.

p {
  color: red;
  text-align: center;
}

Here p is the selector (it targets every <p> element), color and text-align are properties, and red and center are their values.

Inserting CSS: Inline, Internal, External, ID and Class Selectors

CSS can be attached to a page in three ways. An inline style, written in the style attribute of a single tag, applies only to that one element and loses most of the benefits of a shared stylesheet, so it should be used sparingly.

<h1 style="color:blue; text-align:center;">This is a heading</h1>

An internal stylesheet, written inside a <style> element in the <head>, applies to every matching element on that one page.

<head>
<style>
h1 { color: blue; text-align: center; }
p { color: red; }
</style>
</head>

An external stylesheet is the recommended approach: all the rules live in a separate .css file, linked from the page's <head> with a <link> element, so that one file can restyle an entire website.

<head>
<link rel="stylesheet" href="mystyle.css">
</head>

Selectors decide which elements a rule targets. The element selector (p { ... }) matches every element of that tag name. The ID selector, written with a hash, matches the single element carrying that unique id attribute; an ID must not be reused on the same page. The class selector, written with a period, matches every element carrying that class attribute, and the same class can be applied to many elements at once.

#para1 { text-align: center; color: red; }   /* ID selector */
.para2 { text-align: center; color: red; }   /* class selector */

Colors

CSS colors can be specified in several equivalent formats. A built-in color name (red, green, blue) is the simplest. A hexadecimal value, a # followed by six hex digits, gives the red, green and blue components two digits each (#ff0000 is pure red). An RGB value, rgb(R, G, B), gives each component as an integer from 0 to 255. RGBA adds a fourth value, alpha, from 0.0 (fully transparent) to 1.0 (fully opaque). HSL, hsl(H, S%, L%), specifies hue as a degree on a 360-degree color wheel (0 red, 120 green, 240 blue), saturation as a percentage, and lightness as a percentage (0% black, 100% white).

h1 { color: #ff0000; }
p  { color: rgba(0, 0, 255, 0.5); }

What this unit covers

  • Introduction
  • Cascadding Style Sheets (CSS)
  • CSS Syntax
  • Inserting CSS: Inline, Internal, External, ID and Class Selectors
  • Colors
  • Backgrounds
  • Borders
  • Text
  • Font
  • List
  • Table
  • CSS Box Model
  • Normal Flow Box Layout: Basic Box Layout, Display Property, Padding, Margin
  • Positioning: Relative, Float, Absolute
  • CSS3 Borders
  • Box Shadows
  • Text Effects and shadow
  • Basics of Responsive Web Designs
  • Media Queries
  • Introduction to Bootstrap

Text Effects and shadow

2082 · 10 marks

Define CSS. What is the use of CSS shadow effects? Write an HTML script with proper CSS as per the following description. Display a text content "CSIT Exam" in a div with id div1. The text should have a horizontal shadow of 3px and a vertical shadow of 2px. The shadow color should be green. Also set the horizontal and vertical shadow of the div to 10px respectively with the shadow color of div to blue.[10]

Show solved answer

CSS: Definition, Shadow Effects, and HTML Script


1. Definition of CSS

CSS (Cascading Style Sheets) is a style sheet language used to describe the presentation and formatting of HTML documents. It controls the layout, colors, fonts, spacing, and overall visual appearance of web pages.

CSS separates content (HTML) from design (styling), making web pages more attractive, consistent, and easier to maintain across multiple pages.


2. Use of CSS Shadow Effects

CSS Shadow Effects are used to add visual depth and dimension to web page elements. They make text and boxes stand out from the background, improving the overall visual appeal of a webpage.

CSS provides two main shadow properties:

PropertyPurpose
text-shadowAdds shadow to text content
box-shadowAdds shadow to HTML elements (div, p, etc.)

Syntax:

text-shadow: h-shadow  v-shadow  color;
box-shadow:  h-shadow  v-shadow  color;

Where:

  • h-shadow -- Horizontal offset of the shadow (in px)
  • v-shadow -- Vertical offset of the shadow (in px)
  • color -- Color of the shadow

3. HTML Script with CSS Shadow Effects

Requirements:

  • A div with id="div1" containing the text "CSIT Exam"
  • Text shadow: horizontal = 3px, vertical = 2px, color = green
  • Box shadow: horizontal = 10px, vertical = 10px, color = blue
<!DOCTYPE html>
<html>
  <head>
    <title>CSS Shadow Effects</title>
    <style>
      #div1 {
        font-size: 30px;
        padding: 20px;
        margin: 50px;
        text-shadow: 3px 2px green;
        box-shadow: 10px 10px blue;
      }
    </style>
  </head>
  <body>
    <div id="div1">
      CSIT Exam
    </div>
  </body>
</html>

4. Explanation of Properties Used

PropertyValueMeaning
text-shadow3px 2px green3px horizontal, 2px vertical, green colored shadow on the text
box-shadow10px 10px blue10px horizontal, 10px vertical, blue colored shadow on the div
font-size30pxSets text size for visibility
padding20pxAdds inner spacing inside the div
margin50pxAdds outer spacing around the div

Conclusion

CSS shadow effects enhance the visual appearance of web pages by adding depth to both text and box elements. The text-shadow property applies shadow to text content, while the box-shadow property applies shadow to block-level elements such as div. Both properties accept horizontal offset, vertical offset, and color as values.

Open this answer on its own page →

2081 · 5 marks

What is text-overflow property in CSS? How can you use clip and ellipsis in text-overflow? [5]

The CSS text-overflow property specifies how overflowed content that is not displayed should be signaled (viewed) to the user. When text is too long to fit within its container, this property controls how the overflow is indicated to the user. Note: For text-overflow to work, the element typicall...

Open this answer on its own page →

Positioning

2082 · 5 marks

Create an HTML page containing three paragraphs p1, p2 and p3 with some contents. Write internal CSS to set the position of p1 to relative, p2 to float and p3 to absolute. [5]

Show solved answer

HTML Page with CSS Positioning (p1: relative, p2: float, p3: absolute)

Concept (from notes)

  • position: relative - element is positioned relative to its normal position in the document flow.
  • float - used to float an element to the left or right, allowing other content to wrap around it.
  • position: absolute - element is positioned relative to the nearest positioned ancestor; removed from normal document flow and can overlap other elements.

HTML Code

<!DOCTYPE html>
<html>
  <head>
    <title>CSS Positioning Example</title>

    <style>
      /* p1: position relative */
      #p1 {
        position: relative;
        top: 20px;
        left: 30px;
        background-color: lightblue;
        width: 300px;
        padding: 10px;
      }

      /* p2: float */
      #p2 {
        float: left;
        background-color: lightgreen;
        width: 250px;
        padding: 10px;
        margin: 10px;
      }

      /* p3: position absolute */
      #p3 {
        position: absolute;
        top: 200px;
        left: 400px;
        background-color: lightyellow;
        width: 300px;
        padding: 10px;
      }
    </style>

  </head>

  <body>

    <h2>CSS Positioning Demo</h2>

    <p id="p1">
      This is paragraph P1. Its position is set to <strong>relative</strong>.
      It is shifted 20px from top and 30px from left relative to its
      normal position in the document flow.
    </p>

    <p id="p2">
      This is paragraph P2. It uses <strong>float: left</strong>.
      This element floats to the left and other content
      wraps around it.
    </p>

    <p id="p3">
      This is paragraph P3. Its position is set to <strong>absolute</strong>.
      It is positioned relative to the nearest positioned ancestor
      or the document body. It is removed from the normal document flow.
    </p>

  </body>
</html>

Output Description

ParagraphCSS PropertyEffect
p1position: relativeShifted 20px down and 30px right from its normal position
p2float: leftFloats to the left; other content wraps around it
p3position: absolutePlaced at exact coordinates (top: 200px, left: 400px) from the document body; removed from normal flow

Key Notes

  • Internal CSS is written inside the <style> tag placed within the <head> section.
  • The top, left, right, and bottom properties only work when the position property is explicitly set.
  • Absolute positioned elements are removed from normal flow and can overlap other elements.
Open this answer on its own page →

2080 · 5 marks

How can you define position property for specifying specific positioning of an HTML element? Write internal CSS to illustrate position of an HTML div. [5]

The position property in CSS specifies the type of positioning method used for an HTML element. There are five different position values: Value Description -------------------- static Default value; positioned according to the normal flow of the page. Not affected by top, bottom, left, right prop...

Open this answer on its own page →

2076 · 10 marks

How positioning of HTML elements can be done using CSS? Create a HTML page with a div with some content and two paragraph with some contents having id p1 and p2. Write external CSS for the div tag having fixed position with border style solid and width 2px. the p1 should have relative position. The font type of p1 should be Arial and color should be red. The p2 have absolute position with top 0px and left 200px.[10]

The position property specifies the type of positioning method used for an element. There are five different position values: Value Description -------------------- static Default; positioned according to normal flow of the page relative Positioned relative to its normal position fixed Positioned...

Open this answer on its own page →

Inserting CSS

2081 · 5 marks

How internal and external CSS differ from each other. Illustrate with example. [5]

CSS (Cascading Style Sheets) is used to control the presentation and layout of HTML elements. It can be applied in different ways, two of which are Internal CSS and External CSS. --- - Internal CSS is written inside the HTML document itself, within the <style tag placed in the <head section. - It...

Open this answer on its own page →

2079 · 5 marks

What are the usages of class and id selectors in CSS? Illustrate with example. [5]

The ID selector uses the id attribute of an HTML element to select a specific unique element. The id of an element must be unique within a page, so the ID selector is used to select only one unique element. To select an element with a specific ID, we write a hash () character followed by the ID o...

Open this answer on its own page →

2078 · 10 marks

How external CSS is inserted in an HTML page? Create an HTML page with two paragraph tags with id p1 and p2, and one div tag with id dv1. Write an external CSS file as per the description. the margin of the paragraphs should be 100px from the top and 80px from the left. Both of paragraphs should have the display set to block. The dv1 should have padding left and right set to 30px and 25px respectively, its background color should be blue and the font color should be red with the font type Verdana. Set the display of dv1 to none.[10]

Show solved answer

External CSS in HTML - Complete Answer

How External CSS is Inserted in an HTML Page

External CSS is a separate .css file that contains all the style rules. It is linked to an HTML page using the <link> tag inside the <head> section of the HTML document.

Syntax:

<link rel="stylesheet" type="text/css" href="filename.css">
  • rel="stylesheet" defines the relationship between the HTML file and the linked file.
  • type="text/css" specifies the media type (optional in HTML5).
  • href="filename.css" specifies the path to the external CSS file.

Advantages of External CSS:

  • One CSS file can control the style of multiple HTML pages.
  • Changes made in one file reflect across all linked pages.
  • Keeps HTML structure and styling separate (clean code).

HTML File (index.html)

<!DOCTYPE html>
<html>
  <head>
    <title>External CSS Example</title>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>

    <p id="p1">This is Paragraph 1 (id = p1)</p>

    <p id="p2">This is Paragraph 2 (id = p2)</p>

    <div id="dv1">
      This is a div block (id = dv1)
    </div>

  </body>
</html>

External CSS File (style.css)

/* Styles for paragraph with id p1 */
#p1 {
    margin-top: 100px;
    margin-left: 80px;
    display: block;
}

/* Styles for paragraph with id p2 */
#p2 {
    margin-top: 100px;
    margin-left: 80px;
    display: block;
}

/* Styles for div with id dv1 */
#dv1 {
    padding-left: 30px;
    padding-right: 25px;
    background-color: blue;
    color: red;
    font-family: Verdana;
    display: none;
}

Explanation of CSS Properties Used

PropertyElementValueDescription
margin-top#p1, #p2100pxSets top margin to 100px
margin-left#p1, #p280pxSets left margin to 80px
display#p1, #p2blockDisplays paragraphs as block elements
padding-left#dv130pxSets left padding to 30px
padding-right#dv125pxSets right padding to 25px
background-color#dv1blueSets background color to blue
color#dv1redSets font/text color to red
font-family#dv1VerdanaSets font type to Verdana
display#dv1noneHides the div element completely

Note: The # selector in CSS is used to target elements by their id attribute. Since id values are unique per page, #p1, #p2, and #dv1 target exactly those specific elements.

Open this answer on its own page →

Text

2079 · 5 marks

What is word-wrap property in CSS? Write an HTML script to illustrate the word-wrap property. [5]

The CSS word-wrap property allows long words to be broken and wrapped onto the next line. If a word is too long to fit within an area, it normally expands outside the container. The word-wrap property forces the text to wrap within the boundary of the containing element. Syntax: Value Description...

Open this answer on its own page →

CSS Box Model

2078 · 5 marks

Describe CSS Box model with example. [5]

The CSS Box Model is a fundamental concept in web design that describes how every HTML element is rendered as a rectangular box on a webpage. According to standard web technology principles, every element on a page is treated as a box consisting of four layers (from inside to outside): --- Compon...

Open this answer on its own page →

Media Queries

2076 · 5 marks

How web pages can be made responsive using media queries? Illustrate with an example. [5]

A media query is a CSS technique that applies styles only when certain conditions (such as screen width, device type, or orientation) are met. It allows a single web page to adapt its layout and appearance for different screen sizes (desktops, tablets, mobiles), making the page responsive. --- Me...

Open this answer on its own page →