How to shift a background image with css

前端 未结 3 1819
悲哀的现实
悲哀的现实 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:13

    You can use the background-position:

    background-position: 8px 8px;
    
    0 讨论(0)
  • 2020-12-30 20:26

    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;

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题