I'm trying to figure out how to make a background-image in a div full width and responsive. The background-image is expanding across the width of the page (and is responsive), but the height of the image isn't its full height. It seems like it's being cut-off somehow. I'm using bootstrap framework, and the reason I'm trying to do this is I want to overlay some text on the image. I've tried so many different things but cant seem to figure it out, help!
<div class="bg-image">
<div class="container">
<div class="row-fluid">
<div class="span12">
<h1>This is some text</h1>
</div>
</div>
</div>
</div>
.bg-image {
background: url(img/image.jpg) no-repeat center top;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
max-height: 450px;
}
Here is one way of getting the design that you want.
Start with the following HTML:
Note that the background image is now part of the regular flow of the document.
Apply the following CSS:
How This Works
The image is set an regular flow content with a width of 100%, so it will adjust itself responsively to the width of the parent container. However, you want the height to be no more than 450px, which corresponds to the image width of 1200px, so set the maximum width of the image to 1200px. You can keep the image centered by using
display: block
andmargin: 0 auto
.The text is painted over the image by using absolute positioning. In the simplest case, I stretch the
h1
element to be the full width of the parent and usetext-align: center
to center the text. Use the top or bottom offsets to place the text where it is needed.If your banner images are going to vary in aspect ratio, you will need to adjust the maximum width value for
.bg-image img
dynamically using jQuery/Javascript, but otherwise, this approach has a lot to offer.See demo at: http://jsfiddle.net/audetwebdesign/EGgaN/
I also tried this style for ionic hybrid app background. this is also having style for background blur effect.
}
When you use
background-size: cover
the background image will automatically be stretched to cover the entire container. Aspect ratio is maintained however, so you will always lose part of the image, unless the aspect ratio of the image and the element it is applied to are identical.I see two ways you could solve this:
Do not maintain the aspect ratio of the image by setting
background-size: 100% 100%
This will also make the image cover the entire container, but the ratio will not be maintained. Disadvantage is that this distorts your image, and therefore may look very weird, depending on the image. With the image you are using in the fiddle, I think you could get away with it though.You could also calculate and set the height of the element with javascript, based on its width, so it gets the same ratio as the image. This calculation would have to be done on load and on resize. It should be easy enough with a few lines of code (feel free to ask if you want an example). Disadvantage of this method is that your width may become very small (on mobile devices), and therfore the calculated height also, which may cause the content of the container to overflow. This could be solved by changing the size of the content as well or something, but it adds some complexity to the solution/