Responsive-Web-Design
Responsive-Web-Design
WEB DESIGN
Responsive Web Design
.container {
width: 100%; /* Container adjusts to screen size */
}
It will adjust to the available space for all screens, but you should
consider additional properties like media queries and max-width
to optimize the layout for different screen sizes.
Why is Responsive Web Design
Important?
Improved User Experience:
• Users are accessing websites from different devices, and
responsive design ensures that they can easily navigate and
interact with content.
• Better readability and easier navigation without the need to
zoom in or scroll horizontally.
SEO Benefits:
• Google prioritizes mobile-friendly websites in its search rankings,
meaning responsive sites tend to perform better in search results.
Reduced Maintenance:
• Rather than maintaining separate websites for mobile and desktop,
a single responsive site caters to all device sizes.
Media Queries
body {
color: black;
background-color: grey;
The background color in this
} illustration is set to grey. However,
@media print { the background color should be
body { clear if the page is printed. User
printer ink is preserved in this way.
background-color:
transparent;
}
}
Example
body {
color: black;
background-color:
white; If you don't specify any media type
} for your CSS, it will automatically
have a media type value of
all. These two blocks of CSS are
@media all { equivalent.
body {
color: black;
background-color:
white;
}
}
Query Conditions
■ You can add conditions to media types. These are called media
queries. The CSS is applied only if the media type matches and
the condition is also true. These conditions are called media
features.
■ This is the syntax for media queries:
img {
max-width: 100%;
height: auto; /* Maintains aspect ratio */
}
The max-width: 100% ensures that the image scales down with the
container while maintaining its aspect ratio.
<picture> element
<picture>
<source media="(max-width: 600px)" srcset="small.jpg">
<source media="(max-width: 1200px)" srcset="medium.jpg">
<img src="large.jpg" alt="Responsive image">
</picture>
This example serves a smaller image (small.jpg) for screens that are
600px wide or less and a medium-sized image (medium.jpg) for screens
between 600px and 1200px.
Responsive Video Example
<style>
.video-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /*
<div class="video-container">
Aspect ratio 16:9 (56.25% = 9/16 *
<iframe
100) */
src="https://www.youtube.com/em
}
bed/dQw4w9WgXcQ"
frameborder="0"
.video-container iframe {
allowfullscreen></iframe>
position: absolute;
</div>
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
</style>
Practice Questions