Module 2
Module 2
Primary Defines the structure and Defines the presentation and style of
Purpose content of a web page. a web page.
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* It's great for beginners, but for larger sites, we put this in a separate file. */
body {
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
.form-box {
background-color: white;
padding: 30px;
width: 100%;
max-width: 600px;
}
h1 {
color: #d11212;
text-align: center;
margin-bottom: 20px;
/* Styling for the table to align the form labels and inputs */
table {
width: 100%;
td {
padding: 8px 0;
input[type="text"],
input[type="email"],
input[type="tel"],
select,
textarea {
width: 100%;
padding: 8px;
box-sizing: border-box;
border-radius: 4px;
/* Specific styles for the "Upload Photo" file input to hide the default browser look */
.file-input-wrapper {
display: flex;
align-items: center;
.file-input-button {
background-color: #e0e0e0;
border-radius: 4px;
cursor: pointer;
.file-input-display {
margin-left: 10px;
color: #555;
.button-group {
text-align: center;
padding-top: 20px;
button {
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
}
button[type="submit"] {
background-color: #333;
color: white;
margin-right: 10px;
button[type="reset"] {
background-color: #e0e0e0;
color: #333;
</style>
</head>
<body>
<div class="form-box">
<!-- Using a table to align labels and input fields as seen in the image -->
<table>
<tr>
<td></td>
<td>
<label for="mr">Mr.</label>
<input type="radio" id="mrs" name="salutation">
<label for="mrs">Mrs.</label>
<label for="ms">Ms.</label>
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td><label for="city">City</label></td>
</tr>
<tr>
<td><label for="state">State</label></td>
<td>
<option>Maharashtra</option>
<option>Karnataka</option>
<option>Delhi</option>
</select>
</td>
</tr>
<tr>
<td><label for="zip">Zip</label></td>
</tr>
<tr>
</tr>
<tr>
<td><label for="email">E-Mail</label></td>
</tr>
<tr>
<td><label for="mobile">Mobile</label></td>
<td>
</td>
</tr>
<tr>
<td><label>Languages known</label></td>
<td>
<label for="hindi">Hindi</label><br>
<label for="english">English</label><br>
<label for="marathi">Marathi</label>
</td>
</tr>
<tr>
</tr>
</table>
<div class="button-group">
<button type="submit">submit</button>
<button type="reset">reset</button>
</div>
</form>
</div>
</body>
</html>
Design an HTML table to show a monthly expense tracker, with rows for
categories (food, rent, travel, etc.) and columns for each week of the month.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<center>
<thead>
<tr>
<th>Category</th>
<th>Week 1</th>
<th>Week 2</th>
<th>Week 3</th>
<th>Week 4</th>
<th>Monthly Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>Food</td>
<td>$150.00</td>
<td>$120.00</td>
<td>$160.00</td>
<td>$135.00</td>
<td>$565.00</td>
</tr>
<tr>
<td>Rent</td>
<td>$0.00</td>
<td>$0.00</td>
<td>$0.00</td>
<td>$1,000.00</td>
<td>$1,000.00</td>
</tr>
<tr>
<td>Travel</td>
<td>$50.00</td>
<td>$75.00</td>
<td>$20.00</td>
<td>$90.00</td>
<td>$235.00</td>
</tr>
<tr>
<td>Entertainment</td>
<td>$30.00</td>
<td>$45.00</td>
<td>$15.00</td>
<td>$60.00</td>
<td>$150.00</td>
</tr>
<tr>
<td>Utilities</td>
<td>$0.00</td>
<td>$85.00</td>
<td>$0.00</td>
<td>$0.00</td>
<td>$85.00</td>
</tr>
<tr>
<td>Other</td>
<td>$25.00</td>
<td>$10.00</td>
<td>$5.00</td>
<td>$30.00</td>
<td>$70.00</td>
</tr>
<tr>
<td><b>Total</b></td>
<td><b>$255.00</b></td>
<td><b>$330.00</b></td>
<td><b>$200.00</b></td>
<td><b>$1,315.00</b></td>
<td><b>$2,100.00</b></td>
</tr>
</tbody>
</table>
</center>
</body>
</html>
Create a table in HTML that uses: a. rowspan to merge two rows. b. colspan
to merge two columns.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.tailwindcss.com"></script>
<style>
table, th, td {
table {
width: 100%;
</style>
</head>
<tr>
</th>
<th scope="col" class="px-6 py-3">
Status
</th>
</tr>
</thead>
<tbody>
Q1
</td>
Revenue
</td>
$500,000
</td>
</tr>
Expenses
</td>
$200,000
</td>
</tr>
Q2
</td>
</td>
$650,000
</td>
</tr>
</tbody>
<tfoot>
Total
</td>
$1,150,000
</td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
Write HTML code to create a webpage with two sections: one showing an
image gallery and the other playing a video.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<!-- This is the first section for the image gallery -->
<div>
<h1>Image Gallery</h1>
<!-- This div holds all the images for the gallery -->
<div>
</div>
</div>
<hr>
<!-- This is the second section for the video player -->
<div>
<h2>Video Section</h2>
<div>
<video controls>
<!-- This text shows up if the browser can't play the video -->
</video>
</div>
</div>
</body>
</html>
Create a personal website with Formatting, Colors, Images, Lists, Links and
Tables HTML tags.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--
-->
<style>
/*
We are applying styles directly to the body and other HTML tags.
This makes the code shorter and easier to understand for beginners.
*/
body {
line-height: 1.6;
margin: 20px;
padding: 20px;
background-color: #f4f4f4;
color: #333;
max-width: 960px;
background-color: #fff;
border-radius: 8px;
h1, h2 {
color: #0056b3;
img {
max-width: 100%;
height: auto;
display: block;
margin-bottom: 20px;
border-radius: 8px;
a{
color: #007bff;
text-decoration: none;
a:hover {
text-decoration: underline;
ul, ol {
margin-bottom: 20px;
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
th, td {
padding: 8px;
text-align: left;
th {
background-color: #0056b3;
color: white;
</style>
</head>
<body>
<p>
Hello! My name is John Doe, and this is a simple website to showcase my skills and interests.
</p>
<hr>
<h2>My Photo</h2>
<hr>
<h2>About Me</h2>
<p>
</p>
<hr>
<ul>
</ul>
<ol>
</ol>
<hr>
<h2>Important Links</h2>
<ul>
</ul>
<hr>
<h2>My Skills</h2>
<p>This table summarizes my technical skills:</p>
<table>
<thead>
<tr>
<th>Skill</th>
<th>Level</th>
</tr>
</thead>
<tbody>
<tr>
<td>HTML5</td>
<td>Advanced</td>
</tr>
<tr>
<td>CSS3</td>
<td>Intermediate</td>
</tr>
<tr>
<td>JavaScript</td>
<td>Beginner</td>
</tr>
</tbody>
</table>
</body>
</html>
Explain how the element works. What kind of applications use it?
That's an excellent question! I can explain how the selected code uses CSS to add formatting to the
webpage.
The code within the <style> tags in the <head> of the HTML document is called internal CSS. It's used
to define rules for how the different elements on the page should look. Instead of applying styles to
each element individually, you write rules that apply to all instances of a specific HTML tag.
• body: This rule applies to the entire webpage. It sets the font style (font-family), the space
between lines of text (line-height), and the background color (background-color: #f4f4f4). It
also centers the content and adds a white background with a shadow, creating a nice, clean
box for your content to sit in.
• h1, h2: This rule targets all h1 and h2 headings at once. It changes their color to a shade of
blue (#0056b3), making them stand out.
• img: This rule applies to all images on the page. It ensures the image is responsive (max-
width: 100%) so it won't be larger than its container, and adds a small space below it
(margin-bottom) and rounded corners (border-radius).
• a: This rule changes the color of all links (<a>) to a different shade of blue (#007bff) and
removes the default underline (text-decoration: none). The next rule, a:hover, adds the
underline back only when you hover your mouse over the link, which is a common visual cue
for interactivity.
• table, th, td: These rules work together to style the table. The table rule makes the table take
up the full width (width: 100%) and collapses the borders. The th (table header) and td (table
data) rules add a border, padding, and text alignment. The th rule also gives the table
headers a bold background color and white text.
Using this method, you can easily change the look of your entire website by simply editing the CSS
rules in one place, which is much more efficient than changing styles on every single HTML tag.
Create a multimedia web page showcasing a cultural event using images,
videos, and formatted content with appropriate layout.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<hr>
<hr>
</video>
<hr>
<p>
The Harvest Festival is a very old tradition that celebrates our community and its history.
</p>
<blockquote>
"Culture is the way we live, the way we celebrate, and the way we connect with our past."
</blockquote>
<p>
We celebrate with a big parade, local markets, music, and lots of food. It's a fun day for
everyone!
</p>
</body>
</html>
This is the most basic selector. It targets HTML elements directly by their tag name.
• How it works: You just write the name of the HTML tag.
CSS
p{
color: red;
2. Class Selector
This selector is used to select all HTML elements with a specific class attribute. Classes are super
useful because you can apply the same style to multiple elements, even if they're different types of
tags.
• How it works: You start with a period (.) followed by the class name.
• Example: Let's say you want to style a specific group of photos. You'd give them a class like
gallery-image:
HTML
Then, you can style them all at once with this CSS:
CSS
.gallery-image {
3. ID Selector
The ID selector is used to target a single, unique element. An ID should only be used once on a
webpage.
HTML
CSS
#main-title {
font-size: 48px;
4. Universal Selector
The universal selector does exactly what it sounds like: it selects every single element on the page.
• Example: If you want to change the font for everything on your page, you can use this:
CSS
*{
font-family: sans-serif;
5. Attribute Selector
This selector targets elements based on the presence of a specific attribute or the value of that
attribute.
• How it works: You put the attribute and its value inside square brackets ([]).
• Example: To select all links that open in a new tab, you would use this:
CSS
a[target="_blank"] {
color: green;
This will select any <a> tag that has a target attribute with the value _blank.
Classify the three ways to create CSS with an example and output. 11. Define
the CSS Box Model. What are its four components? Show how
The three main ways to add CSS to an HTML document are inline, internal, and external. Each
method has a different use case, and they can all be used in the same project.
1. Inline CSS
Inline CSS is used to apply a style to a single HTML element. You add the style attribute directly to the
tag you want to modify. This method is the fastest way to apply a style, but it's not very flexible
because you have to add the style to every single element you want to change. It's often used for
quick fixes or when a style is truly unique to one element.
Example:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inline CSS</title>
</head>
<body>
</body>
</html>
Output:
• A heading with the text "This is a blue heading." that is blue and has a font size of 24 pixels.
2. Internal CSS
Internal CSS is defined in a <style> tag within the <head> section of an HTML document. This is useful
for styling a single HTML page when you don't need to link to an external stylesheet. You can apply
styles to multiple elements at once using selectors.
Example:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Internal CSS</title>
<style>
h1 {
color: purple;
p{
font-style: italic;
</style>
</head>
<body>
</body>
</html>
Output:
3. External CSS
External CSS is the most common and recommended method for styling websites. The styles are
written in a separate file with a .css extension, and you link to it from your HTML document using a
<link> tag in the <head> section. This is great for managing styles across a large website, as you can
change the look of your entire site by editing a single file.
Example:
index.html
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External CSS</title>
</head>
<body>
</body>
</html>
styles.css
CSS
h1 {
color: red;
p{
Output:
• A paragraph with the text "This paragraph has a border." that has a black border around it.
Define the CSS Box Model. What are its four components? Show how total
width and height of an element is calculated with a sample CSS rule.
The CSS Box Model is a fundamental concept that describes how all HTML elements are rendered as
rectangular boxes on a webpage. Every element, regardless of its content (text, image, etc.), is
treated as a box with four distinct parts. When you apply styles, you're essentially changing the
dimensions of these four parts.
1. Content: This is the innermost part of the box. It holds the actual content of the element,
such as text, images, or a video player. Its dimensions are determined by the width and
height CSS properties.
2. Padding: This is the transparent space directly surrounding the content. It acts as a buffer
between the content and the border. You can set the padding on all sides or individually
(e.g., padding-top, padding-right).
3. Border: This is a line that goes around the padding and the content. You can style the border
with different colors, widths, and styles (e.g., solid, dashed).
4. Margin: This is the transparent space on the outside of the border. It creates a gap between
the element and other elements on the page, helping to prevent them from touching.
The total space an element occupies on a page is the sum of its content, padding, border, and
margin. This is a common source of confusion for beginners because the width and height properties
only affect the content area.
CSS
.box {
width: 100px;
height: 50px;
padding: 10px;
• Total Width: 100px (width) + 10px (left padding) + 10px (right padding) + 5px (left border) +
5px (right border) + 20px (left margin) + 20px (right margin) = 170px
• Total Height: 50px (height) + 10px (top padding) + 10px (bottom padding) + 5px (top border)
+ 5px (bottom border) + 20px (top margin) + 20px (bottom margin) = 120px
So, even though the content box is defined as 100px by 50px, the entire element will take up a space
of 170px by 120px on the webpage.
Differentiate between content-box and border-box models in CSS. Create a
styled HTML layout to illustrate the difference.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
margin: 20px;
.box {
width: 200px;
padding: 25px;
margin: 20px;
background-color: #eee;
.content-box {
box-sizing: content-box;
.border-box {
box-sizing: border-box;
</style>
</head>
<body>
<h1>CSS Box Model</h1>
<h2>Content-Box</h2>
</div>
<hr>
<h2>Border-Box</h2>
</div>
</body>
</html>
Scenario: A product page displays items in a 3-column grid. Each item has a
title, image, and price, enclosed in a box with shadows.
Question: Write external CSS rules for the class .product-card to:
a. Set width to 30%
b. Add margin between items
c. Add box-shadow
d. Align text to the center
/* This is a simple CSS file for a product page. */
.product-card {
width: 30%;
margin: 15px;
text-align: center;