如何在位置固定的情况下在中心设置图像
这个问题在这里已经有了答案:
对于可变宽度/高度的内容,您需要使用带有转换的百分比偏移量,如下所示:
.bgimg {
top: 50%;
left: 50%;
position: fixed;
opacity:0.09;
transform: translate(-50%, -50%);
}
或者,如果您知道宽度和高度,则可以避免使用变换并将所有位置设置为0
与margin: auto;
配对margin: auto;
:
.bgimg {
width: 400px;
height: 400px;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
您可以在下面看到两种方法的实际应用!
.bgimg {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: .5;
}
/* you need to set the width and height on this one, otherwise it stretches it to fill */
.center-something-without-transform {
width: 50px;
height: 50px;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
background-color: blue;
}
<img class="bgimg" src="http://placekitten.com.s3.amazonaws.com/homepage-samples/200/287.jpg" />
<div class="centered-without-transform"></div>
链接地址: http://www.djcxy.com/p/75797.html
上一篇: How to set image in center with position fixed
下一篇: Center header element with position fixed to top of page