style内部使用媒体查询
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- width=device-width:设置视口的宽度为设备的宽度,这样网页内容能够适应不同大小的屏幕 -->
<!-- initial-scale=1.0:设置初始缩放比例为1,确保网页在加载时不进行缩放-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>
<style>
#div0 {
width: 500px;
height: 200px;
background: #3c9;
margin: 0 auto;
}
#div0 div {
width: 100%;
height: 50px;
float: left;
}
#div0 div:nth-child(1) {
background-color: #927;
}
#div0 div:nth-child(2) {
background-color: #972;
}
#div0 div:nth-child(3) {
background-color: #733;
}
/*使用媒体查询针对屏幕宽度小于或等于499像素的设备*/
@media screen and (max-width: 499px) {
#div0 div {
width: 100%;
}
}
/*使用媒体查询针对屏幕宽度在501像素到1000像素之间的设备*/
@media screen and (min-width: 501px) and (max-width: 1000px) {
#div0 div {
width: 50%;
}
}
/*使用媒体查询针对屏幕宽度大于1001像素的设备*/
@media screen and (min-width: 1001px) {
#div0 div {
width: 33.33%;
}
}
</style>
</head>
<body>
<div id="div0">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
link使用媒体查询
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>
<link rel="stylesheet" href="css/css1.css">
<link rel="stylesheet" href="css/css2.css">
</head>
<body>
<div id="div0">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
css文件配置
css1.css
#div0 {
width: 500px;
height: 200px;
background: #3c9;
margin: 0 auto;
}
#div0 div {
width: 100%;
height: 50px;
float: left;
}
#div0 div:nth-child(1) {
background-color: #927;
}
#div0 div:nth-child(2) {
background-color: #972;
}
#div0 div:nth-child(3) {
background-color: #733;
}
css2.css
/*使用媒体查询针对屏幕宽度小于或等于499像素的设备*/
@media screen and (max-width: 499px) {
#div0 div {
width: 100%;
}
}
/*使用媒体查询针对屏幕宽度在501像素到1000像素之间的设备*/
@media screen and (min-width: 501px) and (max-width: 1000px) {
#div0 div {
width: 50%;
}
}
/*使用媒体查询针对屏幕宽度大于1001像素的设备*/
@media screen and (min-width: 1001px) {
#div0 div {
width: 33.33%;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了