欢迎光临! 【模板119】平台每天更新源码-所有VIP源码均有在线演示

logo

建站一条龙服务

响应式web设计之CSS3 Media Queries

织梦源码推荐
响应式web设计之CSS3 Media Queries二维码

本文手机访问二维码

开始研究响应式web设计,CSS3 Media Queries是入门。

Media Queries,其作用就是允许添加表达式用以确定媒体的环境情况,以此来应用不同的样式表。换句话说,其允许我们在不改变内容的情况下,改变页面的布局以精确适应不同的设备。

那么,Media Queries是如何工作的?

两种方式,一种是直接在link中判断设备的尺寸,然后引用不同的css文件:

<link rel="stylesheet" type="text/css" href="styleA.css" media="screen and (min-width: 400px)">

意思是当屏幕的宽度大于等于400px的时候,应用styleA.css

在media属性里:

  • screen 是媒体类型里的一种,CSS2.1定义了10种媒体类型
  • and 被称为关键字,其他关键字还包括 not(排除某种设备),only(限定某种设备)
  • (min-width: 400px) 就是媒体特性,其被放置在一对圆括号中。完整的特性参看 相关的Media features部分
<link rel="stylesheet" type="text/css" href="styleB.css"  media="screen and (min-width: 600px) and (max-width: 800px)">

意思是当屏幕的宽度大于600小于800时,应用styleB.css

其它属性可看这里:http://www.swordair.com/blog/2010/08/431/

另一种方式,即是直接写在<style>标签里:

@media screen and (max-width: 600px) { /*当屏幕尺寸小于600px时,应用下面的CSS样式*/
  .class {
    background: #ccc;
  }
}

写法是前面加@media,其它跟link里的media属性相同

其实基本上就是样式覆盖~,判断设备,然后引用不同的样式文件覆盖。

要注意的是由于网页会根据屏幕宽度调整布局,所以不能使用绝对宽度的布局,也不能使用具有绝对宽度的元素。这一条非常重要,否则会出现横向滚动条。

 

补充:media query中的not only all等关键字

今天在群里一群友问起 @media only screen and (min-width: 320px) 中only是什么意思,查了些资料。

not: not是用来排除掉某些特定的设备的,比如 @media not print(非打印设备)、

only: 用来定某种特别的媒体类型。对于支持Media Queries的移动设备来说,如果存在only关键字,移动设备的Web浏览器会忽略only关键字并直接根据后面的表达式应用样式文件。对于不支持Media Queries的设备但能够读取Media Type类型的Web浏览器,遇到only关键字时会忽略这个样式文件。

all: 所有设备,这个应该经常看到

还有其它一些:

media_type

设备类型说明

all

所有设备

aural

听觉设备

braille

点字触觉设备

handled

便携设备,如手机、平板电脑

print

打印预览图等

projection

投影设备

screen

显示器、笔记本、移动端等设备

tty

如打字机或终端等设备

tv

电视机等设备类型

embossed

盲文打印机

相关资料扩展:http://book.51cto.com/art/201204/328362.htm

        http://www.w3cplus.com/content/css3-media-queries

        http://www.w3.org/TR/CSS2/media.html#media-types

----------------------------------华丽的分割线-----------------------------------------------------------

以下是demo

一个三栏布局的,在不同的尺寸下,变为两栏,再变为一栏~

代码:

复制代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>css3-media-queries-demo</title>
<style>
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, textarea, p, blockquote, th, td {
    padding: 0;
    margin: 0;
}
.content{
    zoom:1;
}
.content:after{
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden; 
}
.leftBox, .rightBox{
    float: left;
    width: 20%;
    height: 500px;
    margin: 5px;
    background: #ffccf7;
    display: inline;
    -webkit-transition: width 1s ease;
    -moz-transition: width 1s ease;
    -o-transition: width 1s ease;
    -ms-transition: width 2s ease;
    transition: width 1s ease;
}
.middleBox{
    float: left;
    width: 50%;
    height: 800px;
    margin: 5px;
    background: #b1fffc;
    display: inline;
    -webkit-transition: width 1s ease;
    -moz-transition: width 1s ease;
    -o-transition: width 1s ease;
    -ms-transition: width 1s ease;
    transition: width 1s ease;
}
.rightBox{
    background: #fffab1;
}
@media only screen and (min-width: 1024px){
    .content{
            width: 1000px;
            margin: auto
        }
}
@media only screen and (min-width: 400px) and (max-width: 1024px){
    .rightBox{
        width: 0;
    }
    .leftBox{ width: 30%}
    .middleBox{ width: 65%}
}
@media only screen and (max-width: 400px){
    .leftBox, .rightBox, .middleBox{ 
        width: 98%;
        height: 200px;
    }
}
</style>
</head>

<body>
<div class="content">
  <div class="leftBox"></div>
  <div class="middleBox"></div>
  <div class="rightBox"></div>
</div>
</body>
</html>
复制代码



模板119提醒您 以下是一种更保险的实现方式
 

PHP、asp、js判断客户端输出对应的样式

 

------------------- 1.媒体查询方法在 css 里面这样写 --------------------

