关键字 @media
其中screen是代表显示器,如果是打印机等其他设备可相应修改;a.谨记and后面必须含有空格!!!b.就是媒体查询的大范围会覆盖小范围,所以编写的时候要先写大范围,不然小范围的样式会被覆盖!!!
<!DOCTYPE html
>
<
html
lang=
"en"
>
<
head
>
<
meta
charset=
"UTF-8"
>
<
meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<
meta
http-equiv=
"X-UA-Compatible"
content=
"ie=edge"
>
<
meta
name=
"viewport"
content=
"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scale=0"
/>
//引入外部文件的方式
<
link
rel=
"stylesheet"
type=
"text/css"
href=
"外部css文件"
media=
"screen and (max-width:960px)"
>
<
title
>Document
</
title
>
<
style
>
/*内部使用的方式*/
@media
screen and (
max-width:
960){
body{
background:
red
}
}
<
/
style
>
</
head
>
<
body
>
<
h1
>你好
</
h1
>
<
p
>响应式
</
p
>
</
body
>
</
html
>
小例子
<!DOCTYPE html
>
<
html
lang=
"en"
>
<
head
>
<
meta
charset=
"UTF-8"
>
<
meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<
meta
http-equiv=
"X-UA-Compatible"
content=
"ie=edge"
>
<
meta
name=
"viewport"
content=
"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scale=0"
/>
<!-- <link rel="stylesheet" type="text/css" href="外部css文件" media="screen and (max-width:960px)" > -->
<
title
>Document
</
title
>
<
style
>
/*内部使用的方式*/
body , div{
padding:
0px;
margin:
0px;
}
div{
width:
100%
}
section{
width:
25%;
height:
200px;
float:
left;
border:
1px
solid
red;
box-sizing:
border-box;
overflow:
hidden
}
@media
screen and (
max-width:
960px){
section{
width:
50%;
height:
200px;
float:
left;
border:
1px
solid
red;
box-sizing:
border-box;
overflow:
hidden
}
}
@media
screen and (
max-width:
500px){
section{
width:
100%;
height:
200px;
float:
left;
border:
1px
solid
red;
box-sizing:
border-box;
overflow:
hidden
}
}
<
/
style
>
</
head
>
<
body
>
<
div
>
<
section
><
img
src=
"../imegs/1.jpg"
/></
section
>
<
section
><
img
src=
"../imegs/1.jpg"
/></
section
>
<
section
><
img
src=
"../imegs/1.jpg"
/></
section
>
<
section
><
img
src=
"../imegs/1.jpg"
/></
section
>
</
div
>
</
body
>
</
html
>