html - 媒体查询不起作用。我究竟做错了什么?
问题描述
当我调整窗口大小时,媒体查询不起作用,(max-width: 768px)
正在工作并且(max-width:425px)
不起作用,但是当我检查我的页面时,两者都不起作用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
h2 {
text-align: center;
padding-top: 50px;
text-transform: uppercase;
}
span {
font-size: 40px;
padding: 10px;
background-color: rgba(68, 77, 78, 0.6);
}
.box {
position: relative;
width: 25%;
float: left;
}
.img-text {
position: absolute;
top: 0;
left: 0;
}
.project-image {
width: 100%;
height: auto;
}
/* this is working when resize window but not when I inspect my page */
@media screen and (max-width: 768px) {
.box {
width: 50%;
}
}
/* this is not working on both */
@media screen and (max-width: 425px) {
.box {
width: 100%;
}
}
</style>
</head>
<body>
<div id="project">
<h2>Project</h2>
<div class="project-img-container">
<div class="box">
<span class="img-text">Summer House</span>
<img class="project-image" src="house5.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Brick House</span>
<img class="project-image" src="house2.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Renovated</span>
<img class="project-image" src="house3.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Barn House</span>
<img class="project-image" src="house4.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Summer House</span>
<img class="project-image" src="house2.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Brick House</span>
<img class="project-image" src="house5.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Renovated</span>
<img class="project-image" src="house4.jpg" alt="#">
</div>
<div class="box">
<span class="img-text">Barn House</span>
<img class="project-image" src="house3.jpg" alt="#">
</div>
</div>
</div>
</body>
</html>
为什么我的代码输出在检查窗口和实时服务器上都不同?
解决方案
抱歉,您在移动优化方面遇到问题,您似乎需要将以下元标记添加到您的 html 中。
<meta content="width=device-width, initial-scale=1" name="viewport" />
推荐阅读
- angular - Checkbox not working. Angular 2
- android - 令牌以及如何处理它们
- react-native - iOS模拟器上需要未知模块“11”错误
- youtube-api - Q: Youtube Data Api v3 - 如何增加配额
- angular - 无法在角度 5 中正常闪烁消息
- vb.net - VB.NET 查找图像的像素颜色
- sql - SQL - 如何在 where 子句条件中添加计数
- android - 使用服务时Android应用关闭时如何从服务器获取数据并显示在通知中
- javascript - 我在 html 页面中的函数没有执行
- java - 如何使用 Ant 扩展 xml 文件中的非实体引用