html - CSS flex 居中到父全宽
问题描述
我正在尝试将两个按钮置于其父元素的中心。问题是第一个元素在开头和结尾都有内容,这导致 flex 居中,弄乱了整体的对齐方式。
#container {
background-color: green;
width: 100%;
}
#d1 {
height: 50px;
display: flex;
}
#d2 {
background-color: blue;
width: 50px;
height: 50px;
}
#d3 {
background-color: red;
}
<div id="container">
<div id="d1">
<p>This is some text</p>
<input type="button">
<div id="d2"></div>
</div>
<div id="d3">
<input type="button">
</div>
</div>
解决方案
您需要justify-content: center;
与display:flex;
父 div 一起使用。
#container {
background-color: green;
width: 100%;
display: flex;
justify-content: center;
}
#d1 {
height: 50px;
display: flex;
}
#d2 {
background-color: blue;
width: 50px;
height: 50px;
display: flex;
}
#d3 {
background-color: red;
}
<div id="container">
<div id="d1">
<p>This is some text</p>
<input type="button">
<div id="d2"></div>
</div>
<div id="d3">
<input type="button">
</div>
</div>
推荐阅读
- java - Warnings when running my web app with Glassfish 4.1.1 in Netbeans
- angular - 如何正确设置新的 Angular 6 项目
- sql - 在 SQL Server 中一次性获取 DISTINCT COUNT
- javascript - 有没有办法暂停和恢复加载 JavaScript 的 html 元素?
- python - 如何获取注释变量以在 django 模板上显示数据?
- r - 只提取 _ 之后的字母,中间有数字
- c++ - 隐藏超出边界的 QGraphicsItem 区域
- php - 如何在本地 Web 系统上执行自我更新?
- responsive-design - Bootstrap4 响应式导航栏在调整大小时包装文本
- android - SQLite 无法读取表中的字段