Выровняйте элемент снизу с помощью flexbox
У меня есть 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, но я не могу заставить его работать.
Ответы
Ответ 1
Вы можете использовать автопоставки
До выравнивания по 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>
Ответ 2
Вы можете использовать display: flex, чтобы расположить элемент снизу, но я не думаю, что вы хотите использовать flex в этом случае, так как это повлияет на все ваши элементы.
Чтобы расположить элемент снизу с помощью flex, попробуйте это:
.container {
display: flex;
}
.button {
align-self: flex-end;
}
Лучше всего установить позицию: абсолютную по отношению к кнопке и установить ее bottom: 0
, или вы можете поместить кнопку за пределы контейнера и использовать отрицательное transform: translateY(-100%)
чтобы transform: translateY(-100%)
его в контейнер следующим образом:
.content {
height: 400px;
background: #000;
color: #fff;
}
.button {
transform: translateY(-100%);
display: inline-block;
}
Проверьте это JSFiddle
Ответ 3
Решение с 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;
}
.content .upper, .content .bottom {
border: 1px solid #ccc;
}
.content .upper > * {
border: 1px solid #999;
}
<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>
Ответ 4
Попробуй это
.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>
Ответ 5
Не уверен насчет flexbox, но вы можете сделать это с помощью свойства position.
установить родительскую position: relative
div
position: relative
и дочерний элемент, который может быть <p>
или <h1>
т.д.. установить position: absolute
и bottom: 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;
}
Код ручка здесь.