0
5.6kviews
Explain in detail Media Queries with an example.
2 Answers
written 8.2 years ago by |
A media query consists of a media type and zero or more expressions that check for the conditions of particular media features. Among the media features that can be used in media queries are 'width', 'height', and 'color'. By using media queries, presentations can be tailored to a specific range of output devices without changing the content itself.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
}
@media screen and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) {
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
@media screen and (max-width: 1000px) and (min-width: 700px) {
ul li a:before {
content: "Email: ";
font-style: italic;
color: #666666;
}
}
@media screen and (min-width: 1001px) {
ul li a:after {
content: " (" attr(data-email) ")";
font-size: 12px;
font-style: italic;
color: #666666;
}
}
</style>
</head>
<body>
<h1>Resize the browser window to see the effect!</h1>
<ul>
<li><a data-email="[email protected]" href="mailto:[email protected]">John Doe</a></li>
<li><a data-email="[email protected]" href="mailto:[email protected]">Mary Moe</a></li>
<li><a data-email="[email protected]" href="mailto:[email protected]">Amanda Panda</a></li>
</ul>
</body>
</html>
Output:
written 6.7 years ago by |
Media queries in CSS3
@media not|only mediatype and (expressions) { CSS-Code; }
< !DOCTYPE html>
< html>
< head> <meta name="viewport" content="width=device-width, initial-scale=1.0">
< style>
body { background-color: lightgreen; }
@media only screen and (max-width: 500px)
{ body {
background-color: lightblue; }
}
< /style>
< /head>
< /html>