html - 跨 div 的中心水平线
问题描述
我试图将一条水平线置于 div 的中心,该水平线可以根据其中的内容动态变化。我创建了一个包含内容的 div。
水平线应在框的任一侧居中,但不应在 div 内可见。
html
<div class ="box">
<hr/>
<div class="container">
<h3>Click on either of the two buttons!</h3>
<button class="button ">Button 1</button>
<button class="button ">Button 2</button>
</div>
</div>
css
hr {
border: 2px solid;
}
.container {
border: 2px solid;
width: 45%;
margin: auto;
padding: 10px;
text-align: center;
}
.button {
background: transparent;
border: 2px solid;
padding: 10px 40px;
font-size: 15px;
text-align: center;
margin: 20px 10px 10px 0;
font-weight: bold;
display: inline-block;
}
解决方案
介绍一点 Flexbox CSS。希望这对您的需要有所帮助。
这是将三个元素合二为一<div>
,然后在同一行上从左到右对齐。
(“运行代码片段”并单击整页大小,您将看到问题图像中要求的设计)。
.box {
display: flex;
}
hr {
align-self: center;
background: black;
width: 40%;
height: 1px
}
.container {
border: 2px solid;
width: 45%;
padding: 10px;
text-align: center;
}
.button {
background: transparent;
border: 2px solid;
padding: 10px 40px;
font-size: 15px;
text-align: center;
margin: 20px 10px 10px 0;
font-weight: bold;
display: inline-block;
}
<div class ="box">
<hr>
<div class="container">
<div>
<h3>Click on either of the two buttons!</h3>
<button class="button ">Button 1</button>
<button class="button ">Button 2</button>
</div>
</div>
<hr>
</div>
推荐阅读
- javascript - 循环遍历 HTMLCollection 时包装元素会导致问题
- amazon-web-services - 添加 SSL 配置后 Mosquitto MQTT 服务重启失败
- c++ - 字符串附加属性c ++的异常行为
- java - 如何在MongoDB聚合Java查询的匹配阶段构造多个AND逻辑?
- javascript - 如何使用键数组中的键和另一个对象数组中的值来构建新对象
- python - Python:如何将概率与给定值相关联?
- jquery - 通过ajax发布复选框值
- java - 无法在项目上执行目标.. 无法读取 IntellJ 中错误的工件描述符
- mysql - 当 NEW.status = true 时,是否可以在其他行设置“状态”为 false?
- edge-detection - Halcon - 找到边缘位置,画线和线交点