I am creating an adaptive image gallery where thumbnails of the image are displayed in squares. For this I put the background images using background-image
and so that the image occupies the whole square, I use background-size:cover
.
This works regardless of the size of the image (the different background images will have different sizes) and the images conform to the size of the container div without problems:
#gallery .squared {
height:0;
padding-top:50%;
background:#ab00ff;
position:relative;
color:white;
font-size:1.2em;
background-position:center center;
background-size:cover;
}
@media (min-width: 768px) {
#gallery .squared { padding-top:25%; }
}
@media (min-width: 992px) {
#gallery .squared { padding-top:16.66%; }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<section id="gallery" class="section container-fluid">
<div class="row">
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/200/people);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/300/nature);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/500/cats);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/500/300/sports);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/200/abstract);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/400/animals);"></div>
</div>
</section>
Now I want to add some animation and when the user moves the mouse over the squares the background image grows a bit, for example 10%. But I can not find a way to do it. I have tried changing the value of background-size
to 100% and in :hover
to 110%, but it does not work well because that percentage is with respect to the container and not the size of the image, so, although the effect works , the landscape images do not finish looking good (they are repeated):
#gallery .squared {
height:0;
padding-top:50%;
background:#ab00ff;
position:relative;
color:white;
font-size:1.2em;
background-position:center center;
background-size:100%;
transition:all 1s;
}
#gallery .squared:hover {
background-size:110%;
}
@media (min-width: 768px) {
#gallery .squared { padding-top:25%; }
}
@media (min-width: 992px) {
#gallery .squared { padding-top:16.66%; }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<section id="gallery" class="section container-fluid">
<div class="row">
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/200/people);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/300/nature);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/500/cats);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/500/300/sports);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/200/abstract);"></div>
<div class="squared col-xs-6 col-sm-3 col-md-2" style="background-image:url(http://lorempixel.com/300/400/animals);"></div>
</div>
</section>
To avoid repeating it, I could do something like background-repeat: no-repeat
but that's not what I want because there would be blank spaces in some squares.
So the question would be: Is it possible to do this exclusively with HTML and CSS without using JavaScript ?