css - 背景图像没有响应的问题
问题描述
我正在尝试使背景图像响应媒体查询,但图像保留其原始大小。
我看不出我在哪里做错了。我很感激任何帮助
#wrapper{
background: url(https://via.placeholder.com/1500x1000) center center cover no-repeat fixed;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}
@media (min-width:1201) {
#wrapper {
background-image: url(https://via.placeholder.com/1300x900);
}
}
@media (max-width:1200px) and (min-width:901) {
#wrapper{
background-image: url(https://via.placeholder.com/900x750);
}
}
@media (max-width:900px) and (min-width:601) {
#wrapper {
background-image: url(https://via.placeholder.com/800x650);
}
}
/* For mobile devices */
@media only screen and (max-width: 600px) {
#wrapper{
background-image: url(https://via.placeholder.com/500x405);
}
}
<div id="wrapper">
</div
解决方案
检查 HTML 会发现您的background
属性无效。尝试
#wrapper{
/* mobile first this image and move to wherever you intended this one */
background: url(https://via.placeholder.com/1500x1000) center center no-repeat;
background-size: cover;
background-attachment: fixed;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}
您的其余查询可以使用一些清理
/* For mobile devices */
@media only screen and (max-width: 600px) {
#wrapper{
background-image: url(https://via.placeholder.com/500x405);
}
}
@media (min-width:601px) {
#wrapper {
background-image: url(https://via.placeholder.com/800x650);
}
}
@media (min-width:901px) {
#wrapper{
background-image: url(https://via.placeholder.com/900x750);
}
}
@media (min-width:1201px) {
#wrapper {
background-image: url(https://via.placeholder.com/1300x900);
}
}
推荐阅读
- jquery - 使用 jQuery slideDown 或 toggleClass 和 css 过渡添加向下滑动效果
- json - 带有可选成员的 Kotlin 类
- git - git-tag 仅列出当前 HEAD 或以下的那些标签
- javascript - 在map函数中使用三元显示两个选项
- jmeter - 如何使用 Jmeter 测试没有 Varnish 的页面的加载时间
- vba - 将范围乘以当月剩余天数的百分比
- search - 如何根据同一网站上另一个页面的内容在一个页面上搜索关键字?
- angular - 如何为角度前端本地的后端服务器提供服务?
- opennms - 在 OpenNMS 中追踪“打开的文件过多”的原因
- django - 在 django url 中将时间作为参数传递