You are making great progress. Utilizing an :after
element for the icon is a smart choice, and it's important to maintain its positioning and dimensions based on the icon itself.
The effectiveness of the :after
selector lies in its ability to position itself correctly without relying on the dimensions of its parent containers. To ensure proper positioning with absolute placement, it's crucial to define the dimensions of the parent element so the child element understands its boundaries.
To start, since .gallery-icon
is already a block element, specifying its height (rather than width) is sufficient:
.gallery-icon {
position: relative;
height: 100%;
}
Additionally, utilizing a :before
element to set a background color eliminates the need to adjust the existing :after
icon:
&:before {
content: '';
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
background-color: #333;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
By adding an opacity change on hover, you can enhance the visual presentation:
&:hover {
.gallery-icon {
&:before {
opacity: .5;
}
&:after {
opacity: 0.6;
}
}
For convenience, I've created a codepen based on your original work: http://codepen.io/anon/pen/JRWqxX
Regarding the issue with the img
tag extending past the container's bottom, a simple fix involves:
.gallery-icon {
img {
display: block;
}