如何使用CSS移动背景图像

发布于 2024-07-29 14:54:48 字数 96 浏览 3 评论 0原文

我想在有背景图像的桌子周围放置边框。 边框工作正常,但是当我这样做时(它是 8px 边框),背景图像会被边框截断。 我可以将背景图像向右移动 8 像素,向下移动 8 像素吗?

I wanted to put a border around a table which has a background image. The border works fine, but when I do this (it is an 8px border) the background image gets cut off by the border. Am I able to shift the background image to start 8px to the right and 8px down?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月打碎记忆 2024-08-05 14:55:09

您不应手动将图像移动 8px,而应将图像锚定到内边距框(下图中的绿色)而不是边框​​框(黄色) )。 这样做会将图像的左上角放置在边框内部而不是边框​​后面。

background-origin: padding-box;

这将使维护更容易,因为即使您更改边框宽度它仍然可以工作。

盒子模型

您还可以将背景的原点设置为内容框(蓝色):

background-origin: content-box;

文档/简单演示 @ MDN

Rather than manually shifting the image by 8px, you should just anchor the image to the padding box (green in the diagram below) instead of the border box (yellow). Doing this will place the top-left corner of the image inside of the border instead of behind it.

background-origin: padding-box;

This will make maintenance easier since it will still work even if you change the border width.

box model

You can also set the background's origin to the content box (blue):

background-origin: content-box;

Documentation / Simple Demo @ MDN

扛刀软妹 2024-08-05 14:55:04

你也可以使用一些速记。
背景:<颜色> <图片地址> <重复> <左> <顶部>

对于你来说,我会想到这样的事情:
背景:透明 url()无重复 8px 8px 滚动;

you could also use some short hand.
background: <colour> <image url> <repeat> <left> <top> <scroll>

for yours i'd be thinking something like:
background : transparent url(<location to img>) no-repeat 8px 8px scroll;

十秒萌定你 2024-08-05 14:54:58

您可以使用背景位置:

background-position: 8px 8px;

更多信息: https: //developer.mozilla.org/en-US/docs/Web/CSS/background-position

You can use the background-position:

background-position: 8px 8px;

More on that: https://developer.mozilla.org/en-US/docs/Web/CSS/background-position

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文