Currently, I am utilizing display:inline-block
and pseudo-elements (::before
, ::after
) to achieve vertical alignment in the middle, but unfortunately, it seems to be ineffective. The pseudo-element ends up taking a whole row of space even with a width
set to 0
. What could be causing this issue?
I am aware of alternative methods like using flex
, position
, or adjusting line-height
. However, I am intrigued as to why the current approach is not working correctly. Any insights would be greatly appreciated. To view a demonstration, you can check out the following link: https://jsfiddle.net/pm06tkjs/
.container{
height: 200px;
background: rgba(0, 0, 0, 0.2);
}
.container::before {
content: '';
display: inline-block;
width: 0;
height: 100%;
vertical-align: middle;
}
.img-group {
display: inline-block;
vertical-align: middle;
}
img {
display: inline-block;
margin-right: -4px;
max-width: 20%;
height: auto;
}
<div class="container">
<div class="img-group">
<img src="https://www.penghu-nsa.gov.tw/FileDownload/Album/Big/20161012162551758864338.jpg" alt="">
<img src="https://www.penghu-nsa.gov.tw/FileDownload/Album/Big/20161012162551758864338.jpg" alt="">
<img src="https://www.penghu-nsa.gov.tw/FileDownload/Album/Big/20161012162551758864338.jpg" alt="">
<img src="https://www.penghu-nsa.gov.tw/FileDownload/Album/Big/20161012162551758864338.jpg" alt="">
<img src="https://www.penghu-nsa.gov.tw/FileDownload/Album/Big/20161012162551758864338.jpg" alt="">
</div>
</div>