css3 --- > 使用媒体查询进行响应式布局

css3引入@media,可以根据设备特性进行不同的布局,
本文展示的是根据不同屏幕的宽度进行不同的布局,代码如下:

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title> 针对浏览器宽度调整布局 </title>
	<style type="text/css">
		/* 设置默认的CSS样式 */
		#container{
			text-align: center;
			margin:auto;
			width:750px;
		}
		#container>div{
			border:1px solid #aaf;
			text-align: left;
			box-sizing: border-box;
			border-radius: 12px 12px 0px 0px;
			padding: 5px;
		}
		div#left{
			width:300px;
			height:260px;
			float:left;
		}
		div#main{
			width:450px;
			height:260px;
			float:left;
			clear:right;
		}
		div#right{
			width:750px;
			float:left;
		}
		/* 设置当浏览器宽度大于1000px时的CSS样式 */
		@media screen and (min-width:1000px){
			#container{
				text-align:center;
				margin:auto;
				width:960px;
			}
			#container{
				border:1px solid #aff;
				box-sizing: border-box;
				border-radius: 12px 12px 0px 0px;
				padding:5px;
			}
			div#left{
				width:240px;
				float:left;
				height:260px;
			}
			div#main{
				width:460px;
				float:left;
				height:260px;
				clear:none;
			}
			div#right{
				width:260px;
				float:left;
				height:260px;
			}
		}
		/* 设置当浏览器宽度小于480px时的CSS样式 */
		@media screen and (max-width: 480px){
			#container{
				text-align: center;
				margin:auto;
				width:450px;
			}
			#container>div{
				border:1px solid #aaf;
				box-sizing: border-box;
				border-radius: 12px 12px 0px 0px;
				padding:5px;
			}
			div#left{
				width:450px;
				float:left;
				height:150px;
			}
			div#main{
				width:450px;
				float:left;
				height:260px;
				clear:both;
			}
			div#right{
				width:450px;
				float:left;
				height:170px;
			}
		}
	</style>
</head>
<body>
	<div id="container">
		<div id ="left">
			<h2>紫薯布丁紫薯布丁</h2>
			<ul>
				<li>紫薯布丁紫薯布丁</li>
				<li>紫薯布丁紫薯布丁</li>
				<li>紫薯布丁紫薯布丁</li>
			</ul>
		</div>
		<div id = "main">
			<h2>紫薯布丁紫薯布丁</h2>
			疯紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁
			????紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁
		紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯
		紫薯布丁紫薯布丁
		</div>
		<div id="right">
			<h2>紫薯布丁紫薯布丁</h2>
			..紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁紫薯布丁
					
		</div>
	</div>
	

</body>
</html>

以下是宽度为847.27的屏幕的效果
在这里插入图片描述
以下是宽度为463.64px的效果:
在这里插入图片描述
参考《疯狂H5+CSS3+JS讲义》(第2版)P319~P320

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值