使背景图像响应
making background images responsive
我正在编写一个即将推出的页面,它在桌面模式下运行良好!但在移动设备上,图像的一些艺术作品被隐藏了,
在移动视图中,它看起来像这样:
您可能会注意到,在移动设备视图中,它隐藏了部分艺术品
但在桌面模式下效果很好:
这是我的完整片段:
<!DOCTYPE html>
<html>
<head>
<title>HOME: </title>
</head>
<style>
body, html {
height: 100%;
margin: 0;
}
.bgimg {
background-image: url('bg12.png');
height: 100%;
max-width: 100%;
background-position: center;
background-size: cover;
position: relative;
color: black;
font-family: KhmerUI;
src: url(KhmerUI.ttf);
font-size: 25px;
}
.topleft {
position: absolute;
top: 0;
left: 16px;
}
.bottomright {
position: absolute;
top: 0;
right: 30px;
}
.middle {
position: absolute;
top: 50%;
left: 70%;
transform: translate(-50%, -50%);
text-align: center;
}
hr {
margin: auto;
width: 40%;
}
</style>
<body>
<div class="bgimg">
<div class="topleft">
<img src="loDgo-2.png" alt="logo" style="width:150px;height:120px;">
</div>
<div class="middle">
<h1>COMING SOON</h1>
<hr>
<p>STAY TUNED</p>
</div>
<div class="bottomright">
<p>Powered By</p>
<img src="cropD-3.png" style="width:150px;height:50px;">
<div></div>
</div>
</div>
</body>
</html>
这是我的背景图片:
任何人都可以帮我修复移动视图,我需要图像的艺术作品应该看起来像我看到的桌面模式。作品无法隐藏
一种解决方案是将样式更新为:
.bgimg {
background-image: url('bg12.png');
height: 100%;
max-width: 100%;
background-position: center;
background-size: contain;
background-repeat: no-repeat;
position: relative;
color: black;
font-family: KhmerUI;
src: url(KhmerUI.ttf);
font-size: 25px;
}
为了进一步自定义图像的位置,您使用媒体查询在 background-position
上尝试不同的选项
在此处查看更多信息:
CSS background-size cover and background-position
这里:
https://www.w3schools.com/cssref/pr_background-position.asp
试试这个
img {
max-width: 100%;
display:block;
height: auto;
}
示例 jsFiddle(调整浏览器大小以查看响应能力)
您可以尝试以下 CSS。
img {
width : 100%;
height : auto
}
希望这能帮助您解决问题。
Link to codepen for the same as I could not add image as background in here.
.background{
background-image:url('image url');
background-repeat:no-repeat;
background-position:50%;
background-size:100%;
width:calc(100vw - 1em);
height:calc(100vh - 1em);
display:block;
position:relative;
}
h1{
margin:0;
display:inline-block;
padding:0;
text-align:center;
transform:translate(10em,10em);
color:white;
position:relative;
}
<div class="background">
<h1>Coming Soon</h1>
</div>
很高兴您正在使用 background-size: cover;
,因为它在良好的编码实践中更可取。但要根据屏幕尺寸调整图片大小,请使用 background-size: 100%;
我正在编写一个即将推出的页面,它在桌面模式下运行良好!但在移动设备上,图像的一些艺术作品被隐藏了,
在移动视图中,它看起来像这样:
您可能会注意到,在移动设备视图中,它隐藏了部分艺术品
但在桌面模式下效果很好:
这是我的完整片段:
<!DOCTYPE html>
<html>
<head>
<title>HOME: </title>
</head>
<style>
body, html {
height: 100%;
margin: 0;
}
.bgimg {
background-image: url('bg12.png');
height: 100%;
max-width: 100%;
background-position: center;
background-size: cover;
position: relative;
color: black;
font-family: KhmerUI;
src: url(KhmerUI.ttf);
font-size: 25px;
}
.topleft {
position: absolute;
top: 0;
left: 16px;
}
.bottomright {
position: absolute;
top: 0;
right: 30px;
}
.middle {
position: absolute;
top: 50%;
left: 70%;
transform: translate(-50%, -50%);
text-align: center;
}
hr {
margin: auto;
width: 40%;
}
</style>
<body>
<div class="bgimg">
<div class="topleft">
<img src="loDgo-2.png" alt="logo" style="width:150px;height:120px;">
</div>
<div class="middle">
<h1>COMING SOON</h1>
<hr>
<p>STAY TUNED</p>
</div>
<div class="bottomright">
<p>Powered By</p>
<img src="cropD-3.png" style="width:150px;height:50px;">
<div></div>
</div>
</div>
</body>
</html>
这是我的背景图片:
任何人都可以帮我修复移动视图,我需要图像的艺术作品应该看起来像我看到的桌面模式。作品无法隐藏
一种解决方案是将样式更新为:
.bgimg {
background-image: url('bg12.png');
height: 100%;
max-width: 100%;
background-position: center;
background-size: contain;
background-repeat: no-repeat;
position: relative;
color: black;
font-family: KhmerUI;
src: url(KhmerUI.ttf);
font-size: 25px;
}
为了进一步自定义图像的位置,您使用媒体查询在 background-position
在此处查看更多信息: CSS background-size cover and background-position
这里: https://www.w3schools.com/cssref/pr_background-position.asp
试试这个
img {
max-width: 100%;
display:block;
height: auto;
}
示例 jsFiddle(调整浏览器大小以查看响应能力)
您可以尝试以下 CSS。
img {
width : 100%;
height : auto
}
希望这能帮助您解决问题。
Link to codepen for the same as I could not add image as background in here.
.background{
background-image:url('image url');
background-repeat:no-repeat;
background-position:50%;
background-size:100%;
width:calc(100vw - 1em);
height:calc(100vh - 1em);
display:block;
position:relative;
}
h1{
margin:0;
display:inline-block;
padding:0;
text-align:center;
transform:translate(10em,10em);
color:white;
position:relative;
}
<div class="background">
<h1>Coming Soon</h1>
</div>
很高兴您正在使用 background-size: cover;
,因为它在良好的编码实践中更可取。但要根据屏幕尺寸调整图片大小,请使用 background-size: 100%;