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.
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]
2080 · 5 marks
Write internal CSS to illustrate the static, relative, and fixed position property of an HTML element. [5]
0 · 5 marks
Describe the relative, float and absolute positioning with suitable examples. [5]
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 answerHide 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 Used | Target Element | Properties 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> elements | background-color, display |
Why External CSS?
- The CSS rules are written in a separate
.cssfile (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: solidis required alongsideborder-colorandborder-widthfor the border to be visible in browsers.
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]
2079 · 10 marks
What is CSS? How CSS is inserted in a HTML page? Illustrate with examples.[10]
Show solved answerHide 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
| Feature | Inline CSS | Internal CSS | External CSS |
|---|---|---|---|
| Location | Inside HTML tag | Inside <head> | Separate .css file |
| Scope | Single element | Single page | Multiple pages |
| Reusability | No | No | Yes |
| Maintenance | Difficult | Moderate | Easy |
| Best Use | Quick fixes | Single page | Large 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.
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]
0 · 10 marks
What is the use of CSS? Describe various methods of inserting CSS in HTML. Give proper examples.[10]
Show solved answerHide 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
| Feature | Inline CSS | Internal CSS | External CSS |
|---|---|---|---|
| Location | Inside HTML tag | Inside <style> tag | Separate .css file |
| Scope | Single element | Single page | Multiple pages |
| Reusability | No | No | Yes |
| Maintenance | Difficult | Moderate | Easy |
| Best Use | Quick fixes | Single page styling | Large 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.
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]
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]
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]
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]
Make Unit 5 stick
Practice BIT251 with flashcards & quizzes