css 背景图片居中显示

background 简写

background: #ffffff url('https://profile-avatar.cn/b9a.jpg') center center / 100% no-repeat;

效果如图:
在这里插入图片描述

CSS 中让 `<caption>` 元素的背景图片居中,可借助 `background-position` 属性达成。`background-position` 属性用来设定背景图片在元素内的初始位置,将其值设为 `center` 就能使背景图片居中显示。 以下是示例代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table { border: 1px solid #000; border-collapse: collapse; margin: 0 auto; } td, th { font-size: 32px; padding: 50px; border: 1px solid #000; } caption { font-size: 30px; font-weight: 700; margin-bottom: 30px; /* 设置背景图片 */ background-image: url('your-image-url.jpg'); /* 使背景图片不重复 */ background-repeat: no-repeat; /* 让背景图片居中 */ background-position: center; /* 设置背景图片大小 */ background-size: contain; height: 200px; } </style> </head> <body> <table> <caption>我是一个表格</caption> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> </tbody> </table> </body> </html> ``` 在上述代码里,`background-position: center;` 把背景图片在 `<caption>` 元素中水平和垂直都居中显示。同时,`background-repeat: no-repeat;` 避免背景图片重复,`background-size: contain;` 保证背景图片完整显示在 `<caption>` 元素内。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值