记录background-position的使用方法

开发时遇到了一个样式请添加图片描述
箭头要距离右边10像素,但是之前只用过background-position: right center;这种,不能满足需求。

除了使用::after/before伪类定位到这个样子,还有什么方法呢?

经查询后,得知,原来background-position还可以这么写

.a {
	background-position: top 10px right 10px;
	background-position: bottom 10px left 10px;
}

这个的意思就是,距离某一边多少距离,完美契合我的需求。

但是,这种写法的兼容性并不好,很多浏览器还不支持。如果不考虑 IE8,可以采用这种方式。如果要考虑 IE8 的话,要么给容器定宽,计算出具体的 x 和 y 的值,要么,使用百分比来大约的定位。

注意:
background-position 如果要写10px 10px的话,第一个值是x轴距离左边的距离,第二个是y轴距离顶部的距离。

center 10px 表示:x轴居中,距离top 10像素
center; 表示上下左右都居中

center 10px center 10px是不能使用的!这是一个错误的语法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE html> <html> <head> <title>智能客服</title> <style> #call-btn { position: fixed; bottom: 0; right: 0; background-color: #007bff; color: #fff; border: none; padding: 10px 20px; } </style> <script src="https://cdn.bootcss.com/jquery/3.5.1/jquery.min.js"></script> <script> function toggleWidget() { var widget = document.getElementById("widget"); $(this).html(widget.style.display === "none" ? "收起" : "呼出"); widget.style.display = widget.style.display === "none" ? "block" : "none"; } </script> <title>智能客服</title> <style> .chat-window { position: fixed; bottom: 20px; right: 20px; width: 300px; height: 400px; background-color: #fff; border: 1px solid #ccc; border-radius: 10px; overflow: hidden; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); font-family: Arial, sans-serif; } .chat-window-header { background-color: #f5f6f7; padding: 10px; border-bottom: 1px solid #ccc; font-weight: bold; font-size: 16px; } .chat-window-body { padding: 10px; height: 300px; overflow-y: scroll; } .chat-window-input { padding: 10px; border-top: 1px solid #ccc; } .chat-window-input input[type="text"] { width: 100%; border: none; font-size: 14px; padding: 5px; border-radius: 5px; box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1); } .chat-window-input input[type="submit"] { display: none; } .chat-message { margin-bottom: 10px; padding: 10px; background-color: #f5f6f7; border-radius: 5px; box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1); } .chat-message-user { background-color: #d1f2eb; text-align: right; } .chat-message-bot { background-color: #f5f6f7; text-align: left; } </style> </head>记住这段代码
最新发布
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值