heading 1

heading 2

...">

heading 1

heading 2

..."/>

heading 1

heading 2

...">

heading 1

heading 2

...">

使用flexbox将元素对齐到底部

html CSS

小胖

2020-03-22

我有div一些孩子:

<div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some more or less text</p>
  <a href="/" class="button">Click me</a>
</div>

我想要以下布局:

 -------------------
|heading 1          |
|heading 2          | 
|paragraph text     |
|can have many      |
|rows               |
|                   |
|                   |
|                   | 
|link-button        |
 -------------------

无论文本中有多少文本,p我都希望.button始终将始终停留在底部,而不会将其从流程中移出。我听说这可以通过Flexbox实现,但我无法使其正常工作。

第2564篇《使用flexbox将元素对齐到底部》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

6个回答
伽罗 2020.03.22

尝试这个

.content {
      display: flex;
      flex-direction: column;
      height: 250px;
      width: 200px;
      border: solid;
      word-wrap: break-word;
    }

   .content h1 , .content h2 {
     margin-bottom: 0px;
    }

   .content p {
     flex: 1;
    }
   <div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some more or less text</p>
  <a href="/" class="button">Click me</a>
</div>

米亚西里 2020.03.22

我刚刚找到了解决方案。

对于那些对给定答案不满意的人,可以尝试使用flexbox这种方法

的CSS

    .myFlexContainer {
        display: flex;
        width: 100%;
    }

    .myFlexChild {
        width: 100%;
        display: flex;

        /* 
         * set this property if this is set to column by other css class 
         *  that is used by your target element 
         */
        flex-direction: row;

        /* 
         * necessary for our goal 
         */
        flex-wrap: wrap;
        height: 500px;
    }

    /* the element you want to put at the bottom */
    .myTargetElement {
        /*
         * will not work unless flex-wrap is set to wrap and 
         * flex-direction is set to row
         */
        align-self: flex-end;
    }

的HTML

    <div class="myFlexContainer">
        <div class="myFlexChild">
            <p>this is just sample</p>
            <a class="myTargetElement" href="#">set this to bottom</a>
        </div>
    </div>
飞云樱 2020.03.22

的解决方案align-self: flex-end;对我不起作用,但是如果您要使用flex,那么该解决方案就可以了:

结果

 -------------------
|heading 1          |
|heading 2          | 
|paragraph text     |
|                   |
|                   |
|                   | 
|link button        |
 -------------------

注意: “运行代码段”时,您必须向下滚动才能看到底部的链接。

.content {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  height: 300px;
}

.content .upper {
  justify-content: normal;
}

/* Just to show container boundaries */
.content .upper, .content .bottom, .content .upper > * {
  border: 1px solid #ccc;
}
<div class="content">
  <div class="upper">
	  <h1>heading 1</h1>
	  <h2>heading 2</h2>
	  <p>paragraph text</p>
  </div>

  <div class="bottom">
	  <a href="/" class="button">link button</a>
  </div>
</div>

樱小胖Mandy 2020.03.22

您可以使用自动边距

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

因此,您可以使用以下一种(或两种):

p { margin-bottom: auto; } /* Push following elements to the bottom */
a { margin-top: auto; } /* Push it and following elements to the bottom */

.content {
  height: 200px;
  border: 1px solid;
  display: flex;
  flex-direction: column;
}
h1, h2 {
  margin: 0;
}
a {
  margin-top: auto;
}
<div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some text more or less</p>
  <a href="/" class="button">Click me</a>
</div>

或者,您可以在a增长之前使元素填充可用空间:

p { flex-grow: 1; } /* Grow to fill available space */

.content {
  height: 200px;
  border: 1px solid;
  display: flex;
  flex-direction: column;
}
h1, h2 {
  margin: 0;
}
p {
  flex-grow: 1;
}
<div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some text more or less</p>
  <a href="/" class="button">Click me</a>
</div>

Tom凯 2020.03.22

您可以使用display:flex将元素放置在底部,但在这种情况下,我认为您不希望使用flex,因为它将影响所有元素。

要使用flex将元素定位到底部,请尝试以下操作:

.container {
  display: flex;
}

.button {
  align-self: flex-end;
}

最好的选择是将位置:绝对设置为按钮并将其设置为bottom: 0,也可以将按钮放置在容器外部,并使用负号transform: translateY(-100%)将其放入容器,如下所示:

.content {
    height: 400px;
    background: #000;
    color: #fff;
}
.button {
    transform: translateY(-100%);
    display: inline-block;
}

检查这个JSFiddle

Tony凯 2020.03.22

不确定flexbox,但是可以使用position属性。

集父div position: relative 和子元素,这可能是一个<p><h1>等集position: absolutebottom: 0

例:

index.html

<div class="parent">
  <p>Child</p>
</div>

style.css

.parent {
  background: gray;
  width: 10%;
  height: 100px;    
  position: relative;
}
p {
  position: absolute;
  bottom: 0;
}

密码笔在这里。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android