CSS背景图像在DIV的垂直中心

时间:2021-10-31 21:27:17

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.

谢谢。

2 个解决方案

#1


40  

To vertically center:

垂直中心:

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

To horizontally center:

横向中心:

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

#2


9  

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

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

50%是水平和垂直居中。你也可以使用中心

#1


40  

To vertically center:

垂直中心:

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

To horizontally center:

横向中心:

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

#2


9  

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

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

50%是水平和垂直居中。你也可以使用中心