Can a background image be larger than the div itself?

前端 未结 9 1291
天命终不由人
天命终不由人 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条回答
  • 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.

    0 讨论(0)
  • 2020-12-12 20:54

    Not really - the background image is bounded by the element it's applied to, and the overflow properties only apply to the content (i.e. markup) within an element.

    You can add another div into your footer div and apply the background image to that, though, and have that overflow instead.

    0 讨论(0)
  • 2020-12-12 20:57

    This could help. It requires the footer height to be a fixed number. Basically, you have a div inside the footer div with it's normal content, with position: absolute, and then the image with position: relative, a negative z-index so it stays "below" everything, and a negative top value of the footer's height minus the image height (in my example, 50px - 600px = -550px). Tested in Chrome 8, FireFox 3.6 and IE 9.

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