是否可以在弹性布局中定位每行的第一个和最后一个元素? [英] Is it possible to target the first and the last element per row in a flex layout?

查看:97
本文介绍了是否可以在弹性布局中定位每行的第一个和最后一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出Flex布局中的缩略图列表,如果flex容器的宽度可变,是否可以仅使用CSS定位每行的第一个和最后一个元素?

Given a list of thumbnails in a flex layout, is it possible to target the first and the last element per row using only CSS if the flex container has variable width?

.thumbnails {
  display: flex;
  margin: 0;
  padding: 0;
  list-style-type: none;
  flex-flow: row wrap;
}
.thumbnail {
  width: 250px;
  height: 250px;
  margin: 5px;
  background: #ccc;
  border: 1px solid #000;
}

<ul class="thumbnails">
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
</ul>

推荐答案

.thumbnails {
  display: flex;
  margin: 0;
  padding: 0;
  list-style-type: none;
  flex-flow: row wrap;
}
.thumbnail {
  width: 250px;
  height: 250px;
  margin: 5px;
  background: #ccc;
  border: 1px solid #000;
}
.thumbnail:nth-child(odd) {
  background: #e6e6e6;
}
.thumbnail:nth-child(even) {
  background: #e9edf2;
}

<ul class="thumbnails" id="wrapper">
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
  <li class="thumbnail"></li>
</ul>

以我的方式,只需使用:nth-child(even):nth-child(odd).希望这会有所帮助.

In my way just use :nth-child(even) and :nth-child(odd). Hope this helps.

这篇关于是否可以在弹性布局中定位每行的第一个和最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