html - How to get an image to appear in a header, fully sized when the screen is wide enough but centered when the screen is too thin?
问题描述
I have an image I am using in the header of a website, and on a computer the image takes up the whole width as intended. When the site is displayed on mobile devices however, it scales down and only shows the far left side of the image.
.backimage {
padding:20px;
background-image:url(background.jpeg);
width:100%;
height:35%;
background-repeat:no-repeat;
background-size:cover;
}
Is there some code I can add to center the image when the width is too small for the whole image? Thanks, if more information is needed I will provide.
解决方案
使用@mediaquery
@media only screen and (max-width: 600px) {
.backimage {
padding:20px;
background-image:url(background.jpeg);
width:50%;
height:35%;
margin-left: 0px;
background-repeat:no-repeat;
background-size:cover;
}
}
如果浏览器窗口为 600 像素或更小,则图像宽度将为 50%。
如果您想要不同尺寸的不同样式,您可以使用
@media only screen and (max-width: 900px) {
.backimage {
padding:20px;
background-image:url(background.jpeg);
width:70%;
height:35%;
margin-left: 0px;
background-repeat:no-repeat;
background-size:cover;
}
}
如果浏览器窗口为 900 像素或更小,则图像宽度将为 70%。
推荐阅读
- html - Bootstrap4 导航链接活动顶部边框样式
- windows - 如何使用 vb.net 计算一天的总收入?
- c - 如何使用 qsort 对结构进行排序
- c# - System.IO.FileStream 不包含“ReadLine”的定义,也不包含“ReadLine”类型的扩展方法。您是否缺少程序集参考?
- javascript - 用javascript替换图像src
- python - OAuth 2.0 连接由对等方重置
- python - 在 Python 中使用 groupby 计算平均差异
- wordpress - 如何更改 phpmyadmin 的密码
- django - 错误:没有名为消息的模块 Django 1.1.3
- bash - 如何在出现任何错误时自动从源 bash 脚本返回?