CSS3 Media Queries模板

一篇好文章,收藏以备用。

本文转自W3CPLUS

原文:地址:http://www.w3cplus.com/css3/css3-media-queries-for-different-devices


最早在《CSS3 Media Queries》一文中初探了CSS3的媒体类型和媒体特性的相关应用。简单的知道了使用这个能在各种不同的设备显示不一样的样式风格。随着Responsive的响应式设计的兴起,前面跟大家一起学习了:

  1. Responsive设计和CSS3 Media Queries的结合
  2. 了解Responsive网页设计的三个特性
  3. Responsive设计的关键三步

从这几篇文章中可以知道,在Responsiv的设计中,CSS3的Media是起着非常关键性的作用,也可以说没有CSS3 Media这个属性,Responsiv设计是玩不起来,也是玩不转的。大家都知道Responsiv是为各种不同的设备进行样式设计的,但有一个问题大家或许还处在模糊状态,这个CSS3 Media要如何设置各自的临界点?

那么今天我们就针对上面的问题,一起来探讨一下CSS3 Media Queries在各种不同设备(桌面,手机,笔记本,ios等)下的模板制作。那么Media Queries是如何工作的?那么有关于他的工作原理大家要是感兴趣的话可以参考《CSS3 Media Queries》一文,里面已经做过详细的介绍,这里就不在进行过多的阐述。

CSS3 Media Queries模板

CSS3 Media Queries一般都是使用“max-width”和“min-width”两个属性来检查各种设备的分辨大小与样式表所设条件是否满足,如果满足就调用相应的样式。打个比方来说,如果你的Web页面在960px的显屏下显示,那么首先会通过CSS3 Media Queries进行查询,看看有没有设置这个条件样式,如果找到相应的,就会采用对应下的样式,其他的设备是一样的道理。下面具体看看“max-width”和“min-width”模板:

使用max-width

		@media screen and (max-width: 600px) {
			//你的样式放在这里....
		}
	

使用min-width

		@media screen and (min-width: 900px) {
			//你的样式放在这里...
		}
	

max-width和min-width的混合使用

		@media screen and (min-width: 600px) and (max-width: 900px) {
			//你的样式放在这里...
		}
	

同时CSS3 Media Queries还能查询设备的宽度“device-width”来判断样式的调用,这个主要用在iPhone,iPads设备上,比如像下面的应用:

iPhone和Smartphones上的运用

		/* iPhone and Smartphones (portrait and landscape) */
		@media screen and (min-device-width : 320px) and (max-device-width: 480px) {
			//你的样式放在这里...
		}
	

max-device-width指的是设备整个渲染区宽度(设备的实际宽度)

iPads上的运用

		/* iPads (landscape) */
		@media screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
			//你的样式放在这里...
		}
		/* iPads (portrait) */
		@media screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
			//你的样式放在这里...
		}
	

针对移动设备的运用,如果你想让样式工作得比较正常,需要在“<head>”添加“viewport”的meta标签:

		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	

有关于这方面的介绍大家可以看看这个blog上进行的的移动端开发的相关总结。

调用独立的样式表

你可能希望在不同的设备下调用不同的样式文件,方便管理或者维护,那么你可以按下面的这种方式进行调用:

		<link rel="stylesheet" media="screen and (min-device-width : 320px) and (max-device-width: 480px)" href="iphone.css" />
		<link rel="stylesheet" media="screen and (min-device-width : 768px) and (max-device-width : 1024px)" href="ipad.css" />
	
CSS3 Media Queries在标准设备上的运用

下面我们一起来看看CSS3 Meida Queries在标准设备上的运用,大家可以把这些样式加到你的样式文件中,或者单独创建一个名为“responsive.css”文件,并在相应的条件中写上你的样式,让他适合你的设计需求:

1、1024px显屏

		@media screen and (max-width : 1024px) {
			/* CSS Styles */
		}
	

2、800px显屏

		@media screen and (max-width : 800px) {
			/* CSS Styles */
		}
	

