CSS3 Media Queries Examples
In this section, we will demonstrate some media query examples for you.
Before we start, let's create a list of email links. The HTML code is as follows:
Example 1
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
}
</style>
</head>
<body>
<ul>
<li><a data-email="johndoe@example.com" href="mailto:johndoe@example.com">John Doe</a></li>
<li><a data-email="marymoe@example.com" href="mailto:marymoe@example.com">Mary Moe</a></li>
<li><a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com">Amanda Panda</a></li>
</ul>
</body>
</html>
Note the data-email
attribute. In HTML, we can use attributes prefixed with data-
to store information.
520 to 699px Width - Add Email Icon
When the browser width is between 520 and 699px, add an email icon before the email link:
Example 2
@media screen and (max-width: 699px) and (min-width: 520px) {
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
700 to 1000px Width - Add Text Prefix Information
When the browser width is between 700 and 1000px, add "Email: " before the email link:
Example 3
@media screen and (max-width: 1000px) and (min-width: 700px) {
ul li a:before {
content: "Email: ";
font-style: italic;
color: #666666;
}
}
Greater than 1001px Width - Add Email Address
When the browser width is greater than 1001px, add the email address after the link.
We will use the data-
attribute to add the email address after each name:
Example 4
@media screen and (min-width: 1001px) {
ul li a:after {
content: " (" attr(data-email) ")";
font-size: 12px;
font-style: italic;
color: #666666;
}
}
Greater than 1151px Width - Add Icon
When the browser width is greater than 1001px, add an icon before the name.
In this example, we do not write additional query blocks; we can add other media queries separated by commas after the existing query (similar to the OR operator):
Example 5
@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;
}
}