How to shift a background image with css

隐身守侯 提交于 2020-01-10 11:51:04

问题


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?


回答1:


You can use the background-position:

background-position: 8px 8px;



回答2:


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;




回答3:


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.

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

background-origin: content-box;

Documentation / Simple Demo @ MDN



来源:https://stackoverflow.com/questions/1175557/how-to-shift-a-background-image-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!