简体   繁体   中英

Vertical alignment of responsive images of unknown height using CSS

I'm trying to layout images vertically aligned bottom (see picture below) in a responsive layout with images of unknown heights.

The images are scaled to fit the width of the column, but the heights vary and I cannot find out what it is in advance.

在此输入图像描述

Unfortunately at the moment the closest I have managed to get is this

在此输入图像描述

I'd really like to avoid using javascript if possible, because the number of columns will change depending on the screen width using media queries - which will make the javascript more complex.

The CSS I am using at the moment is

#catalogue-items {
  @include clearfix;
  margin: 40px 0;

  .catalogue-item {
    width: 20%;
    float: left;
    margin-left: 4%;
    @include box-sizing(border-box);
    margin-bottom: 20px;

    img {
      width: 100%;
    }

    p {
      font-family: sans-serif;
      font-size: 0.8em;
      padding: 0.5em;
    }

    &:nth-child(4n + 1) {
      clear:left;
    }
  }
}

With the nth-child selector changing using media queries.

Is there any way to do this?

I would solve it using display: inline-block

HTML:

<ul>
    <li>
        <img width="50" height="100" src="/img/logo.png"/><br/>
        Description
    <li>
    <li>
        <img width="50" height="125" src="/img/logo.png"/><br/>
        Description
    <li>
    <li>
        <img width="50" height="75" src="/img/logo.png"/><br/>
        Description
    <li>
<ul>

CSS:

ul {
    margin-left: -10px;
}

li {
    display: inline-block;
    margin: 0 10px 0 10px;
}

img {
    border: 1px solid red;
}

See Fiddle

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