3、640px显屏

		@media screen and (max-width : 640px) {
			/* CSS Styles */
		}
	

4、iPad横板显屏

		@media screen and (max-device-width: 1024px) and (orientation: landscape) {
			/* CSS Styles */
		}
	

5、iPad竖板显屏

		@media screen and (max-device-width: 768px) and (orientation: portrait) {
			/* CSS Styles */
		}
	

iPhone 和 Smartphones

		@media screen and (min-device-width: 320px) and (min-device-width: 480px) {
			/* CSS Styles */
		}
	

现在有关于这方面的运用也是相当的成熟,twitter的Bootstrap第二版本中就加上了这方面的运用。大家可以对比一下:

	// Landscape phones and down
	@media (max-width: 480px) { ... }
	 
	// Landscape phone to portrait tablet
	@media (max-width: 768px) { ... }
	 
	// Portrait tablet to landscape and desktop
	@media (min-width: 768px) and (max-width: 980px) { ... }
	 
	// Large desktop
	@media (min-width: 1200px) { .. }
	

bootstrap中的responsive.css采用的是网格布局,大家可以到官网查看或下载其源码进行学习。另外960gs为大家提供了一个Adapt.js也很方便的帮大家实现上述效果。感兴趣的同学可以去了解了解。

下面给大家提供几个这方面的案例,以供大家参考:

  1. CSS3 Media Queries案例——Hicksdesign
  2. CSS3 Media Queries案例——Tee Gallery
  3. CSS3 Media Queries案例——A List Apart
更新CSS3 Media Queries模板查询

1、Smartphones (portrait and landscape)

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
	/* Styles */
}

2、Smartphones (landscape)

@media only screen and (min-width : 321px) {
	/* Styles */
}

3、Smartphones (portrait)

@media only screen and (max-width : 320px) {
	/* Styles */
}

4、iPads (portrait and landscape)

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
	/* Styles */
}

5、iPads (landscape)

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
	/* Styles */
}

6、iPads (portrait)

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
	/* Styles */
}

7、iPhone 4

@media only screen and (-webkit-min-device-pixel-ratio : 1.5),only screen and (min-device-pixel-ratio : 1.5) {
	/* Styles */
}

8、640px显屏

@media screen and (max-width : 640px) {
	/* CSS Styles */
}

9、800px显屏

@media screen and (max-width : 800px) {
	/* CSS Styles */
}

10、1024显屏

@media screen and (max-width : 1024px) {
	/* CSS Styles */
}

11、Desktops and laptops

@media only screen and (min-width : 1224px) {
	/* Styles */
}

12、Large screens

@media only screen and (min-width : 1824px) {
	/* Styles */
}

那么有关于CSS3 Media Queries模板的相关介绍就说到这里了,最后希望大家喜欢。如果你觉得这篇文章对你有所帮助或者比较有实用价值,您可以把他推荐给您的朋友,如果你有更好的分享可以在下面的评论中直接与我们一起分享您的经验。

2012年10月09日更新
@media only screen and (min-width: 320px) {

  /* Small screen, non-retina */

}

@media
only screen and (-webkit-min-device-pixel-ratio: 2)      and (min-width: 320px),
only screen and (   min--moz-device-pixel-ratio: 2)      and (min-width: 320px),
only screen and (     -o-min-device-pixel-ratio: 2/1)    and (min-width: 320px),
only screen and (        min-device-pixel-ratio: 2)      and (min-width: 320px),
only screen and (                min-resolution: 192dpi) and (min-width: 320px),
only screen and (                min-resolution: 2dppx)  and (min-width: 320px) { 

  /* Small screen, retina, stuff to override above media query */

}

@media only screen and (min-width: 700px) {

  /* Medium screen, non-retina */

}

