CSS3 Media Query Examples

In this chapter, we will demonstrate some multimedia query examples.

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>

Try it Yourself »

Notedata-emailAttribute. In HTML, we can use withdata-Use prefixed attributes 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;
    }
}

Try it Yourself »

700 to 1000px - 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;
    }
}

Try it Yourself »

Greater than 1001px width - add email address

When the browser width is greater than 1001px, add the email address after the link.

We will usedata-attribute to add an email address after each person's 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;
    }
}

Try it Yourself »

Greater than 1151px width - add icon

When the browser width is greater than 1001px, an icon will be added before the person's name.

In the example, we did not write additional query blocks; we can add other media queries after the existing query media using comma separation (similar to an 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;
    }
}

Try it Yourself »

实例

More Examples

Use a mailing list link on a webpage sidebar
This example adds a list of email links to the left sidebar of the web page.

other extensions