css - 如何在CSS中从右侧设置固定背景位置?

标签 css

<分区>

在这种情况下

li {background: url("../img/grey-arrow-next.png") no-repeat right center;
    border-bottom: 1px solid #D4E8EB}

我想在背景图像之前在右侧留出 20px 空间,我不能在 li 上留边距,因为 border 应该触及边缘.

所以我需要设置 20px 但它需要 20px 从左侧而不是右侧。

li {background: url("../img/grey-arrow-next.png") no-repeat 20px center;
        border-bottom: 1px solid #D4E8EB}

最佳答案

在你的 css 中提到位置然后是“间距值(50px)”然后是其他位置(中心/顶部)

li {background: url("../img/grey-arrow-next.png") no-repeat right 50px center;
border-bottom: 1px solid #D4E8EB}

旧版浏览器和IE8及旧版IE不支持此代码。最新更新的浏览器与此方法没有冲突,希望 future 的更新也能支持。

如果您使用的是现代浏览器,请按照另一个答案中的建议尝试 background-position: calc(100% - 50px) center;calc 还有很长的路要走,因为它在逻辑和数学上能够产生非常准确的结果。

关于css - 如何在CSS中从右侧设置固定背景位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5731520/

相关文章:

html - 为什么页面宽度会影响 Firefox 中的 "margin-top:50%"?

css - 创建一个 "Google Analytics"点赞站点覆盖

html - iFrame 未加载 IE 8

javascript - 如何向 JQuery Select2 插件添加复选框

css - 在 Mac 上测试 IE 的最佳工具 - 无需 PC

javascript - JQuery slider 不能正常用于下一张幻灯片(.next() 有错误)

html - 如何在 css 中创建叠加层?

html - 打开关闭sidenav相同的按钮

css - 页面标题消失在正文后面

javascript - Bootstrap 样式化 JQuery/Java 弹出框