Html DIV 垂直中心上的 CSS 背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5676197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:01:02  来源:igfitidea点击:

CSS background image on DIV's vertical center

htmlcss

提问by Satch3000

I have a div and I need to get a small background image into the vertical center of that DIV using CSS.

我有一个 div,我需要使用 CSS 将一个小背景图像放入该 DIV 的垂直中心。

Can anyone help please?

有人可以帮忙吗?

Thanks.

谢谢。

回答by sandeep

To vertically center:

垂直居中:

#con {
    background: url(image.jpg) no-repeat left center;
}

To horizontally center:

水平居中:

#con {
    background: url(image.jpg) no-repeat center top;
}

回答by clairesuzy

#somediv {
background: url (image.jpg) no-repeat 50% 50%;
}

the 50% centers it horizontally and vertically.. alternatively you can use center center

50% 将它水平和垂直居中.. 或者你可以使用 center center