css怎样让背景图片居中,CSS让背景图片居中显示的方法

本文介绍了如何使用CSS来精确控制背景图片的位置,包括水平垂直居中、顶部水平居中及其它定位方式。通过不同的属性值组合,实现背景图在不同位置的显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们的背景图片写成:

background: url("images/titleborder.png") no-repeat;

时,表示背景图片不重复,位置会默认为在div中居于左上角。

例:

#header {

width: 100%;

height: 400px;

border: 1px solid;

background: url("images/titleborder.png") no-repeat;

}

效果:

974f392230b6?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.png

如果背景图片写成:

background: url("images/titleborder.png") center center no-repeat;

效果是水平垂直居中对齐:

974f392230b6?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

2.png

第一个center表示水平居中,第二个center表示垂直居中。

要令背景图片水平居中,并贴近div顶部,代码可以这么写:

background: url("images/titleborder.png") center 0 no-repeat;

或者:

background: url("images/titleborder.png") center top no-repeat;

效果:

令背景图片居于右上角的写法:

background: url("images/titleborder.png") right top no-repeat;

效果:

左居中:

background: url("images/titleborder.png") left center no-repeat;

974f392230b6?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

P9_3E4$9[Q4F0EL}9%]R32A.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值