Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can background image extend beyond div's borders?

Tags:

html

css

image

Can background image extend beyond div's borders? Does overflow: visible apply to this?

like image 454
Yarin Avatar asked Oct 29 '10 22:10

Yarin


People also ask

How do I make a background image fit the whole screen CSS?

CSS-Only Technique #1 We set a min-height which keeps it filling the browser window vertically, and set a 100% width which keeps it filling horizontally. We also set a min-width of the width of the image so that the image never gets smaller than it actually is.

How do you extend a background in HTML?

The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover.

Can object fit in background image?

Adding a Background to an Image With object-fit: contain # We would benefit from that when also using object-fit: contain . In the example below, we have a grid of images. When the aspect ratios of the image and the container are different, the background color will appear.


2 Answers

No, a background can't go beyond the edge of an element.

The overflow style controls how the element reacts when the content is larger than the specified size of the element.

However, a floating element inside the div can extent outside the div, and that element could have a background. The usefulness of that is limited, though, as IE7 and earlier has a bug that causes the div to grow instead of letting the floating element show outside it.

like image 186
Guffa Avatar answered Nov 07 '22 13:11

Guffa


Following up on kijin's advice, I'd like to share my solution for image offsets:

/**    * Only effective cross-browser method to offset image out of bounds of container AFAIK,    * is to set as background image on div and apply matching margin/padding offsets:   */  #logo {   margin:-50px auto 0 auto;   padding:50px 0 0 0;   width:200px;   height:200px;   background:url(../images/logo.png) no-repeat; } 

I used this example on a simple div element <div id="logo"></div> to position my logo with a -50px vertical offset. (Note that the combined margin/padding settings ensure you don't run into collapsing margin issues.)

like image 24
Yarin Avatar answered Nov 07 '22 12:11

Yarin