html - 如何使两个居中没有它们重叠?
问题描述
我试图以按钮的形式居中两个 div 元素。但我尝试的任何方法似乎都不起作用。我得到的最远的是将它放在中间,position: absolute;
但是当我这样做时,这两个元素会相互重叠。
我目前正在使用的代码:
/* For the button: */
.btn1 {
color: #000 !important;
text-transform: uppercase;
background: #ed1b36;
padding: 20px;
border-radius: 5px;
display: inline-block;
border: none;
text-decoration: none;
cursor: pointer;
width: 30%;
transition: all 0.3s ease 50ms;
text-align: center;
max-width: 400px;
position: absolute;
top: 10%;
bottom: 10%;
left: 0;
right: 0;
margin: auto;
}
/* For the hover effect: */
.btn1:hover {
background: #f2d630;
-webkit-box-shadow: 0px 5px 40px -10px rgba(0, 0, 0, 0.57);
-moz-box-shadow: 0px 5px 40px -10px rgba(0, 0, 0, 0.57);
box-shadow: 5px 40px -10px rgba(0, 0, 0, 0.57);
transition: all 0.3s ease 50ms;
cursor: pointer;
border-radius: 15px 15px 15px 15px;
}
<div class="btn1">
<a class="btn1" href="showTable.php"><b>Reserveringen</b></a>
</div><br>
<div class="btn1">
<a class="btn1" href="logout.php"><b>Logout</b></a>
</div>
提前致谢!
解决方案
希望这可以帮助。我使用margin-right:auto
andmargin-left:auto
让 btn1 水平居中对齐
.btns{
margin-top:50%;
transform:translateY(-50%)
}
将其垂直居中对齐
.btn1 {
color: #000 !important;
text-transform: uppercase;
background: #ed1b36;
padding: 20px;
border-radius: 5px;
display: block;
border: none;
text-decoration: none;
cursor: pointer;
width: 30%;
transition: all 0.3s ease 50ms;
text-align: center;
max-width: 400px;
margin-left:auto;
margin-right:auto;
margin-bottom:10px;
}
.btn1:hover {
background: #f2d630;
-webkit-box-shadow: 0px 5px 40px -10px rgba(0, 0, 0, 0.57);
-moz-box-shadow: 0px 5px 40px -10px rgba(0, 0, 0, 0.57);
box-shadow: 5px 40px -10px rgba(0, 0, 0, 0.57);
transition: all 0.3s ease 50ms;
cursor: pointer;
border-radius: 15px 15px 15px 15px;
}
.btns{
margin-top:50%;
transform:translateY(-50%)
}
<div class="btns">
<a class="btn1" href="showTable.php"><b>Reserveringen</b></a>
<a class="btn1" href="logout.php"><b>Logout</b></a>
</div>
推荐阅读
- javascript - 双管道 - 未包含选项时未定义?
- python - 连接字符串和变量,给出类型错误,“标签”的操作数不受支持
- r - R中for循环中具有渲染功能的多个参数
- nginx - 在 nGinx Staticfile buildpack 中添加 X-Frame-Options
- java - 测试 java jersey 应用程序
- javascript - ReportViewer 15.0.0 渲染:Sys.ArgumentNullException:值不能为空。参数名称:元素
- ruby-on-rails - Rails 不会为 vue 加载 /javascript/packs
- c++ - Allowing a custom macro in #ifdef check
- html - HTML 和 CSS:将复选框放在图像的右上角
- android - Can you set a fontFamily variable in Android?