我需要将背景图定位在屏幕底部。但是在移动设备的分辨率上,我想将它显示在底部以下100像素,就像“底部减去100像素”。在CSS中是否可以实现这样的效果?
是的,使用background-position
属性的四值语法可以实现。
表述
// This two-value declaration
background-position: center bottom
// Is equivalent of this four-value declaration
background-position: center 0px bottom 0px
免责声明:支持的浏览器包括Chrome 25+,Firefox 13+,IE9+,Safari 7+
在您的情况下
因此,您可以将背景定位为“底部减100像素”:
background-position: center bottom -100px
示例:
.container {
height: 190px;
padding: 1em;
margin: 0 auto 1em;
border: 1px solid #333;
text-align: center;
background-image: url('http://lorempixel.com/200/140/city/4');
background-repeat: no-repeat;
background-position: center bottom;
}
.plus {
background-position: center bottom 20px;
}
.minus {
background-position: center bottom -20px;
}
<div class="container">
background-position:<br>
center bottom
</div>
<div class="container plus">
background-position:<br>
center bottom 20px
</div>
<div class="container minus">
background-position:<br>
center bottom -20px
</div>
查看MDN 文档
center 0px bottom 0px
:您无法定义“距中心100像素”(到哪里?)。它只适用于 top
、bottom
、left
和 right
。 - Cladiuss.background{
background-position: center bottom -100px
}
.background{
margin-bottom -100px;
}
或者
.background{
bottom: -100px;
position: absolute;
}