Unit 3: Web Design & Internet Technologies
Chapter 2: CSS Styling and Layouts
1. Introduction to CSS
Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation, typography, visual colors, and spatial layout of documents written in HTML.
HTML (Structure) + CSS (Style & Layout) = Beautiful Web Page
[Raw headings, links] [Colors, margins, grids] [Polished UI]
Three Ways to Apply CSS:
<!-- 1. Inline CSS (Directly on HTML elements via style attribute) -->
<h1 style="color: blue; text-align: center;">Welcome</h1>
<!-- 2. Internal / Embedded CSS (Inside <style> tag in <head>) -->
<head>
<style>
p { color: #333333; line-height: 1.6; }
</style>
</head>
<!-- 3. External CSS (Recommended: linked standalone .css stylesheet) -->
<head>
<link rel="stylesheet" href="styles.css">
</head>
2. CSS Syntax and Selectors
A CSS rule comprises a Selector pointing to the HTML element and a Declaration Block containing property-value pairs:
Selector Declaration Block
h1 { color: navy; font-size: 24px; }
\_________/ \_____________/
Property: Property:
Value Value
Types of CSS Selectors:
- Element / Type Selector: Selects all elements of that tag name.
p { color: gray; } - Class Selector (
.): Matches elements having a specific class name..highlight { background-color: yellow; } - ID Selector (
#): Targets a unique element with a specific ID.#navbar { background-color: #1e293b; } - Universal Selector (
*): Matches every element on the page.* { box-sizing: border-box; margin: 0; } - Grouping Selector (
,): Applies the same styles to multiple selectors.h1, h2, h3 { font-family: 'Segoe UI', sans-serif; } - Descendant Selector (space): Targets elements nested inside another element.
nav a { color: white; text-decoration: none; }
3. The CSS Box Model
Every HTML element rendered on a web page is treated by the browser as a rectangular box:
+-------------------------------------------------------------+
| MARGIN |
| Transparent space outside the border |
| +-----------------------------------------------------+ |
| | BORDER | |
| | Visible line surrounding padding and content | |
| | +---------------------------------------------+ | |
| | | PADDING | | |
| | | Transparent clear area inside border | | |
| | | +-------------------------------------+ | | |
| | | | CONTENT | | | |
| | | | Actual text, image, or media | | | |
| | | +-------------------------------------+ | | |
| | +---------------------------------------------+ | |
| +-----------------------------------------------------+ |
+-------------------------------------------------------------+
Components of the Box Model:
- Content: The innermost area where text and images appear. Dimensions set by
widthandheight. - Padding: Transparent spacing between the content and the border. Set using
padding: 10px;(orpadding-top,padding-right,padding-bottom,padding-left). - Border: Decorative or solid line wrapped around the padding. Set using
border: 1px solid #ccc;. - Margin: Clears space outside the border, separating the element from neighboring elements. Set using
margin: 15px auto;.
Best Practice: Use
box-sizing: border-box;so that padding and border widths are included in the element's total width and height calculation.
4. Typography, Colors, and Backgrounds
- Color Values:
- Named colors:
red,blue,crimson - Hexadecimal:
#ffffff(white),#0f172a(navy) - RGB / RGBA:
rgb(34, 197, 94)/rgba(0, 0, 0, 0.5)(with alpha transparency) - Typography:
font-family: Comma-separated fallback font stack (e.g.,font-family: Arial, Helvetica, sans-serif;).font-size:16px,1.2rem,120%.font-weight:normal,bold,600,700.text-align:left,center,right,justify.
5. Modern Layouts: Display and Flexbox Basics
display: block;: Takes full width of parent container, forces a new line (e.g.,<div>,<p>,<h1>).display: inline;: Takes only necessary width, does not force a new line; ignores top/bottom margins (e.g.,<span>,<a>).display: inline-block;: Flows inline like text, but respectswidth,height, andmargin.display: none;: Removes the element completely from page layout.
Flexbox Basics (display: flex;)
A one-dimensional layout model for arranging items in rows or columns:
.card-container {
display: flex;
flex-direction: row; /* Align horizontally */
justify-content: center; /* Horizontally centers items */
align-items: center; /* Vertically centers items */
gap: 20px; /* 20px space between children */
}
6. Board Exam Practice Questions & Solutions
Q1. Explain the CSS Box Model with the help of a labeled diagram.
Answer:
The CSS Box Model defines the rectangular layout structure wrapped around every HTML element:
1. Content: The innermost zone where text, images, or media are rendered.
2. Padding: The transparent clearing space immediately surrounding the content inside the border.
3. Border: The boundary line wrapping around the padding.
4. Margin: The outermost clear area separating the element from surrounding HTML elements.
Q2. Differentiate between Class selector and ID selector in CSS.
Answer:
- A Class Selector (prefixed with a dot, e.g., .card) can be applied to multiple HTML elements on the same page to share a uniform style.
- An ID Selector (prefixed with a hash, e.g., #main-header) must be unique to a single element per HTML page and has higher styling specificity.
Q3. Write CSS code to create a button with a blue background, white text, 10px padding, rounded corners, and no border.
Answer:
.custom-btn {
background-color: #2563eb;
color: #ffffff;
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 16px;
cursor: pointer;
}