Assignments Of Class 23: Enhancing HTML with CSS
Assignment 1: Styling a Simple Box Using the Box Model
Objective:
Create a simple box and apply the CSS box model to control its padding, border, and margin.
Steps:
1. Create an HTML file with a div
element.
2. Apply CSS to style the div
using the box model.
3. Set a fixed width and height, add padding, a border, and margin.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Box Model Example
</title>
<style>
.box {
width: 200px; /* Content width */
height: 150px; /* Content height */
padding: 20px; /* Padding around content */
border: 5px solid black; /* Border around the box */
margin: 30px; /* Margin around the box */
background-color: lightblue;
}
</style>
</head>
<body>
<div class="box">This is a box.
</div>
</body>
</html>
Explanation:
- The
div
element has a width of200px
and height of150px
for the content area. - Padding of
20px
is applied inside the box, affecting the space around the content. - A border of
5px
solid black surrounds the padding. - A margin of
30px
creates space outside the box. - The background color is set to light blue.
Assignment 2: Center a Box Using the Box Model
Objective:
Center a box in the middle of the page using margins and padding.
Steps:
1. Create a div
container and apply a background color.
2. Use the box model to center the box horizontally and vertically.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Box
</title>
<style>
body {
height: 100vh; /* Full viewport height */
display: flex; /* Enable flexbox */
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
margin: 0; /* Remove default margin */
}
.box {
width: 300px;
height: 200px;
background-color: coral;
padding: 20px;
border: 5px solid black;
text-align: center;
line-height: 200px;
}
</style>
</head>
<body>
<div class="box">Centered Box
</div>
</body>
</html>
Explanation:
- The
body
uses flexbox (display: flex
) to center the.box
both horizontally (justify-content: center
) and vertically (align-items: center
). - The
.box
has a fixed width of300px
and height of200px
, with padding of20px
and a border of5px
. - The text inside the box is centered using
text-align
andline-height
.
Assignment 3: Position a Box Relative to Its Normal Position
Objective:
Use relative positioning to move an element from its original position.
Steps:
1. Create a div
with some content.
2. Apply relative positioning and move it 50px from the top and 100px from the left.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Relative Positioning
</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightgreen;
padding: 20px;
position: relative;
top: 50px;
left: 100px;
}
</style>
</head>
<body>
<div class="box">Relative Positioning
</div>
</body>
</html>
Explanation:
- The
.box
is relatively positioned, meaning its position is adjusted from its normal position without affecting other elements. - The
top: 50px
andleft: 100px
properties move the box 50px down and 100px to the right.
Assignment 4: Position a Box Absolutely
Objective:
Position a div
absolutely within its parent container.
Steps:
1. Create a parent container with a div
inside it.
2. Apply absolute positioning to the child div
to place it within the parent container.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Absolute Positioning
</title>
<style>
.container {
position: relative;
width: 400px;
height: 300px;
background-color: lightgray;
}
.box {
position: absolute;
top: 50px;
left: 100px;
width: 200px;
height: 100px;
background-color: lightcoral;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Absolutely Positioned
</div>
</div>
</body>
</html>
Explanation:
- The
.container
is set toposition: relative
, which makes it the reference point for the absolutely positioned.box
. - The
.box
is positioned absolutely, meaning it is placed 50px from the top and 100px from the left of the.container
.
Assignment 5: Fixed Positioning on Scroll
Objective:
Make an element stay fixed in the viewport as the user scrolls.
Steps:
1. Create a div
and apply fixed positioning.
2. Observe how the element stays fixed even when the page scrolls.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fixed Positioning
</title>
<style>
body {
height: 2000px; /* Create a scrollable page */
margin: 0;
}
.fixed-box {
position: fixed;
top: 10px;
right: 10px;
width: 200px;
height: 100px;
background-color: lightblue;
padding: 10px;
border: 2px solid black;
}
</style>
</head>
<body>
<div class="fixed-box">I stay fixed!
</div>
</body>
</html>
Explanation:
- The
.fixed-box
stays fixed in the viewport even as the user scrolls. It is positioned 10px from the top and 10px from the right of the viewport. - The
height: 2000px
in thebody
creates a long page to demonstrate scrolling.
Assignment 6: Sticky Header
Objective:
Create a sticky header that remains visible when scrolling.
Steps:
1. Create a header
and apply sticky positioning.
2. Add content to the page to enable scrolling.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sticky Header
</title>
<style>
body {
margin: 0;
padding: 0;
}
header {
position: sticky;
top: 0;
background-color: #333;
color: white;
padding: 10px;
text-align: center;
font-size: 24px;
}
.content {
height: 2000px; /* Add content to create scrolling */
}
</style>
</head>
<body>
<header>Sticky Header
</header>
<div class="content">Scroll to see the sticky header in action!
</div>
</body>
</html>
Explanation:
- The
header
element is positioned withposition: sticky
and stays at the top (top: 0
) as you scroll down the page. - The content area is tall enough to make the page scrollable, so the sticky header remains visible at the top.
Assignment 7: Flexbox Layout
Objective:
Create a layout with three boxes using flexbox.
Steps:
1. Create a parent div
with three child div
elements.
2. Apply flexbox to the parent div
to arrange the child boxes.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Layout
</title>
<style>
.container {
display: flex;
justify-content: space-around;
background-color: lightgray;
padding: 20px;
}
.box {
width: 100px;
height: 100px;
background-color: coral;
text-align: center;
line-height: 100px;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box 1
</div>
<div class="box">Box 2
</div>
<div class="box">Box 3
</div>
</div>
</body>
</html>
Explanation:
- The
.container
usesdisplay: flex
to enable flexbox layout. - The child
.box
elements are arranged in a row with space around them usingjustify-content: space-around
.
Assignment 8: Grid Layout
Objective:
Create a simple grid layout with three columns.
Steps:
1. Use CSS Grid to create a layout with three columns.
2. Place content inside each grid item.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid Layout
</title>
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
gap: 20px;
padding: 20px;
}
.box {
height: 100px;
background-color: lightblue;
text-align: center;
line-height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Column 1
</div>
<div class="box">Column 2
</div>
<div class="box">Column 3
</div>
</div>
</body>
</html>
Explanation:
- The
.container
uses CSS Grid with three equal columns (grid-template-columns: repeat(3, 1fr)
). - Each
.box
is placed inside a grid cell.
Assignment 9: Box Shadow Effect
Objective:
Apply a box shadow effect to a div
element using the box model.
Steps:
1. Create a div
and apply the box shadow effect.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Box Shadow
</title>
<style>
.box {
width: 300px;
height: 200px;
background-color: lightyellow;
margin: 50px auto;
box-shadow: 10px 10px 15px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div class="box">Box with Shadow
</div>
</body>
</html>
Explanation:
- The
.box
has abox-shadow
with an offset of10px
horizontally and10px
vertically. - The shadow has a blur radius of
15px
and a light opacity (rgba(0, 0, 0, 0.2)
).
Assignment 10: Responsive Design with Box Model
Objective:
Create a responsive layout where the width of the elements adjusts according to the screen size.
Steps:
1. Create a container with a few child div
elements.
2. Use the box model and media queries to make the design responsive.
Solution:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Box Model
</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
padding: 20px;
}
.box {
flex: 1;
background-color: lightgreen;
padding: 20px;
text-align: center;
}
@media (max-width: 600px) {
.box {
flex-basis: 100%; /* Make each box take full width on small screens */
}
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box 1
</div>
<div class="box">Box 2
</div>
<div class="box">Box 3
</div>
</div>
</body>
</html>
Explanation:
- The
.container
usesdisplay: flex
withflex-wrap: wrap
to allow the items to wrap on smaller screens. - Each
.box
takes equal width usingflex: 1
, but on screens smaller than600px
, theflex-basis
changes to100%
, making each box take up the full width.
These assignments will help students get hands-on experience with CSS box model properties, positioning techniques, flexbox, grid layouts, and responsive design. Each assignment builds on the previous one, offering step-by-step solutions and detailed explanations to reinforce understanding.