我想在桌子底部有一个按钮,位于桌子底部的中间,居中。我确实添加了left:30%
和right:30%
,因此按钮位于中间位置,但是,我注意到Chrome中的呈现方式与Safari或Firefox不同,在Chrome中,百分比不一样,因此按钮最左边比右边更多,而不是居中/中间。试试下面。
我怎样才能在Chrome中同样使用它?为什么百分比的呈现方式不同,在absolute
定位中需要将这个元素集中在一起的最佳技术是什么?
.panel {
background-color: #fff;
border-radius: 10px;
padding: 15px 25px;
position: relative;
width: 100%;
z-index: 10;
}
.table {
box-shadow: 0px 10px 13px -6px rgba(0, 0, 0, 0.08), 0px 20px 31px 3px rgba(0, 0, 0, 0.09), 0px 8px 20px 7px rgba(0, 0, 0, 0.02);
display: flex;
flex-direction: column;
height:350px;
background:lightblue;
}
.table * {
text-align: center;
}
.contact-button {
bottom: -25px;
background: #f9f9f9;
border-radius: 10px;
left: 30%;
right: 30%;
margin: 0 auto;
position:absolute;
}
.btn {
display: inline-block;
padding: 8px 32px;
cursor: pointer;
text-align: center;
vertical-align: middle;
white-space: nowrap;
outline: none;
line-height: inherit;
background-color: white;
border: 1px solid black;
color: black;
font-size: 14px;
letter-spacing: 2px;
}
<div class="panel table">
<a href="/contact"><button class="btn contact-button">CONTACT MORE INFO</button></a>
</div>
答案 0 :(得分:0)
您可以从button
移动div
并创建类似
<div class="panel table"></div>
<div class="wrapper">
<button class="btn contact-button">CONTACT MORE INFO</button>
</div>
这样您就不必使用position:absolute;
,只需使用
display:flex;
justify-content:center;
如果您想要更多支持较旧的浏览器版本,请查看此article
检查以下示例
.panel {
background-color: #fff;
border-radius: 10px;
position: relative;
width: 100%;
z-index: 10;
}
.table {
box-shadow: 0px 10px 13px -6px rgba(0, 0, 0, 0.08), 0px 20px 31px 3px rgba(0, 0, 0, 0.09), 0px 8px 20px 7px rgba(0, 0, 0, 0.02);
display: flex;
flex-direction: column;
height: 350px;
background: lightblue;
}
.table * {
text-align: center;
}
.contact-button {
bottom: -25px;
background: #f9f9f9;
border-radius: 10px;
left: 30%;
right: 30%;
margin: 0 auto;
}
.btn {
display: inline-block;
padding: 8px 32px;
cursor: pointer;
text-align: center;
vertical-align: middle;
white-space: nowrap;
outline: none;
line-height: inherit;
border: 1px solid black;
color: black;
font-size: 14px;
letter-spacing: 2px;
}
.wrapper {
display: flex;
justify-content: center;
}
body {
margin: 0;
}
&#13;
<div class="panel table">
</div>
<div class="wrapper">
<button class="btn contact-button">CONTACT MORE INFO</button>
</div>
&#13;
答案 1 :(得分:0)
您可以使用它也可以帮助您而不会改变很多事情:
.contact-button {
background: #f9f9f9;
border-radius: 10px;
margin: 0 auto;
margin-top:370px;
}