@elaineleung
Posted
Hi Sef, well done in completing your second challenge!
About your question, height is generally not needed, but because you're using background-image
for the image, then yes, you would definitely need height in the mobile view if there isn't another container right next to it (to give it the height) or if there is no content itself inside the container. For good practice, I would suggest that you use img
instead because background-image
is meant for an image used as a background, mainly as decoration. Since this is a product image, it means this is important content and not background decoration at all. You'd always want the main content to be part of the HTML, as this can help screen reader users.
If you want to try using a responsive img
instead where you can put two source images inside an img
, you can use this:
// HTML
<img alt="Glass perfume bottle placed against a leafy background"
srcset="images/image-product-desktop.jpg 600w, images/image-product-mobile.jpg 686w"
sizes="(max-width: 900px) 686px, 600px"
src="images/image-product-desktop.jpg" >
// CSS:
.product {
// remove all background properties
}
img {
max-width: 100%;
height: 100%;
object-fit: cover;
}
Hope this answers your question!
Marked as helpful
@sef1210
Posted
@elaineleung wow thank you for this that's the main reason why i used background image coz i dont have any idea on how will change the image in the image element. Thank you.
@elaineleung
Posted
@sef1210 No worries, I've been there too! Glad to help 😊