Can a background image be larger than the div itself?
I do not believe that you can make a background image overflow its div. Images placed in Image tags can overflow their parent div, but background images are limited by the div for which they are the background.
Why does my background image not fit my div container
The scrollbar has nothing to do with the background image, not even with the element which has the background image. (Btw., a background image can never affect box sizing.)
The .main-con element has a width of 100% and a margin of 160px. The 100% does not refer to the remaining space; it refers to the width of the parent element, in this case the body.
So the .main-con element is as big as the body/screen, but has a 160px distance from the left side, resulting in it overflowing 160px on the right side.
One way to counteract this without completly reimplementing your website would be to set the width of the .main-con element to calc(100% - 160px).
Force div to have the size of background image
Well, the reason why this is?
In your working example you use content. A content has it's own height and uses up space, which the other elements on the page have to respect.
With the background-image
solution, you use a background, which does not use space. The .imageContainer2
element can not know about the height of the background-image AND adapt itself to it.
This very problem was addressed here: How to get div height to auto-adjust to background size?
Just check, if the workaround is suitable for you
Scale background image width to the size of the div
Background size will work, but the aspect ratio of the image will be changed. 500px below is the known height.
.myDiv {
background-image: url(image.png);
background-repeat: no-repeat;
background-size: 100% 500px;
}
More info on background-size here, it's not supported on <= IE8.
Or alternatively you could absolutely position an image relative to the container, but background-size is better.
img.cover {
width:100%;
height:100%;
position:absolute:
top:0;
left:0;
right:0;
bottom:0;
z-index:-1;
}
How to get div height to auto-adjust to background size?
Another, perhaps inefficient, solution would be to include the image under an img
element set to visibility: hidden;
. Then make the background-image
of the surrounding div the same as the image.
This will set the surrounding div to the size of the image in the img
element but display it as a background.
<div style="background-image: url(http://your-image.jpg);">
<img src="http://your-image.jpg" style="visibility: hidden;" />
</div>
How to set a background image in full size of a div that doesn't fill page
Give
position:relative;
in the container div..It should work..
And the background div(with position:absolute;
) should be a child of container div..then only u'll get the div overlapping property
How to have a background image wider than the content area of a website (without scrollbars)
Set the min-width on the div containing the background image to the width of the content.
Background image aligned at bottom even when resized
Does this work for you?
#container {
width: 100%;
height: 300px;
background: url("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg");
position: relative;
background-size: contain;
background-position: bottom;
background-repeat: no-repeat;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
background: #333;
opacity: 0.5;
}
#container {
width: 100%;
height: 300px;
background: url("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg");
position: relative;
background-size: contain;
background-position: bottom;
background-repeat: no-repeat;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
background: #333;
opacity: 0.5;
}
<div id="container">
<div class="overlay"></div>
</div>
Related Topics
Label Next to Selectinput in Shiny
Svg + CSS3 Animation Not Working with Link Markup
CSS - CSS Coding That I Have Never Seen
Why Doesn't My Child Element Inherit Color from Its Parent When Parent Has More Specific Selector
Flex Item with Image Child Doesn't Adjust Its Size Properly
How Add Class to Parent in Angular Application
Maintain Aspect Ratio of a Div According to Height
Remove 3D Press Effect Internet Explorer Button
Display:Block Not Working in Chrome or Safari
How to Change a Background Image Opacity Without Changing on Div Content
How to Revert Back to Normal After Display:None for Table Row
How to Make Background-Image with Linear-Gradient Work on Ie 11
How to Specify Font Attributes for All Elements on an HTML Web Page