0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

左右に余白があって均等配置の横並びサムネリストを作る

Posted at

左右のマージン10pxで、画像間のマージン5pxのサムネが並んだ枠を作成した。
演算がややこしくて忘れそうなのでメモ。

ulに左右10pxのマージン、liは5pxずつ隙間がある。
5個横並びを固定で、画像のサイズは画面幅に応じて可変とする。
imageの縦横比は3:4。
縦横比の異なる画像は、枠内に納める。

image 66 (1).png

HTML

<div>
  <ul>
    <li>
      <a href="#">
        <img src="https://placehold.jp/150x150.png" alt="正方形">
      </a>
    </li>
    <li>
      <a href="#">
        <img src="https://placehold.jp/300x500.png" alt="縦長">
      </a>
    </li>
    <li>
      <a href="#">
        <img src="https://placehold.jp/500x150.png" alt="横長">
      </a>
    </li>
    <li>
      <a href="#">
        <img src="https://placehold.jp/400x300.png" alt="4:3">
      </a>
    </li>
    <li>
      <a href="#">
        <img src="https://placehold.jp/300x500.png" alt="縦長">
      </a>
    </li>
    <li>
      <a href="#">
        <img src="https://placehold.jp/500x150.png" alt="横長">
      </a>
    </li>
  </ul>
</div>

css

// ここはリセット
ul, li {
  list-style: none;
  margin:0;
  padding:0;
}
// ここまでリセット


div {
  background: #3c3c3e;
}

ul {
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    margin: 0 10px;
}

li{
      margin: 0 0 5px 5px;
    width: calc((100vw - 40px) / 5  );
    height: calc( ((100vw - 40px) / 5) * (3/4) );
    overflow: hidden;

    &:first-child,
    &:nth-child(6n)  {
        margin-left: 0;
    } 
}

a {
    position: relative;
    display: block;
    padding: 1px;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.1);
    width: 100%;
    height: 100%;
}

img {
    display: block;
    width: calc((100vw - 40px) / 5 );
    height: calc( ((100vw - 40px) / 5) * (3/4) );
    object-fit: contain;
    aspect-ratio: 4/3;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?