HTML background image offset by x pixels from the center

后端 未结 9 1326

I\'d like to put an image as the background of a webpage but have it offset by some number of pixels with respect to the center.

How can I do this?

I want:

9条回答
  •  礼貌的吻别
    2020-12-23 09:38

    if you know the width of the image you can use this:

    background-position: (BgWidth - 50)px 0px;
    

    Note that you can't have it like that, i.e. you need to calculate (BgWidth - 50) and then write the number there.

    If you don't know the width you can use Javascript(with-or-without jQuery) and then use this:

    $(#ID).css('background-position', (BgWidth - 50)+'px 0px');
    

提交回复
热议问题