Background images: how to fill whole div if image is small and vice versa
Solution 1
Resize the image to fit the div size.
With CSS3 you can do this:
/* with CSS 3 */
#yourdiv {
background: url('bgimage.jpg') no-repeat;
background-size: 100%;
}
How Do you Stretch a Background Image in a Web Page:
About opacity
#yourdiv {
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */
}
Or look at CSS Image Opacity / Transparency
Solution 2
To automatically enlarge the image and cover the entire div section without leaving any part of it unfilled, use:
background-size: cover;
Solution 3
This worked perfectly for me
background-repeat: no-repeat;
background-size: 100% 100%;
Solution 4
Try below code segment, I've tried it myself before :
#your-div {
background: url("your-image-link") no-repeat;
background-size: cover;
background-clip: border-box;
}
Solution 5
Rather than giving background-size:100%;
We can give background-size:contain;
Check out this for different options avaliable: http://www.css3.info/preview/background-size/
Aakash Sahai
Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live.
Updated on July 08, 2022Comments
-
Aakash Sahai almost 2 years
I have three problems:
When I tried to use a background image in a smaller size div, the div shows only part of image. How can I show the full or a specific part of image?
I have a smaller image and I want to use in a bigger div. But don't want to use repeat function.
Is there any way in CSS to manipulate the opacity of an image?
-
Aakash Sahai over 13 yearsbut this will create burden over server to load multiple images(which is similar,as v know)..so instead using multiple images,using one image and strechg n collasping is a better way..but dnt know how to it..
-
Sondre over 13 yearsPoint 1 and 2 isn't solutions to this problem. First point is for keeping aspect-ratio on a picture, second point don't seem to work at all. With CSS3 the background-size property will fix this problem, until then you gotta stick with some hack I guess..
-
Bazzz over 13 yearserr, formatting of my post didn't work? I used the toolbar icon... :(
-
Aakash Sahai over 13 yearsi tried it earlier and nw also..if image size is 100px 100px and div has 400px 400px size then it is fixed at the top corner.itz not strechg..background-size not working too..using ie8, firefox,chrome,safari latest browsers
-
Bazzz over 13 yearsHere is an example: jsfiddle.net/9mawE/1 The div is 150px wide. The Google logo is more than 150px wide but is scaled down to fit in the div. The Google Earth icon is less than 150px wide and is scaled up to fit the 150px wide div, Background-size doesn't work in IE 8 because it is CSS 3 which is still unsupported.
-
KarSho about 11 yearsIE not support this(bg-size). Wt to do?
-
Kevin Brown-Silva almost 9 yearsWelcome to Stack Overflow! While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.
-
Ruslan Abuzant about 8 yearsSmallest comment on this page and biggest benefit in my case. Thanks
-
user1709076 about 8 yearsdoes not work for me but my div is inside a <th> inside a <tr> inside a <table> . dont know if that is why
-
Ouadie about 8 yearscould you plz create a jsfiddle for your example ? so we can help you out
-
Alex Rogachevsky over 7 yearsThanks. Was looking specifically for that. Did it years ago and forgot.
-
webs over 6 yearscontain is not covering whole div. So cover seem to be what op is looking for.
-
Boris Gafurov about 5 yearsthis one fits background image both width and height, exactly what is needed.