CSS3媒体查询实现不同宽度的下不同内容的展示功能
发布时间:2023-11-27 15:23:34 作者:鋜斗 我要评论
这篇文章主要介绍了CSS3媒体查询实现不同宽度的下不同内容的展示功能,本章节我们将为大家演示一些多媒体查询实例,需要的朋友可以参考下
前言
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媒体查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
相关文章
详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐)
这篇文章主要介绍了详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的2020-11-16css3媒体查询中device-width和width的区别详解
这篇文章主要介绍了css3媒体查询中device-width和width的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小2020-03-27- 这篇文章主要介绍了css3 响应式媒体查询的示例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下2019-09-25
- 这篇文章主要介绍了css3中媒体查询结合rem布局适配手机屏幕,需要的朋友可以参考下2019-06-10
- 这篇文章主要介绍了CSS3 Media Queries中媒体属性的使用,文章中还以一个响应式设计的例子作为补充讲解,需要的朋友可以参考下2016-02-29
- 这篇文章主要介绍了CSS3媒体查询基本学习教程,包括基本的语法和逻辑操作符,以及媒体查询可检测的特性与利用viewport禁止用户缩放等部分的内容,需要的朋友可以参考下2016-02-29
使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的方法
有无数的理由要求我们在任何时候都应该知道用户是使用的什么设备浏览我们的网站——宽屏,普通屏,平板,手机?知道这些特征,我们web应用的CSS和JavaScript才能同步做相应2014-04-03- 媒体类型:all 所有设备、screen 电脑显示器等等;媒体特性共13种,可以说是一个类似CSS属性的集合,感兴趣的朋友可以了解下本文,希望对大家有所帮助2013-09-12
最新评论