Responsive Web Design Using Media Queries
Responsive Web Design Using Media Queries
DESIGN:
Responsive Web Design is about using HTML
and CSS to automatically resize, hide, shrink,
or enlarge, a website, to make it look good on
all devices (desktops, tablets, and phones).
It is about creating web sites which
automatically adjust themselves to look good
on all devices, from small phones to large
desktops.
A responsive web design will automatically
adjust for different screen sizes and viewports.
Responsive Design need to include following things:
1. meta
<meta name="view-port" content="width=devicewidth,initial-scale=1.0">
2. fluid images : image width and height in %
3. display: grid
4. flex box : flex-wrap, flex-direction
5. columns
6. @media queries
VIEW PORT
The viewport is the user's visible area of a web page.
It is the part of the document we are viewing.
The viewport varies with the device and will be smaller on a mobile phone than on a computer screen.
To create a responsive website, add <meta> tag to all our web pages.
Media properties use “viewport” to control the appearance of content on screen.
Setting The Viewport:
HTML5 introduced a method to let web designers take control over the viewport, through the <meta> tag.
Syntax:
}
o and: The and keyword combines a media feature with a media type or other media features.
Some Media Features:
o max-width: The maximum width of the display area, such as a browser
window.
o min-width: The minimum width of the display area, such as a browser
window.
o orientation: The orientation of the viewport (landscape or portrait mode)
o width: The viewport width.
<!DOCTYPE html>
<html>
<head>
<title>Media</title>
<style>
@media screen and (min-width:400px) {
body {
background-color: yellow;
}
}
</style>
</head>
<body>
<h2>Responsive Design</h2>
<p>Media queries in CSS</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Media</title>
<style>
body {
background-color: yellow;
body {
background-color: lightcyan;
</style>
</head>
<body>
<h2>Responsive Design</h2>
</body>
Media Queries with multiple breakpoints:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body
{
background-color: orange;
}
@media screen and (max-width:1000px) //breakpoint 1
{
body
{
background-color: red;
}
}
@media screen and (max-width:600px) //breakpoint 2
{
body
{
background-color: cyan;
}
}
</style>
</head>
<body>
<h1>This is a media query demo</h1>
</body>
</html>
Media Queries:
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
body
{
background-color: blue;
}
h1
{
color: white;
font-size:5vw;
}
@media screen and (max-width:700px)
{
body
{
background-color: pink;
}
h1
{
color: blue;
font-size:4vw;
}
}
</style>
</head>
<body>
<h1>This is a media query demo</h1>
</body>
</html>