css3怎样实现屏幕自适应?

html-css0127

css3怎样实现屏幕自适应?,第1张

/* 打印样式 */

@media print {

color: red

}

/* 手机等小屏幕手持设备 */

@media screen and (min-width: 320px) and (max-width: 480px) {

body {

background: yellow

}

}

/* 平板之类的宽度 1024 以下设备 */

@media only screen and (min-width: 321px) and (max-width: 1024px) {

body {

background: blue

}

}

/* PC客户端或大屏幕设备: 1028px 至更大 */

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

body {

background: green

}

}

/* 竖屏 */

@media screen and (orientation:portrait) and (max-width: 720px) {对应样式}

/* 横屏 */

@media screen and (orientation:landscape){对应样式}

手机、平板设备屏幕尺寸众多,宽度不一,写手机、平板对应css时最好用百分比宽度来自适应各种大小不一的屏幕宽度。这个百分比尺寸不算太难但也不是个太轻便的活,如果你css不熟练的话最好先别折腾这块,把基础练好了来。

还有,用了css媒体查询后,ie6-8载入不了样式表,要引用个:Respond.js来让ie6-8下正确载入css。js自行百度,到处有下载。

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<head>标签中,输入meta代码:<meta name="viewport" content="width=device-width, initial-scale=1">。

3、浏览器运行index.html页面,此时PC端的网页在移动端上也能自动适配。