Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 Media Query Examples

CSS3 Media Query Examples

In this chapter we will demonstrate some media query examples.

Before we start, let's make 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 »

Note data-email attribute. In HTML we can use the data- prefix to store information.


520 to 699px width - Add email icon

When the browser width is between 520 and 699px, add a mail 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 »


700 to 1000px - Add text prefix

When the browser width is between 700 and 1000px, "Email: " is added 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 »


Greater than 1001px width - Add email address

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

We will use the data- attribute to add an 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;

    }

}

Try it »


Greater than 1151px width - Add icon

When the browser width is greater than 1001px, an icon is added before the name.

In the example, we did not write additional query blocks; we can add other media queries by separating them with commas after the existing media 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;

    }

}

Try it »


More examples

Using email list links in a webpage sidebar
This example adds a list of email links to the left sidebar of a webpage.