首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >响应式布局(媒体查询)

响应式布局(媒体查询)

作者头像
贵哥的编程之路
发布2020-10-30 11:06:30
发布2020-10-30 11:06:30
1.1K0
举报
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style type="text/css">
		*{padding: 0px;margin: 0px;}
		body
		{
			transition: 0.3s;
		}
		@media all and (max-width: 900px)
		{
			body
			{
				background: red;
			}
		}
		@media all and (min-width: 900px) and (max-width: 1200px)
		{
			body
			{
				background: yellow;
			}
		}
		@media all and (min-width: 1200px)
		{
			body
			{
				background: black;
			}
		}
	</style>
</head>
<body>

</body>
</html>

数字>=1200变成黑色. 900<=数字 <=1200.变成黄色. 数字<=900就红色.

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style type="text/css">
		*{padding: 0px;margin: 0px;}
		body
		{
			transition: 0.3s;
		}
		@media all and (orientation:landscape)
		{
			body
			{
				background: black;
			}
			div
			{
				position: absolute;
				width: 100px;
				height: 100px;
				left: 20px;top: 50px;
			}
		}
		@media all and (orientation:portrait)
		{
			body
			{
				background: yellow;
			}
			div
			{
				position: absolute;width: 50px;height: 150px;
				background: green;
				left: 50px;top: 20px;
			}
		}
	</style>
</head>
<body>

</body>
</html>

解释:横屏黑色.竖屏(例如手机端)黄色.

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css" media="(orientation:landscape)">
	<link rel="stylesheet" type="text/css" href="2.css" media="(orientation:portrait)">
	<style type="text/css">

		*{padding: 0px;margin: 0px;}

	</style>
</head>
<body>
<div></div>
</body>
</html>

1.css body{background:black;} 2.css body{background:red;}

横屏黑色,竖屏红色.

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style type="text/css">
		*{margin: 0;padding: 0;}
div{transition:0.9s;}
@media all and (max-width: 600px){
	div{width: 200px;height: 200px;background: red};
}
@media all and (min-width: 600px) and (max-width: 800px){
	div{width: 300px;height: 300px;background: green};
}
@media all and (min-width: 800px){
	div{width: 400px;height: 400px;background: yellow};
}
	</style>
</head>
<body>
	<div id='div1'></div>
	<script type="text/javascript">
		div1.οnclick=function()
		{
			if(innerWidth<600)
			{
				alert(1);
			}
			else if(innerWidth>600&&innerWidth<800)
			{
				alert(2);
			}
			else
			{
				alert(3);
			}
		}
	</script>
</body>
</html>

在小于600的情况下,红色。.点击弹出1 在600以上800以下的情况下,green。.点击弹出2 在800以上的话,是黄色.点击弹出3

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/10/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档