I am using bootstrap
carousal and having some problem with image height and width. Even though I define it in the img
attribute it still displays a
You can use background-size: cover
, while still having hidden element for SEO and semantic purposes. Single img url is used both times, so there's no additional loading, and background-size is well supported (unlike
object-fit
which lacks support in IE, Edge and Android < 4.4.4).
Fiddle Demo
HTML change:
CSS change:
.carousel {
/* any dimensions are fine, it can be responsive with max-width */
width: 300px;
height: 350px;
}
.carousel-inner {
/* make sure your .items will get correct height */
height: 100%;
}
.item {
background-size: cover;
background-position: 50% 50%;
width: 100%;
height: 100%;
}
.item img {
visibility: hidden;
}