MadExp8 (A)
MadExp8 (A)
Lab Objective 04
To Design and Develop a responsive User Interface by applying PWA design technique
Theory:
Responsive web design is about creating web pages that look good on all devices
A responsive web design will automatically adjust for different screen sizes and viewports.
This will set the viewport of your page, which will give the browser instructions on how
to control the page's dimensions and scaling.Here is an example of a web page without
the viewport meta tag, and the same web page with the viewport meta tag:
15
DEPARTMENT OF INFORMATION TECHNOLOGY
Without the viewport meta tag: With the viewport meta tag:
Responsive Images
Responsive images are images that scale nicely to fit any browser size.
Using the width Property
If the CSS width property is set to 100%, the image will be responsive and scale up and
down:
Example: <img src="img_girl.jpg" style="width:100%;">
Using the max-width Property
If the max-width property is set to 100%, the image will scale down if it has to, but
never scale up to be larger than its original size:
Example: <img src="img_girl.jpg" style="max-width:100%;height:auto;">
Show Different Images Depending on Browser Width
The HTML <picture> element allows you to define different images for different browser
window sizes.
Resize the browser window to see how the images below changes depending on the width:
16
DEPARTMENT OF INFORMATION TECHNOLOGY
Example
<picture>
<source srcset="img_smallflower.jpg" media="(max-width: 600px)">
<source srcset="img_flowers.jpg" media="(max-width: 1500px)">
<source srcset="flowers.jpg">
<img src="img_smallflower.jpg" alt="Flowers">
</picture>
Media Queries:
In addition to resize text and images, it is also common to use media queries in responsive
web pages.With media queries you can define completely different styles for different
browser sizes. Example: resize the browser window to see that the three div elements
below will display horizontally on large screens and stacked vertically on small screens:
Example
/* Default layout for large screens */
.left, .right {
float: left;
width: 20%; /* 20% width by default */
}
.main {
float: left;
width: 60%; /* 60% width by default */
}
17
DEPARTMENT OF INFORMATION TECHNOLOGY
CODE:
HTML File: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Card UI Example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>My Responsive Website</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<main>
<div class="card-container">
<div class="card">
<img src="img.jpg" alt="Welcome Image">
<h2>Welcome to My Website</h2>
<p>This is a simple responsive layout using HTML and CSS.</p>
</div>
<div class="card">
<img src="img1.jpg" alt="About Us Image">
<h2>About Us</h2>
<p>We provide various services to help you succeed.</p>
</div>
<div class="card">
<img src="service.jpg" alt="Services Image">
<h2>Our Services</h2>
<p>Explore the range of services we offer to our clients.</p>
</div>
<div class="card">
<img src="contact.jpg" alt="Contact Image">
<h2>Contact Us</h2>
<p>Get in touch with us for more information.</p>
</div>
</div>
</main>
<footer>
<p>© 2023 My Responsive Website</p>
</footer>
</body>
</html>
18
DEPARTMENT OF INFORMATION TECHNOLOGY
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
header {
background: #4CAF50;
color: white;
padding: 10px 20px;
text-align: center;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
display: inline;
margin: 0 15px;
}
nav ul li a {
color: white;
text-decoration: none;
}
.card-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding: 20px;
}
.card {
background: #f4f4f4;
border-radius: 5px;
margin: 10px;
padding: 15px;
width: 300px; /* Fixed width for cards */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
text-align: center;
}
19
DEPARTMENT OF INFORMATION TECHNOLOGY
.card img {
max-width: 100%; /* Make images responsive */
height: auto; /* Maintain aspect ratio */
border-radius: 10px; /* Set border radius to 10px for images */
}
footer {
text-align: center;
padding: 10px 0;
background: #333;
color: white;
}
/* Responsive Styles */
@media (max-width: 600px) {
nav ul li {
display: block;
margin: 10px 0;
}
.card-container {
flex-direction: column; /* Stack cards vertically on small screens */
align-items: center; /* Center cards */
}
}
Output Screenshot:
1) LAPTOP VIEW:
20
DEPARTMENT OF INFORMATION TECHNOLOGY
2) MOBILE VIEW:
Conclusion:
A responsive web page look good on large desktop screens and on small mobile
phones.
21