home
0%
Progress
StudentPortal › Class XI - IT › Unit 3
CBSE Code 802 | Unit 3: Web Design & Internet Technologies

02. CSS Styling & Layouts

CSS selectors, properties, box model, colors, fonts, backgrounds, inline vs internal vs external CSS.

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:

  1. Element / Type Selector: Selects all elements of that tag name. p { color: gray; }
  2. Class Selector (.): Matches elements having a specific class name. .highlight { background-color: yellow; }
  3. ID Selector (#): Targets a unique element with a specific ID. #navbar { background-color: #1e293b; }
  4. Universal Selector (*): Matches every element on the page. * { box-sizing: border-box; margin: 0; }
  5. Grouping Selector (,): Applies the same styles to multiple selectors. h1, h2, h3 { font-family: 'Segoe UI', sans-serif; }
  6. 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:

  1. Content: The innermost area where text and images appear. Dimensions set by width and height.
  2. Padding: Transparent spacing between the content and the border. Set using padding: 10px; (or padding-top, padding-right, padding-bottom, padding-left).
  3. Border: Decorative or solid line wrapped around the padding. Set using border: 1px solid #ccc;.
  4. 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 respects width, height, and margin.
  • 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;
}

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.