Day-5 (CSS)
Day-5 (CSS)
1. Create a simple personal profile page using element and class selectors.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Personal Profile</title>
</head>
<body>
<header>
<h1>Jane Doe</h1>
</header>
<section class="profile">
<h2>About Me</h2>
</section>
<section class="contact">
<h2>Contact Information</h2>
<p>Email: [email protected]</p>
</section>
</body>
</html>
CSS
1
/* Element selector */
h1 {
font-size: 2em;
color: darkblue;
/* Class selector */
.profile {
background-color: lightgrey;
padding: 20px;
.contact {
background-color: lightyellow;
padding: 20px;
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<nav id="main-nav">
2
<ul class="nav-list">
</ul>
</nav>
</body>
</html>
CSS
/* ID selector */
#main-nav {
background-color: #333;
padding: 10px;
/* Class selectors */
.nav-list {
list-style-type: none;
margin: 0;
padding: 0;
display: flex;
justify-content: space-around;
.nav-item a {
color: white;
text-decoration: none;
3
padding: 10px 20px;
.nav-item a:hover {
background-color: #575757;
3. Create a simple blog post layout using element, class, ID, and compound selectors.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<article id="blog-post">
<header>
</header>
<section class="post-content">
<p>Welcome to my first blog post. I'm excited to share my journey in web development with
you.</p>
</section>
<footer class="post-footer">
</footer>
4
</article>
</body>
</html>
CSS
/* Element selector */
p{
font-size: 1em;
line-height: 1.5;
/* ID selector */
#blog-post {
max-width: 600px;
margin: auto;
padding: 20px;
/* Class selectors */
.post-title {
font-size: 2em;
color: #333;
.post-date {
color: grey;
5
.post-content {
margin-top: 20px;
.post-footer {
margin-top: 30px;
font-style: italic;
/* Compound selectors */
.post-title, .post-date {
text-align: center;
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Product Card</title>
</head>
<body>
6
<button class="buy-button">Buy Now</button>
</div>
</body>
</html>
CSS
/* Element selector */
button {
cursor: pointer;
/* ID selector */
#product-1 {
padding: 20px;
max-width: 300px;
margin: auto;
/* Class selectors */
.product-card {
text-align: center;
.product-image {
max-width: 100%;
height: auto;
7
}
.product-title {
font-size: 1.5em;
margin: 10px 0;
.product-description {
color: #666;
font-size: 1em;
.buy-button {
background-color: green;
color: white;
border: none;
border-radius: 5px;
/* Compound selectors */
.product-card:hover .buy-button {
background-color: darkgreen;
1. Uniqueness:
o Class: Can be applied to multiple elements.
o ID: Should be unique and applied to a single element.
2. Specificity:
8
oClass: Lower specificity, allowing it to be overridden by more specific selectors
like IDs or inline styles.
o ID: Higher specificity, making it more difficult to override.
3. Use Cases:
o Class: Ideal for applying common styles to multiple elements (e.g., styling
multiple buttons with the same appearance).
o ID: Ideal for targeting a specific element that requires unique styling (e.g., a
unique header or main content section).
5. HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
CSS
/* styles.css */
.box {
width: 100px;
9
height: 100px;
background-color: lightblue;
#special-box {
background-color: lightcoral;
1. padding: This is a shorthand property for setting all four paddings (top, right, bottom,
left) in one declaration.
2. padding-top: Sets the padding on the top of an element.
3. padding-right: Sets the padding on the right side of an element.
4. padding-bottom: Sets the padding on the bottom of an element.
5. padding-left: Sets the padding on the left side of an element.
.box {
.box2 {
padding: 10px 20px; /* Applies 10px padding to top and bottom, 20px to left and right */
10
.box3 {
padding: 10px 20px 30px; /* Applies 10px to top, 20px to left and right, 30px to bottom */
.box4 {
padding: 10px 20px 30px 40px; /* Applies 10px to top, 20px to right, 30px to bottom, 40px to
left */
Margin Properties:
1. margin: This is a shorthand property for setting all four margins (top, right, bottom, left)
in one declaration.
2. margin-top: Sets the margin on the top of an element.
3. margin-right: Sets the margin on the right side of an element.
4. margin-bottom: Sets the margin on the bottom of an element.
5. margin-left: Sets the margin on the left side of an element.
.box1 {
.box2 {
margin: 10px 20px; /* Applies 10px margin to top and bottom, 20px to left and right */
11
.box3 {
margin: 10px 20px 30px; /* Applies 10px to top, 20px to left and right, 30px to bottom */
.box4 {
margin: 10px 20px 30px 40px; /* Applies 10px to top, 20px to right, 30px to bottom, 40px to
left */
1. HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="box">
Content
</div>
</body>
</html>
CSS
body {
display: flex;
12
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
.box {
width: 200px;
height: 100px;
padding: 20px;
margin: 30px;
background-color: #ffeb3b;
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
13
<div class="box3">Box 3</div>
</body>
</html>
CSS
body {
display: flex;
justify-content: space-around;
align-items: center;
height: 100vh;
margin: 0;
background-color: #e0f7fa;
width: 150px;
height: 75px;
padding: 15px;
background-color: #4db6ac;
.box1 {
margin: 10px;
.box2 {
margin: 20px;
14
.box3 {
margin: 30px;
3. Nested Boxes
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Nested Boxes</title>
</head>
<body>
<div class="outer-box">
Outer Box
<div class="inner-box">
Inner Box
</div>
</div>
</body>
</html>
CSS
body {
display: flex;
justify-content: center;
15
align-items: center;
height: 100vh;
margin: 0;
background-color: #e1bee7;
.outer-box {
width: 300px;
height: 200px;
padding: 20px;
background-color: #ba68c8;
margin: 30px;
position: relative;
.inner-box {
width: 100px;
height: 50px;
padding: 10px;
background-color: #8e24aa;
position: absolute;
top: 50%;
left: 50%;
16
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Card Layout</title>
</head>
<body>
<div class="card">
<h2>Card Title</h2>
<p>This is some text inside the card. It demonstrates padding, borders, and margins.</p>
</div>
</body>
</html>
CSS
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #fff8e1;
.card {
width: 300px;
17
padding: 20px;
border-radius: 10px;
background-color: #fff3e0;
margin: 20px;
.card h2 {
margin-top: 0;
.card p {
margin-bottom: 0;
18