How to shift a background image with css

前端 未结 3 1834
悲哀的现实
悲哀的现实 2020-12-30 19:32

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 th

3条回答
  •  误落风尘
    2020-12-30 20:30

    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

提交回复
热议问题