@media screen and (min-width: 320px) and (max-width: 480px){
在这里写小屏幕设备的样式
}

 

@media only screen and (min-width: 321px) and (max-width: 1024px){
这里写宽度大于321px小于1024px的样式(一般是平板电脑)
}

 

@media only screen and (min-width: 1029px){
这里写pc客户端的样式
}

 

------------------- 2.用js根据客户端输出对应样式 --------------------

/*事实上用asp、php后台判断更保险,js在前端,有可能被用户禁止*/

function loadCSS() {
 if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|iOS|iPad|Android|wOSBrowser|BrowserNG|WebOS)/i))) {
        document.write('<link href="css/pad-phone.css" rel="stylesheet" type="text/css" media="screen" />');
    }
    else {
        document.write('<link href="css/pc.css" rel="stylesheet" type="text/css" media="screen" />');
    }
}
loadCSS();

 

 

 

------------------- 3.既判断分辨率,也判断浏览器-------------------

应E.Qiang提议,重新完善代码,使之成为判断浏览器类型屏幕分辨率自动调用不同CSS的代码。
代码如下:
<SCRIPT LANGUAGE="JavaScript">
<!--
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
var IE1024="";
var IE800="";
var IE1152="";
var IEother="";
ScreenWidth(IE1024,IE800,IE1152,IEother)
}else{
if (window.navigator.userAgent.indexOf("Firefox")>=1)
{
//如果浏览器为Firefox
var Firefox1024="";
var Firefox800="";
var Firefox1152="";
var Firefoxother="";
ScreenWidth(Firefox1024,Firefox800,Firefox1152,Firefoxother)
}else{
//如果浏览器为其他
var Other1024="";
var Other800="";
var Other1152="";
var Otherother="";
ScreenWidth(Other1024,Other800,Other1152,Otherother)
}
}
function ScreenWidth(CSS1,CSS2,CSS3,CSS4){
if ((screen.width == 1024) && (screen.height == 768)){
setActiveStyleSheet(CSS1);
}else{
if ((screen.width == 800) && (screen.height == 600)){
setActiveStyleSheet(CSS2);
}else{
if ((screen.width == 1152) && (screen.height == 864)){
setActiveStyleSheet(CSS3);
}else{
setActiveStyleSheet(CSS4);
}}}
}
function setActiveStyleSheet(title){
document.getElementsByTagName("link")[0].href="style/"+title;
}
//-->
</SCRIPT>

解释:
var IE1024="";
var IE800="";
var IE1152="";
var IEother="";
引号里面分别填写,用户使用IE的时候并且分辨率为1024*768,800*600,1152*864要使用的css文件名.
var Firefox1024="";
var Firefox800="";
var Firefox1152="";
var Firefoxother="";
引号里面分别填写,用户使用FF的时候并且分辨率为1024*768,800*600,1152*864要使用的css文件名.
var Other1024="";
var Other800="";
var Other1152="";
var Otherother="";
引号里面分别填写,用户使用其他浏览器的时候并且分辨率为1024*768,800*600,1152*864要使用的css文件名.

例子:

不判断分辨率,只判断浏览器

实现根据浏览器类型自动调用不同CSS。

<SCRIPT LANGUAGE="JavaScript">
<!--
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{    
//如果浏览器为IE
setActiveStyleSheet("default.css");
}else{
if (window.navigator.userAgent.indexOf("Firefox")>=1)
{
//如果浏览器为Firefox
setActiveStyleSheet("default2.css");
}else{
//如果浏览器为其他
setActiveStyleSheet("newsky.css");
}
}
function setActiveStyleSheet(title){
document.getElementsByTagName("link")[0].href="style/"+title;
}
//-->
</SCRIPT>

解释:
如果浏览器为IE,则调用default.css
如果浏览器为Firefox,则调用default2.css
如果浏览器为其他,则调用newsky.css

用法:
放在
</head>
前面即可。


只要求判断根据屏幕宽度选择不同的CSS样式表。

<script language=javascript>
<!--
if (screen.width == 800)
{
document.write('<link rel=stylesheet type="text/css" href="css800.css">')
}
else {document.write('<link rel=stylesheet type="text/css" href="css1024.css">')}
//-->
</script>

 


响应式web设计之CSS3 Media Queries二维码

本文手机访问二维码

作者:mb119.com        关注度:         时间:2017-05-04 21:52
首先声明,只要是我们的vip会员所有源码均可以免费下载,不做任何限制
☉本站的源码不会像其它下载站一样植入大量的广告。
☉本站只提供精品织梦源码,源码在于可用,不在多!!希望在这里找到你合适的。
☉本站提供的整站织梦程序,均带数据及演示地址。可以在任一源码详情页查看演示地址
☉本站所有资源(包括源码、模板、素材、特效等)仅供学习与参考,请勿用于商业用途。
☉如有其他问题,请加网站客服QQ(971977809)进行交流点击这里给我发消息
相关程序开发内容
在线客服

金融贵金属 HTML5模板响应式模板手机模板营销型模板双语模板商城网站aspcms模板