sma*_*lly 13 html css css3 flexbox
我有3个div使用display:flex.
这些div中的内容具有不同的高度,但我想要的是在每个div中创建一个始终位于底部的链接.
----------- ----------- -----------
| img | | img | | img |
| heading 1 | | heading 3 | | heading 3 |
| paragraph | | paragraph | | paragraph |
| paragraph | | | | paragraph |
| | | | | paragraph |
| link | | link | | link |
----------- ----------- -----------
Run Code Online (Sandbox Code Playgroud)
这样链接将始终排列.
Jos*_*ier 10
你可以改变display的.content到flex,再加入flex-direction: column以使从顶部的内容流至底部.为了将子锚元素放在底部,只需添加添加margin-top: auto:
.content {
display: flex;
flex-direction: column;
}
.content a {
margin-top: auto;
}
Run Code Online (Sandbox Code Playgroud)
这有两种方法:
嵌套的Flexbox
.col {
flex: 1;
display: flex; /* create nested flex container */
flex-wrap: wrap; /* enable flex items to wrap */
justify-content: center; /* center flex items on each line */
}
.col > a { align-self: flex-end; } /* position link always at bottom */
Run Code Online (Sandbox Code Playgroud)
绝对定位
.col {
flex: 1;
position: relative; /* establish containing block (nearest positioned ancestor)
for absolute positioning */
}
.col > a {
position: absolute;
bottom: 5px; /* adjust this value to move link up or down */
left: 50%; /* center link horizontally */
transform: translateX(-50%); /* center link horizontally */
}
Run Code Online (Sandbox Code Playgroud)
第三种方法涉及切换flex-direction到column.但是,在某些情况下,更改弯曲方向不是可接受的选项,因为它会更改布局的行为.(此处未详细说明此方法,因为它已在另一个答案中发布.)