简体   繁体   中英

CSS float: left causing unnecessary whitespace with varying height

This is a problem with a live website, the page I'm talking about is this .

Basically I have some PHP which echos out images from my db wrapped in divs, like this, for example, for each image:

<div class='gall_div'>
<a href='img_gallery/CD cover Bach 2.jpg' class='lightbox'>
<img class='galleryImage' title='(tooltip)' src='someimg.jpg'>
</a></div>

I want my images to wrap to the left, the CSS looks like this:

div.gall_div {
    float:left;
    margin-right:120px;
    padding-bottom:10px;
        padding-top:10px;
    width: 100px;
}

Notice, if you try resizing the page, unnecessary whitespace appears to the left of the second/third row of images. I believe this is due to varying image height. How can I correct this?

I'd say that the best solution here would be to change your CSS slightly, to declare them as inline-block elements, and have the text-align set to the left:

div.gall_div {
    display:inline-block;
    margin-right:120px;
    padding-bottom:10px;
        padding-top:10px;
    text-align:left;
    width: 100px;
}

Additionally, this allows you to set vertical-align: middle; so that they can be vertically centered, if need be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM