响应式布局(媒体查询)

<!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就红色.

<!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>

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

<!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;}

横屏黑色,竖屏红色.

<!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

猜你喜欢

转载自blog.csdn.net/qq_37805832/article/details/109324754