@media
only screen and (-webkit-min-device-pixel-ratio: 2)      and (min-width: 700px),
only screen and (   min--moz-device-pixel-ratio: 2)      and (min-width: 700px),
only screen and (     -o-min-device-pixel-ratio: 2/1)    and (min-width: 700px),
only screen and (        min-device-pixel-ratio: 2)      and (min-width: 700px),
only screen and (                min-resolution: 192dpi) and (min-width: 700px),
only screen and (                min-resolution: 2dppx)  and (min-width: 700px) { 

  /* Medium screen, retina, stuff to override above media query */

}

@media only screen and (min-width: 1300px) {

  /* Large screen, non-retina */

}

@media
only screen and (-webkit-min-device-pixel-ratio: 2)      and (min-width: 1300px),
only screen and (   min--moz-device-pixel-ratio: 2)      and (min-width: 1300px),
only screen and (     -o-min-device-pixel-ratio: 2/1)    and (min-width: 1300px),
only screen and (        min-device-pixel-ratio: 2)      and (min-width: 1300px),
only screen and (                min-resolution: 192dpi) and (min-width: 1300px),
only screen and (                min-resolution: 2dppx)  and (min-width: 1300px) { 

  /* Large screen, retina, stuff to override above media query */

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@charset "utf-8"; /* CSS Document */ @media screen and (max-width: 1000px) { .header-inner,.banner-inner,.inner-box,.main-inner,.footer-inner,.banner-1-inner{ width:100%;} .header{ padding-bottom:12px;} .header-inner{ width:96%;} .header-left{ position:inherit; padding-bottom:80px;} .header-right{ float:right; padding:16px 5% 10px 0; position:relative;} .nav-btn{ display:block; background:#1271A0; width:40px; height:40px; line-height:40px; position:absolute; right:10px; bottom:6px; text-align:center; border-radius:5px; cursor:pointer;} .nav-btn:hover{ background:#2786b5;} .nav-btn i:before{ content:"≡"; font-size:30px; color:#fff;} .smartmenu{ display:none;} .nav-narrow{ display:block; float:left; width:100%;} .nav-narrow li{ background:#1271A0; text-align:center; border-bottom:1px solid #ddd; transition: all 0.1s ease 0.01s;} .nav-narrow li:hover{ background:#2786b5;} .nav-narrow li:hover a{ color:#fff;} .nav-narrow li>a{ display:block; line-height:28px; color:#fff; font-size:14px; font-weight:bold;} /********************home-banner*************************/ .rslides{width:100%;} .rslides li{ margin:0;} .rslides img{ width:100%;} .focus{width:100%; height:auto;} .banner-1-inner img{ padding:0; width:100%;} .h-aslsc{ margin-top:0; position:inherit; z-index:inherit;} .h-aslsc .inner-box{ width:96%;} .h-aslsc-l{ width:100%; padding:0;} .h-aboutus{ float:left; width:48%; margin-top:10px;} .h-news{ float:right; width:48%;} .h-news-b li{ width:auto;} .h-aslsc-r{ width:100%; padding:0; padding-top:10px;} .h-sas{ width:100%;} .h-sas-t{ border:none; padding:0; width:auto; text-align:left;} .h-sas-b{ padding:0; border:none; padding-top:6px;} .h-sas-b li{ width:31%; height:auto; margin:0 1%;} .h-sas-b li img.sas-img{ width:100%;} .h-sas-b li a{ float:none; position:inherit; left:inherit; top:inherit; display:block; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;} .h-prolist{ width:100%;} .h-client{ float:left;} .footer-list .footer-inner{ width:96%;} .footer-logo{ display:none;} .h-production-line{ width:100%; padding-bottom:8px; border-bottom:1px dashed #333;} .sliq-5{ float:left; width:100%;} .flist-box{ width:50%; margin-right:0; background:none;} .footer-inner{ width:96%;} /*about us*/ .main-top-left{ width:23%;} .main-top-right{ width:72%;} .latest-info dl dt img{ width:100%;} /*news*/ .news-content dl{ width:94%; height:auto; background:none; border-bottom:1px dashed #ccc; padding-bottom:10px; padding-left:0;} .news-content dl dt{ display:none;} .news-content dl dd{ width:100%; margin-right:0;} .news-list-desp span{ height:auto;} .news-list-desp a{ padding-top:6px;} /*news1*/ .related-products ul.rel-p li{ width:21%; height:auto; margin:4px 2% 0 2%;} .related-products ul.rel-p li img{ width:100%;} /*products*/ .left-news{ display:none;} .produsts-page-list{ margin-right:0;} .produsts-page-list ul{ margin-right:0;} .produsts-page-list ul li{ width:31%; margin-right:0;} .produsts-page-list a{ width:90%;} .produsts-page-list ul li img{ width:96%;} .produsts-page-list .hmpro-des{ display:none;} /*products1*/ #gallery{ margin:36px 8px 0 8px;} .CALLERY_CONTENT{ width:38%;} .menu1 li{ width:22%; margin-right:1%; background:#ccc; color:#000;} .menu1 li.hover{ width:22%; margin-right:1%;} /*certificates*/ #showimg ul li{ width:48%; height:auto; margin:0 1% 10px 1%;} #showimg ul li img{ width:100%;} /*service*/ .ser h1{ font-size:18px; height:28px; line-height:28px;} /*contact*/ .contact-inquiry{ padding-left:0;} .products1-inquiry input{ width:98%;} .products1-inquiry .pi-message{ width:96%;} .products1-inquiry .products1-inquiry-div dl{ width:31%; margin-right:2%;} .products1-inquiry .products1-inquiry-div input{ width:97%;} } @media screen and (max-width: 860px) { .copyright{ padding:0;} .copyright p{ text-indent:inherit;} .copyright a{ display:none;} .tsbw-img{ display:none;} /*products1*/ #gallery{ float:none; overflow:hidden; display:block; width:290px; margin:10px auto 0 auto;} .CALLERY_CONTENT{ float:none; width:90%; padding:10px 5% 10px 5%; margin-top:10px; overflow:hidden; min-height:inherit;} } @media screen and (max-width: 740px) { /*products*/ .produsts-page-list ul li{ width:50%;} } @media screen and (max-width: 650px) { .nav-t{ display:none;} .smartmenu{ display:none;} .nav-b{ display:none;} .nav-t{ display:none;} .nav-t{ display:none;} .footer{ padding-bottom:60px;} .contact-bar{ display:none;} .rw-footer-box{ display:block;} } @media screen and (max-width: 639px) { .main-top-left{ float:none; width:94%;} .main-top-right{ float:none; width:94%; margin-left:10px;} .left-contacti{ display:none;} /*about us*/ .about_text img{ width:100%;} .latest-info dl dt{ padding:0;} .latest-info dl dt img{ display:none;} /*news1*/ .news1-page img{ width:100%;} /*products1*/ .load_botton{ display:none;} /*contact*/ .contactus-page-f{ float:none; width:100%; padding-bottom:20px;} .contactus-page-p{ float:none; width:100%;} } @media screen and (max-width: 580px) { .header-left{ width:100%; text-align:center;} } @media screen and (max-width: 480px) { .header{ padding-bottom:0;} .header-left img{ width:auto;} .h-aboutus{ width:100%; margin-top:10px;} .h-aboutus-b img{ width:100%;} .h-news{ width:100%;} .h-news-b li{ width:auto;} .h-sas-b li{ width:100%; height:auto; margin:0; margin-bottom:10px;} .h-client{ display:none;} /*news1*/ .related-products ul.rel-p li{ width:46%; margin:4px 2% 0 2%;} .related-products ul.rel-p li img{ width:100%;} /*products1*/ .menu1 li{ width:48%; height:33px; margin-right:1%; background:#ccc; color:#000; border-top:1px solid #fff;} .menu1 li.hover{ width:48%; margin-right:1%; height:33px;} .products_text .main1 dl dt img{ width:100%;} /*service*/ .ser-content img{ width:100%;} /*contact*/ .products1-inquiry .products1-inquiry-div dl{ width:100%;} .products1-inquiry .products1-inquiry-div input{ width:98%;} } @media only screen and (-moz-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) { body{ font-size:24px; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值