【问题标题】:Uniform auto items height in flexboxflexbox中统一的自动项目高度
【发布时间】:2017-06-15 09:24:57
【问题描述】:

我有一个带有弹性框和多个弹性项目的页面,弹性项目代表帖子,这些帖子有时有大标题,有时有小标题,导致每行高度不一致,如下图所示:

我不想设置固定高度,因为现在小标题会有很大的空白空间。相反,我希望每行的高度保持不变,并且等于包含帖子元素的最小高度。

这是我的 CSS:

/* Post Pages */

.post-containter {
    padding:4%;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-around;
    align-content: space-around;
}
.post {
    text-align: center;
    width: 250px;
    padding: 10px 10px;
    border-radius: 5px;
    border: thin solid #f8f8f8;
    box-shadow: 1px 1px 2px rgba(0,0,0,0.1);
    background-color: white;
    display: inline-block;
    margin-top: 15px;
    transition: box-shadow 150ms ease-in-out;
}
.post h5 {
    font-weight: normal;
    margin:5px;
}
.post:hover {
    box-shadow: 0px 0px 25px rgba(0,0,0,0.1);
}
.posts-head {
    text-align: center;
    font-weight: bolder;
    font-size: 50px;
}
img.post-image {
    max-width: 100%;
    height: auto;
    border-top: 3px solid #558abb;
    border-bottom: 3px solid #00c8bd;
}

这里是 HTML:

<div class="post-containter">
{% for post in site.posts %}
    <a href="{{post.url}}">
        <div class="post">
            <img class="post-image" src="{{ post.image_path }}" alt="{{ post.title }}"/>
            <h5>{{ post.title }}</h5>
        </div>
    </a>
{% endfor %}
</div>

网站参考:https://squircleart.github.io/posts.html

【问题讨论】:

  • 你可以试试 justify-content: stretch;并处理帖子的边距。
  • 我们还需要查看内部元素的 HTML 和 CSS,对于我来说 post 肯定有相同的大小,但它的内容没有
  • @Gerard justify-content 没有 stretch 的值
  • @LGSon 我可以发布网站本身吗?squircleart.github.io/posts.html
  • 可以,作为参考,虽然问题应该足以显示问题,所以它也需要更新

标签: html css flexbox


【解决方案1】:

这里的主要问题是post 的父元素,即包装它的链接。

如果你给它display: flex 它会起作用

.post-containter > a {
  display: flex;
}

【讨论】:

  • 如果我在“a”标签中使用post class而不是div会更好吗?
  • @OmarAhmad 不能说,试试看,如果遇到麻烦,你知道去哪里问:)。至少现在你知道是什么原因造成的。
  • 谢谢,它似乎工作正常。我只需要添加 text-decoration: none;确保我的标题没有下划线。
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多