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.
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 answerHide 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:
| Property | Purpose |
|---|---|
text-shadow | Adds shadow to text content |
box-shadow | Adds 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
divwithid="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
| Property | Value | Meaning |
|---|---|---|
text-shadow | 3px 2px green | 3px horizontal, 2px vertical, green colored shadow on the text |
box-shadow | 10px 10px blue | 10px horizontal, 10px vertical, blue colored shadow on the div |
font-size | 30px | Sets text size for visibility |
padding | 20px | Adds inner spacing inside the div |
margin | 50px | Adds 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.
2081 · 5 marks
What is text-overflow property in CSS? How can you use clip and ellipsis in text-overflow? [5]
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 answerHide 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
| Paragraph | CSS Property | Effect |
|---|---|---|
| p1 | position: relative | Shifted 20px down and 30px right from its normal position |
| p2 | float: left | Floats to the left; other content wraps around it |
| p3 | position: absolute | Placed 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, andbottomproperties only work when thepositionproperty is explicitly set. - Absolute positioned elements are removed from normal flow and can overlap other elements.
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]
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]
Inserting CSS
2081 · 5 marks
How internal and external CSS differ from each other. Illustrate with example. [5]
2079 · 5 marks
What are the usages of class and id selectors in CSS? Illustrate with example. [5]
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 answerHide 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
| Property | Element | Value | Description |
|---|---|---|---|
margin-top | #p1, #p2 | 100px | Sets top margin to 100px |
margin-left | #p1, #p2 | 80px | Sets left margin to 80px |
display | #p1, #p2 | block | Displays paragraphs as block elements |
padding-left | #dv1 | 30px | Sets left padding to 30px |
padding-right | #dv1 | 25px | Sets right padding to 25px |
background-color | #dv1 | blue | Sets background color to blue |
color | #dv1 | red | Sets font/text color to red |
font-family | #dv1 | Verdana | Sets font type to Verdana |
display | #dv1 | none | Hides the div element completely |
Note: The
#selector in CSS is used to target elements by theiridattribute. Sinceidvalues are unique per page,#p1,#p2, and#dv1target exactly those specific elements.
Text
2079 · 5 marks
What is word-wrap property in CSS? Write an HTML script to illustrate the word-wrap property. [5]
CSS Box Model
Media Queries
2076 · 5 marks
How web pages can be made responsive using media queries? Illustrate with an example. [5]
Make Unit 3 stick
Practice CSC329 with flashcards & quizzes