将页脚与flexbox的底部对齐

时间:2018-07-03 18:07:25

标签: css css3 flexbox

我有一些flexbox,我想在其中包含一些页脚信息。我试图使页脚与flexbox的底部对齐,而不管内容实际有多短。我尝试过使用自动页边距和flexbox设置,但是似乎找不到解决方法。

这是我的原始设置:

.card-wrapper {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.card {
  flex: 0 1 100%;
  margin: 0 10px 10px 0;
  border: 1px solid grey;
  border-top: 10px solid black;
  box-shadow: 1px 1px 3px #888;
}

.card-content {
  padding: 10px;
}

@media all and (min-width: 30em) {
  .card {
    flex: 0 1 30%;
  }
<div class="card-wrapper">
  <a href="#" class="card">
    <div class="card-content">
      <h2>Title</h2>
      <p>Content</p>
      <p>This should be the footer.</p>
    </div>
  </a>
</div>

5 个答案:

答案 0 :(得分:1)

请记住flex properties work only between parent and child elements。您的flex容器(.card-wrapper)是content元素的远祖(大祖父母),因此页脚不在范围内。

使父级(.card-content)为flex容器,以便可以将flex属性(包括自动边距)应用于子级。

.card-wrapper {
  display: flex;
}

.card {
  flex: 0 1 100%;
  margin: 0 10px 10px 0;
  border: 1px solid grey;
  border-top: 10px solid black;
  box-shadow: 1px 1px 3px #888;
}

.card-content {
  padding: 10px;
  display: flex;           /* new */
  flex-direction: column;  /* new */
  height: 150px;           /* demo only */
}

.card-content > :last-child {
  margin-top: auto;        /* new */
}

.card-content > * {
  margin: 0;               /* demo only */
}
<div class="card-wrapper">
  <a href="#" class="card">
    <div class="card-content">
      <h2>Title</h2>
      <p>Content</p>
      <p>This should be the footer.</p>
    </div>
  </a>
</div>

答案 1 :(得分:0)

实际上我很困惑您的要求,但是如果您在页脚后不需要任何空格,请查看以下代码并输出。

.card-wrapper {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
}

.card {
    flex: 0 1 100%;
    margin: 0 10px 10px 0;
    border: 1px solid grey;
    border-top: 10px solid black;
    box-shadow: 1px 1px 3px #888;
}

.card-content {
    padding: 10px 10px 0;
}
.no-margin {
    margin: 0;
}

@media all and (min-width: 30em) {
    .card {
        flex: 0 1 30%
    }
}
<div class="card-wrapper">
    <a href="#" class="card">
        <div class="card-content">
            <h2>Title</h2>
            <p>Content</p>
            <p class="no-margin">This should be the footer.</p>
        </div>
    </a>
</div>

答案 2 :(得分:0)

我刚刚将页脚“ p”标签更改为“ span”标签。它消除了不必要的利润。
尝试下面的代码

  .card-wrapper {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.card {
  flex: 0 1 100%;
  margin: 0 10px 10px 0;
  border: 1px solid grey;
  border-top: 10px solid black;
  box-shadow: 1px 1px 3px #888;
}

.card-footer{
text-align:center;
}

.card-content {
  padding: 10px;
}

@media all and (min-width: 30em) {
  .card {
    flex: 0 1 30%
  }
  ;
<div class="card-wrapper">
  <a href="#" class="card">
    <div class="card-content">
      <h2>Title</h2>
      <p>Content</p>
    </div>
    <div class="card-footer">
      <span>This is footer.</span>
    </div>
  </a>
</div>

答案 3 :(得分:0)

您可以使用自动边距

在通过justify-content和align-self进行对齐之前,任何正的自由空间都会分配给该维度中的自动边距。

.card-wrapper {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
}

.card {
    flex: 0 1 100%;
    margin: 0 10px 10px 0;
    border: 1px solid grey;
    border-top: 10px solid black;
    box-shadow: 1px 1px 3px #888;
}
p { 
  flex-grow: 1; 
}

.card-content {
    padding: 10px;
}
.footer{ margin-top: auto; } 


@media all and (min-width: 30em) {
    .card {
        flex: 0 1 30%
    }
    ;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="card-wrapper">
    <a href="#" class="card">
        <div class="card-content">
            <h2>Title</h2>
            <p>Content</p>
            <p class="footer">This should be the footer.</p>
        </div>
    </a>
</div>
</body>
</html>

答案 4 :(得分:0)

您实际上是说多张这样的卡片吗?

.cards {
  display: flex;
  flex-direction: row;
}

.card-wrapper {
  flex: 1 0 auto; /* ADDED */
  display: flex;
  /* flex-wrap: wrap; REMOVED */
  /* justify-content: flex-start; REMOVED*/
}

.card {
  display: flex; /* ADDED */
  flex: 1 0 auto; /* changed from 0 1 100% */
  flex-direction: column; /* ADDED */
  margin: 0 10px 10px 0;
  border: 1px solid grey;
  border-top: 10px solid black;
  box-shadow: 1px 1px 3px #888;
}

.card-content,
.card-footer { /* ADDED .card-footer */
  padding: 10px;
}

.card-content { /* ADDED */
  flex: 1 0 auto;
}

.card-footer { /* ADDED */
  flex: 0 0 auto;
}

@media all and (min-width: 30em) {
  .card {
    /* flex: 0 1 30% REMOVED */
  }
}
<div class="cards">
  <div class="card-wrapper">
    <a href="#" class="card">
      <div class="card-content">
        <h2>Title</h2>
        <p>Short Content</p>
      </div>
      <div class="card-footer">
        <p>This should be the footer.</p>
      </div>
    </a>
  </div>

  <div class="card-wrapper">
    <a href="#" class="card">
      <div class="card-content">
        <h2>Title</h2>
        <p>Even<br/>Longer<br/>Content<br/>Here</p>
      </div>
      <div class="card-footer">
        <p>This should be the footer.</p>
      </div>
    </a>
  </div>
  
  <div class="card-wrapper">
    <a href="#" class="card">
      <div class="card-content">
        <h2>Title</h2>
        <p>Very<br/>Long<br/>Content<br/></p>
      </div>
      <div class="card-footer">
        <p>This should be the footer.</p>
      </div>
    </a>
  </div>
</div>