当前位置: 代码网 > it编程>网页制作>Css > CSS3媒体查询实现不同宽度的下不同内容的展示功能

CSS3媒体查询实现不同宽度的下不同内容的展示功能

2023年11月27日 Css 我要评论
CSS3媒体查询实现不同宽度的下不同内容的展示功能这篇文章主要介绍了CSS3媒体查询实现不同宽度的下不同内容的展示功能,本章节我们将为大家演示一些多媒体查询实例,需要的朋友可以参考下... 23-11-27

前言

css3 多媒体查询实例

本章节我们将为大家演示一些多媒体查询实例。

开始之前我们先制作一个电子邮箱的链接列表。html 代码如下:

<!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>

注意 data-email 属性。在 html 中我们可以使用带 data- 前缀的属性来存储信息。

520 到 699px 宽度 - 添加邮箱图标

当浏览器的宽度在 520 到 699px, 邮箱链接前添加邮件图标:

@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 到 1000px - 添加文本前缀信息

当浏览器的宽度在 700 到 1000px, 会在邮箱链接前添加 "email: ":

@media screen and (max-width: 1000px) and (min-width: 700px) {
    ul li a:before {
        content: "email: ";
        font-style: italic;
        color: #666666;
    }
}

大于 1001px 宽度 - 添加邮件地址

当浏览器的宽度大于 1001px 时,会在链接后添加邮件地址接。

我们会使用 data- 属性来为每个人名后添加邮件地址:

@media screen and (min-width: 1001px) {
    ul li a:after {
        content: " (" attr(data-email) ")";
        font-size: 12px;
        font-style: italic;
        color: #666666;
    }
}

大于 1151px 宽度 - 添加图标

当浏览器的宽度大于 1001px 时,会在人名前添加图标。

实例中,我们没有编写额外的查询块,我们可以在已有的查询媒体后使用逗号分隔来添加其他媒体查询 (类似 or 操作符):

@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;
    }
}

代码

<!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) {
            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;
            }
        }
        @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;
            }
        }
    </style>
</head>
<body>
    <ul>
        <li><a data-email="johndoe@example.com" href="mailto:johndoe@example.com">john doe</a></li>3
        <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>

后言

到此这篇关于css3媒体查询实现不同宽度的下不同内容的展示的文章就介绍到这了,更多相关css3媒体查询内容请搜索代码网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com