html - 如何将 2 个 div 并排居中
问题描述
我有 2 个<div>
,每个都有 a<h1>
和 a <button>
。我希望它们居中,但中间有空间。我在互联网上搜索了无数 StackOverflow 问题、文章和教程,但它们似乎都不起作用,这是我的代码:https ://jsfiddle.net/obznmdcr/16/
button {
text-align: center;
height: 75px;
width: 225px;
font-size: 30px;
border-radius: 40px;
overflow: hidden;
border: none;
position: relative;
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.4);
cursor: pointer;
}
.panel-left {
background: rgba(0, 0, 0, 0.2);
border-radius: 10px;
display: grid;
place-items: center;
float: left;
padding: 0px 30px 30px 20px;
vertical-align: middle;
}
.panel-left h2 {
font-family: Helvetica;
text-align: center;
padding-bottom: 5px;
margin-left: auto;
}
.panel-right {
background: rgba(0, 0, 0, 0.2);
border-radius: 10px;
display: grid;
place-items: center;
float: left;
right: 500px;
padding: 10px 20px 30px 20px;
margin-left: 18%;
margin-right: auto;
vertical-align: middle;
}
.panel-right h2 {
font-family: Helvetica;
text-align: center;
padding-bottom: 25px;
}
.btn-left {
background: linear-gradient(90deg, #ff5e62, #ff9966);
}
.btn-left:hover {
background: linear-gradient(90deg, #ff9966, #ff5e62);
}
.btn-left:active {
background: linear-gradient(90deg, #d68359, #d36668);
}
.btn-right {
background: linear-gradient(90deg, #ff9966, #ff5e62);
}
.btn-right:hover {
background: linear-gradient(90deg, #ff5e62, #ff9966);
}
.btn-right:active {
background: linear-gradient(90deg, #d36668, #d68359);
}
<div class="panel-left">
<h2>Title Left</h2>
<button class="btn-left" id="btn" type="button" onclick="console.log('button pressed');">Button Left</button>
</div>
<div class="panel-right">
<h2>Title Right</h2>
<button class="btn-right" id="btn" type="button" onclick="console.log('button pressed');">Button Right</button>
</div>
解决方案
该问题未指定是否divs
也必须垂直居中。
仅水平居中:
HTML:
在 中扭曲 HTML 代码div
,类如.container
这里。
CSS:
设置display
为类,并将其属性设置flex
为。container
justify-content
space-evenly
从中删除margin-left
和。margin-right
.panel-right
.container{
display: flex;
justify-content: space-evenly;
}
button {
text-align: center;
height: 75px;
width: 225px;
font-size: 30px;
border-radius: 40px;
overflow: hidden;
border: none;
position: relative;
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.4);
cursor: pointer;
}
.panel-left {
background: rgba(0, 0, 0, 0.2);
border-radius: 10px;
display: grid;
place-items: center;
float: left;
padding: 0px 30px 30px 20px;
vertical-align: middle;
}
.panel-left h2 {
font-family: Helvetica;
text-align: center;
padding-bottom: 5px;
margin-left: auto;
}
.panel-right {
background: rgba(0, 0, 0, 0.2);
border-radius: 10px;
display: grid;
place-items: center;
float: left;
right: 500px;
padding: 10px 20px 30px 20px;
/*margin-left: 18%;
margin-right: auto;*/
vertical-align: middle;
}
.panel-right h2 {
font-family: Helvetica;
text-align: center;
padding-bottom: 25px;
}
.btn-left {
background: linear-gradient(90deg, #ff5e62, #ff9966);
}
.btn-left:hover {
background: linear-gradient(90deg, #ff9966, #ff5e62);
}
.btn-left:active {
background: linear-gradient(90deg, #d68359, #d36668);
}
.btn-right {
background: linear-gradient(90deg, #ff9966, #ff5e62);
}
.btn-right:hover {
background: linear-gradient(90deg, #ff5e62, #ff9966);
}
.btn-right:active {
background: linear-gradient(90deg, #d36668, #d68359);
}
<div class="container">
<div class="panel-left">
<h2>Title Left</h2>
<button class="btn-left" id="btn" type="button" onclick="console.log('button pressed');">Button Left</button>
</div>
<div class="panel-right">
<h2>Title Right</h2>
<button class="btn-right" id="btn" type="button" onclick="console.log('button pressed');">Button Right</button>
</div>
</div>
对于垂直居中以及:
如果您还希望它们垂直居中,请将html
, body
,的高度设置.container
为100%
(或任何其他尺寸,如果您愿意),并将align-items
属性设置为center
类中container
。
推荐阅读
- python - Fuzzywuzzy 过程 extractBests 的奇怪结果
- python - 用于duckduckgo的python selenium并找到url
- c++ - 在调用 sendto() 之前从 UDP 套接字修改 IP 标头
- javascript - 如何捕获注销确认“是”和“否”的事件
- javascript - 在 HTML 和 JavaScript 中使用检查隐藏某些内容以使其无法查看
- regex - htaccess 中带有斜杠数量的条件
- python - 我需要计算滞后 6 天 Pandas python 的每日价格回报的平均值
- css - 如何正确声明路径?
- python - 将 imshow 与底图一起使用:颜色和地图边界不匹配
- common-lisp - 使用双浮点数时,有没有办法在普通 lisp 中显示更少的数字?