Can a background image be larger than the div itself?

前端 未结 9 1299
天命终不由人
天命终不由人 2020-12-12 20:37

I have a footer div with 100% width. It\'s about 50px high, depending on its content.

Is it possible to give that #footer a background image that kind of overflows t

9条回答
  •  -上瘾入骨i
    2020-12-12 20:54

    No, you can't.

    But as a solid workaround, I would suggest to classify that first div as position:relative and use div::before to create an underlying element containing your image. Classified as position:absolute you can move it anywhere relative to your initial div.

    Don't forget to add content to that new element. Here's some example:

    div {
      position: relative;
    }    
    
    div::before {
      content: ""; /* empty but necessary */
      position: absolute;
      background: ...
    }
    

    Note: if you want it to be 'on top' of the parent div, use div::after instead.

提交回复
热议问题