How do i resize a background image of an element? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do i resize a background image of an element?

div { width: 100%; height: 400px; background-image: url(_theURL_); } th image is 1920×1080

7th May 2017, 3:13 PM
rohan_m
rohan_m - avatar
10 Answers
+ 6
The 'background-size' property can be set with two useful special values: - 'cover': fit the size of the background image to cover the whole surface, not stretching ( keep aspect ratio ) but cropping if necessary. - 'contain': fit the size of the background image to be contain entirely in the element, not stretching, leaving space not cover ( or covered with background image repetition if set ).
7th May 2017, 7:28 PM
visph
visph - avatar
+ 8
just tested div{ width:100%; height:40px; background-image:url("https://images.template.net/wp-content/uploads/2014/12/Colorful-Aquarium-Background.jpg"); background-size:100% 40px; } this will strech the image so it fits the div (width/ height defined the same as in background-size) another mystery solved Watson dear
7th May 2017, 3:34 PM
Burey
Burey - avatar
+ 7
try background-size perhaps (if you are trying to scale the image to fit the window size) https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Background_and_Borders/Scaling_background_images
7th May 2017, 3:21 PM
Burey
Burey - avatar
+ 5
and please update if it works xD
7th May 2017, 3:27 PM
Burey
Burey - avatar
+ 4
In that case, you cannot put width:100% :) Put samo value in px, i don't know, what you width need, you will see :)
7th May 2017, 3:20 PM
Nikola Stojiljkovic
Nikola Stojiljkovic - avatar
+ 4
no problemo
7th May 2017, 3:36 PM
Burey
Burey - avatar
+ 3
ok :) try what @Burey says :)
7th May 2017, 3:25 PM
Nikola Stojiljkovic
Nikola Stojiljkovic - avatar
+ 1
@nikola but i want the image to cover the whole width of the page....?
7th May 2017, 3:23 PM
rohan_m
rohan_m - avatar
+ 1
@burey thanks a lot
7th May 2017, 3:35 PM
rohan_m
rohan_m - avatar
+ 1
@burey thanks again...it worked fine
7th May 2017, 3:37 PM
rohan_m
rohan_m - avatar