弯曲相同高度的物品

时间:2018-10-21 10:59:30

标签: css flexbox

我遇到了问题Flex项目我试图创建高度相同但无法正常工作的Flex容器,现在有人可以解决这个问题吗? 我尝试了flex:1和其他属性,但它无法正常工作 这是我的示例代码:

Sample Layout

.recent-wrapper.container {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  align-items: center;
  padding: 50px 50px 40px 50px;
}

.post-image {
  width: 100%;
  height: 250px;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-color: #999;
}

article.popular_posts {
  min-width: 250px;
  margin-bottom: 10px;
  display: inline-block;
  position: relative;
  margin: 20px 1%;
  width: 100%;
  background-color: #ffffff;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  flex: 1;
}

.grid-date {
  background-color: rgb(237, 43, 71);
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  min-height: 48px;
  min-width: 48px;
  padding: 5px 10px;
  position: absolute;
  right: 15px;
  text-align: center;
  text-transform: uppercase;
  top: 0;
}

.post-container {
  width: 100%;
  padding: 5px 10px 10px 10px;
  margin-bottom: 5px;
  text-align: left;
}
<div class="recent-wrapper container">
  <article class="popular_posts">
    <div class="post-image" style="background-image: url('https://geostrateg.com/wp-content/uploads/2018/10/f-16-min.jpg');">
    </div>
    <div class="grid-date">
      <span class="day"> 07</span>
      <span class="month"> Oct</span>
    </div>
    <div class="post-container">
      <h2> Ф-16 Блок 70/72 “ПОСЛЕДЊИ ФАЛКОН” </h2>
      <p>Захваљујући уговорима о куповини вишенаменског борбеног авиона Ф-16”Figthing Falcon”&nbsp; БЛОК 70/72&nbsp;који су са његовим призвођачем, америчком компанијом “Локид Мартин” потписали</p>
      <div class="submit-btn">
        <a href="https://geostrateg.com/naoruzanje/f-16-blok-70-72-poslednji-falkon/" class="read-more">Сазнај Више</a>
      </div>
    </div>
  </article>
  <article class="popular_posts">
    <div class="post-image" style="background-image: url('https://geostrateg.com/wp-content/uploads/2018/10/win-azur-featured.jpeg');">
    </div>
    <div class="grid-date">
      <span class="day"> 07</span>
      <span class="month"> Oct</span>
    </div>
    <div class="post-container">
      <h2> СТИЖЕ АЖУРУРИРАЊЕ за “WINDOWS 10” </h2>
      <p>Добре вести! Највећа светска корпорација “IT” технологије “Мicrosoft” са седиште у граду Редмонд у америчкој савезној држави Вашингтон, најавла је</p>
      <div class="submit-btn">
        <a href="https://geostrateg.com/nauka/stize-azururiranje-za-windows-10/" class="read-more">Сазнај Више</a>
      </div>
    </div>
  </article>
  <article class="popular_posts">
    <div class="post-image" style="background-image: url('https://geostrateg.com/wp-content/uploads/2018/09/9_114950d1-min.jpg');">
    </div>
    <div class="grid-date">
      <span class="day"> 30</span>
      <span class="month"> Sep</span>
    </div>
    <div class="post-container">
      <h2> РУСКИ МОРСКИ ДУХ </h2>
      <p>Гоестратег први пут у рубрици “Војска и наоружање” посвећује пажњу једном систему морнарице. У питању је нова руска “невидљива и</p>
      <div class="submit-btn">
        <a href="https://geostrateg.com/naoruzanje/ruski-morski-duh/" class="read-more">Сазнај Више</a>
      </div>
    </div>
  </article>
</div>

1 个答案:

答案 0 :(得分:1)

问题是您使用的align-items:center试图垂直对齐3个articles的中心。而且,由于元素的高度不相等(post-container高度不同),因此它们在容器的中心垂直对齐,但是高度没有“相等”。

删除align-items:center,一切都会好起来的。

如果要水平对齐,请使用justify-content:center。请记住,方向(行/列)会更改XY轴。在此处了解更多信息-> align-items / justify-content

在下面的摘录或jsFiddle中查看结果

.recent-wrapper.container {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  /*align-items: center; */
  padding: 50px 50px 40px 50px;
}

.post-image {
  width: 100%;
  height: 250px;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-color: #999;
}

article.popular_posts {
  min-width: 250px;
  margin-bottom: 10px;
  display: inline-block;
  position: relative;
  margin: 20px 1%;
  width: 100%;
  background-color: #ffffff;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  flex: 1;
}

.grid-date {
  background-color: rgb(237, 43, 71);
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  min-height: 48px;
  min-width: 48px;
  padding: 5px 10px;
  position: absolute;
  right: 15px;
  text-align: center;
  text-transform: uppercase;
  top: 0;
}

.post-container {
  width: 100%;
  padding: 5px 10px 10px 10px;
  margin-bottom: 5px;
  text-align: left;
}
<div class="recent-wrapper container">
  <article class="popular_posts">
    <div class="post-image" style="background-image: url('https://geostrateg.com/wp-content/uploads/2018/10/f-16-min.jpg');">
    </div>
    <div class="grid-date">
      <span class="day"> 07</span>
      <span class="month"> Oct</span>
    </div>
    <div class="post-container">
      <h2> Ф-16 Блок 70/72 “ПОСЛЕДЊИ ФАЛКОН” </h2>
      <p>Захваљујући уговорима о куповини вишенаменског борбеног авиона Ф-16”Figthing Falcon”&nbsp; БЛОК 70/72&nbsp;који су са његовим призвођачем, америчком компанијом “Локид Мартин” потписали</p>
      <div class="submit-btn">
        <a href="https://geostrateg.com/naoruzanje/f-16-blok-70-72-poslednji-falkon/" class="read-more">Сазнај Више</a>
      </div>
    </div>
  </article>
  <article class="popular_posts">
    <div class="post-image" style="background-image: url('https://geostrateg.com/wp-content/uploads/2018/10/win-azur-featured.jpeg');">
    </div>
    <div class="grid-date">
      <span class="day"> 07</span>
      <span class="month"> Oct</span>
    </div>
    <div class="post-container">
      <h2> СТИЖЕ АЖУРУРИРАЊЕ за “WINDOWS 10” </h2>
      <p>Добре вести! Највећа светска корпорација “IT” технологије “Мicrosoft” са седиште у граду Редмонд у америчкој савезној држави Вашингтон, најавла је</p>
      <div class="submit-btn">
        <a href="https://geostrateg.com/nauka/stize-azururiranje-za-windows-10/" class="read-more">Сазнај Више</a>
      </div>
    </div>
  </article>
  <article class="popular_posts">
    <div class="post-image" style="background-image: url('https://geostrateg.com/wp-content/uploads/2018/09/9_114950d1-min.jpg');">
    </div>
    <div class="grid-date">
      <span class="day"> 30</span>
      <span class="month"> Sep</span>
    </div>
    <div class="post-container">
      <h2> РУСКИ МОРСКИ ДУХ </h2>
      <p>Гоестратег први пут у рубрици “Војска и наоружање” посвећује пажњу једном систему морнарице. У питању је нова руска “невидљива и</p>
      <div class="submit-btn">
        <a href="https://geostrateg.com/naoruzanje/ruski-morski-duh/" class="read-more">Сазнај Више</a>
      </div>
    </div>
  </article>
</div>

相关问题