格式:
@media 媒体类型 and (媒体特征) {
样式
}
一、自适应
test10.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自适应</title>
<!-- 链接样式 -->
<link rel="stylesheet" type="text/css" href="css/test10.css"/>
</head>
<body>
<div class="box">
中华
</div>
</body>
</html>
test10.css
*{
padding: 0;
margin:0;
}
.box{
width:100%;
height: 300px;
text-align: center; /* 文本居中 */
}
/* screen设备名称 */
/* 屏幕宽度大于1200px */
@media screen and (min-width:1200px) {
.box{
background-color: blue;
font-size: 100px;
}
}
/* 屏幕宽度在980px到1200px */
@media screen and (min-width:980px) and (max-width:1200px) {
.box{
background-color: yellow;
font-size: 50px;
}
}
/* 屏幕宽度小于980px */
@media screen and (max-width:980px) {
.box{
background-color: green;
font-size: 20px;
}
}
运行效果:
二、响应式
test11.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>响应式</title>
<link rel="stylesheet" media="screen and (min-width:1200px)" type="text/css" href="css/pc_mee.css"/>
<link rel="stylesheet" media="screen and (min-width:980px) and (max-width:1200px)" type="text/css" href="css/ipad_mee.css"/>
<link rel="stylesheet" media="screen and (max-width:980px)" type="text/css" href="css/phone_mee.css"/>
</head>
<body>
<div class="pro_box">
<!-- 标题 -->
<div class="title">
<h2>适用于的工作地点</h2>
</div>
<!-- 四个小模块 -->
<div class="pro_list">
<ul>
<li class="item">
<!-- 图片 -->
<div class="imgs">
<img src="img/微软.png" alt="">
</div>
<!-- 文案 -->
<div class="content">
<h4>windows10 企业版</h4>
<p>下载适用于IT专业人士的90天免费评估版本</p>
<a href="#">立即购买</a>
</div>
</li>
<li class="item">
<!-- 图片 -->
<div class="imgs">
<img src="img/微软.png" alt="">
</div>
<!-- 文案 -->
<div class="content">
<h4>windows10 企业版</h4>
<p>下载适用于IT专业人士的90天免费评估版本</p>
<a href="#">立即购买</a>
</div>
</li>
<li class="item">
<!-- 图片 -->
<div class="imgs">
<img src="img/微软.png" alt="">
</div>
<!-- 文案 -->
<div class="content">
<h4>windows10 企业版</h4>
<p>下载适用于IT专业人士的90天免费评估版本</p>
<a href="#">立即购买</a>
</div>
</li>
<li class="item">
<!-- 图片 -->
<div class="imgs">
<img src="img/微软.png" alt="">
</div>
<!-- 文案 -->
<div class="content">
<h4>windows10 企业版</h4>
<p>下载适用于IT专业人士的90天免费评估版本</p>
<a href="#">立即购买</a>
</div>
</li>
</ul>
</div>
</div>
</body>
</html>
pc_mee.css(电脑端)
*{
padding:0;
margin:0;
}
a{
text-decoration: none; /* 去掉a链接的下划线 */
}
ul,li{
list-style: none; /* 列表去掉小黑点 */
}
.pro_box{
width: 90%; /* 用百分比能更好地适应不同分辨率屏幕 90%左右留有间距 */
margin:0 auto; /* 盒子居中 */
/* background-color: #008000; 添加颜色,检验盒子居中效果 */
}
.pro_box .title h2{
font-size: 36px;
line-height: 80px;
}
.pro_list .item{
float: left; /* 浮动,让列表元素横排 */
width:24%;
margin-right: 1%; /* 24%+1%=25% *4=100% */
}
.item .imgs img{
display: block; /* 转为块元素 */
width:100%;
}
.item .content h4{
font-size: 20px;
margin:20px 0;
}
.item .content p{
font-size: 18px;
line-height: 1.2em;
}
.item .content a{
display: block; /* 转为块元素 */
font-size: 16px;
/* 只有先转为块元素,外边距才起作用 */
margin-top: 10px;
color: #0000FF;
}
ipad_mee.css(平板端)
*{
padding:0;
margin:0;
}
a{
text-decoration: none; /* 去掉a链接的下划线 */
}
ul,li{
list-style: none; /* 列表去掉小黑点 */
}
.pro_box{
width: 90%; /* 用百分比能更好地适应不同分辨率屏幕 90%左右留有间距 */
margin:0 auto; /* 盒子居中 */
/* background-color: #008000; 添加颜色,检验盒子居中效果 */
}
.pro_box .title h2{
font-size: 36px;
line-height: 80px;
}
.pro_list .item{
float: left; /* 浮动,让列表元素横排 */
width:49%;
margin-right: 1%; /* 49%+1%=50% *2=100% */
margin-bottom: 30px;
}
.item .imgs img{
display: block; /* 转为块元素 */
width:100%;
}
.item .content h4{
font-size: 20px;
margin:20px 0;
}
.item .content p{
font-size: 18px;
line-height: 1.2em;
}
.item .content a{
display: block; /* 转为块元素 */
font-size: 16px;
/* 只有先转为块元素,外边距才起作用 */
margin-top: 10px;
color: #0000FF;
}
phone_mee.css (手机端)
*{
padding:0;
margin:0;
}
a{
text-decoration: none; /* 去掉a链接的下划线 */
}
ul,li{
list-style: none; /* 列表去掉小黑点 */
}
.pro_box{
width: 90%; /* 用百分比能更好地适应不同分辨率屏幕 90%左右留有间距 */
margin:0 auto; /* 盒子居中 */
/* background-color: #008000; 添加颜色,检验盒子居中效果 */
}
.pro_box .title h2{
font-size: 36px;
line-height: 80px;
}
.pro_list .item{
/* float: left; */ /* 浮动,让列表元素横排 */
width:100%;
/* margin-right: 1%; */ /* 49%+1%=50% *2=100% */
margin-bottom: 30px;
}
.item .imgs img{
display: block; /* 转为块元素 */
width:100%;
}
.item .content h4{
font-size: 20px;
margin:20px 0;
}
.item .content p{
font-size: 18px;
line-height: 1.2em;
}
.item .content a{
display: block; /* 转为块元素 */
font-size: 16px;
/* 只有先转为块元素,外边距才起作用 */
margin-top: 10px;
color: #0000FF;
}
运行效果: