HTML background image offset by x pixels from the center

后端 未结 9 1372

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:44

    Nice answer Luke,

    one more thing, if your block width is larger than screen resolution, your must put your block in another container and do this:

    #container{
      position:relative;
      overflow:hidden;
    }
    
    #shadowBox{
      width: 100%;
      left: -100px; /* this must be TWICE the required offset distance*/
      padding-right: 100px; /* must be the same amount as above */
      background-image: url("background-image.png");
      background-position: center;
      background-repeat: no-repeat;
      position:absolute: /*this is needed*/
    }
    

提交回复
热议问题