html - 带有flexbox的div底部的定位按钮
问题描述
我的页面上有一系列并排放置的 div。我已经使用 flexbox 对它们进行了定位。在每个盒子里面我都有一个按钮,我想把它放在盒子的底部。我看过与我类似的其他问题,但这些解决方案都没有奏效。我试过添加
p {
margin: 0
}
.movie_list_button {
margin-top: auto;
}
但这无济于事。我也试过p { flex-grow: 1; }
了,但也没有用。我不确定我错过了什么。
这是我的 HTML:
<div class="movie_group">
<div class="movie_list">
<ul>
<li>
<p><b>Box 1 text 1</b></p>
<p>Box 1 text 2. No matter how large this text is, I want the button to stick to the bottom of the box</p>
<div class="movie_list_button">
<button>Button</button>
</div>
</li>
</ul>
</div>
<div class="movie_list">
<ul>
<li>
<p><b>Box 2 text 1</b></p>
<p>Box 2 text 2. No matter how large this text is, I want the button to stick to the bottom of the box</p>
<div class="movie_list_button">
<button>Button</button>
</div>
</li>
</ul>
</div>
</div>
CSS:
.movie_group {
margin-top: 120px;
display: flex;
flex-wrap: wrap;
}
.movie_list {
list-style: none;
padding: 10px;
flex-basis: 30%;
border: 1px solid black;
height: 300px;
/* display: flex;
flex-direction: column; */
}
p {
margin: 0;
/* flex-grow: 1; */
}
.movie_list_button {
background-color: white;
margin-top: auto;
}
这是一个 JSFiddle:https ://jsfiddle.net/4pbzfct3/64/
解决方案
你可以通过给它的父 div “position: relative” 和按钮周围的 div “position: absolute” 来将你的按钮从你的 flexbox 的“流”中取出。从那里,您可以使用顶部、右侧、左侧和底部来移动按钮,相对于其父元素。我将在下面发布的内容将按钮放在左下角,但您可以将其移动到任何您喜欢的地方。如果您愿意,您还可以使用负值将其移出其父级。
.movie_group {
margin-top: 120px;
display: flex;
flex-wrap: wrap;
}
.movie_list {
list-style: none;
padding: 10px;
flex-basis: 30%;
border: 1px solid black;
height: 300px;
**position: relative;**
/* display: flex;
flex-direction: column; */
}
p {
margin: 0;
/* flex-grow: 1; */
}
.movie_list_button {
background-color: white;
margin-top: auto;
**position: absolute;
left: 10px;
bottom: 10px;**
}
如果您不喜欢使用绝对位置和相对位置来移动按钮的想法,您还可以在 flexbox 中创建一个 flexbox 来定位您的按钮。这将需要您更改 HTML。最终,从长远来看,这会更好,因为它已经是伪响应了。
您将按钮 div 移到 ul 之外,然后将另一个 div 放在 ul 周围。因此,您的新 movie_list 弹性框中将有两个“弹性项目”。然后,您将“flex-direction: column”添加回您的 .movie_list 类。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Document</title>
</head>
<body>
<div class="movie_group">
<div class="movie_list">
<div class="inner-flex">
<ul>
<li>
<p><b>Box 1 text 1</b></p>
<p>
Box 1 text 2. No matter how large this text is, I want the
button to stick to the bottom of the box
</p>
</li>
</ul>
</div>
<div class="movie_list_button">
<button>Button</button>
</div>
</div>
<div class="movie_list">
<div class="inner-flex">
<ul>
<li>
<p><b>Box 2 text 1</b></p>
<p>
Box 2 text 2. No matter how large this text is, I want the
button to stick to the bottom of the box
</p>
</li>
</ul>
</div>
<div class="movie_list_button">
<button>Button</button>
</div>
</div>
</div>
</body>
</html>
.movie_group {
margin-top: 120px;
display: flex;
flex-wrap: wrap;
}
.movie_list {
list-style: none;
padding: 10px;
flex-basis: 30%;
border: 1px solid black;
height: 300px;
display: flex;
flex-direction: column;
}
p {
margin: 0;
/* flex-grow: 1; */
}
.inner-flex {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.movie_list_button {
background-color: white;
margin-top: auto;
}
推荐阅读
- html - 滑入、等待、滑出 CSS 动画
- asp.net-core - 无法使用 asp.net 核心中的角色连接到 DynamoDB 和 S3
- ruby-on-rails - sortablejs 方法不可用
- r - 用 Latex 表达式覆盖 ggplot 轴标签
- java - kafka 流示例引发类转换异常,serde windowed -> string。如何设置正确的 Serde?
- python - Python提取两个数据帧中的索引不匹配
- c++ - ESP8266 NodeMCU 的 P10 Led Matrix 64x32 在像素时间(PxMatrix 库)中不显示为真
- string - 模型“用户”的路径“工作”中的值“['Electrician','Active']”转换为字符串失败
- java - 如何使用我自己的 CA 证书在 stunnel 和 Android 应用程序之间建立 TLS 连接
- heroku - Heroku 重新附加一个取消配置的 portgres 数据库 ( Hobby-Basic )