html - 如何将部分底线放在 h1 跨度文本的正中间?
问题描述
您好,我在 h1 文本下有一条部分底部黄线,但我不知道如何将它放在中间。我可以把它放在左边,但对齐中心或边距自动不起作用。
我试着玩了大约一个小时,但我无法弄清楚。我看到有些人给 h1 跨度一个特定的像素宽度,但我试图避免这样做,因为我计划让 ipad 和移动设备的文本更小。
.l-heading {
font-weight: bold;
font-size: 4rem;
margin-bottom: 0.75rem;
line-height: 1.1;
}
.container {
max-width: 1404px;
margin: auto;
padding: 0 2rem;
overflow: hidden;
}
.text-center {
text-align: center;
}
#meet-our-team .container h1 span {
position: relative;
}
#meet-our-team .container h1 span::after {
content: '';
width: 40%;
height: 4px;
background: red;
position: absolute;
bottom: -4px;
left: 0;
}
<section id="meet-our-team" class="bg-black">
<div class="container">
<h1 class="l-heading text-center py-2">
<span>Meet The Owner</span>
</h1>
</div>
</section>
解决方案
margin: auto
是正确的,但是您不能像这样将绝对伪元素居中,只能使用 auto。但是,如果您设置left: 0;
和right: 0;
,您将水平居中。
.l-heading {
font-weight: bold;
font-size: 4rem;
margin-bottom: 0.75rem;
line-height: 1.1;
}
.container {
max-width: 1404px;
margin: auto;
padding: 0 2rem;
overflow: hidden;
}
.text-center {
text-align: center;
}
#meet-our-team .container h1 span {
position: relative;
}
#meet-our-team .container h1 span::after {
content: '';
width: 40%;
height: 4px;
background: red;
position: absolute;
bottom: -4px;
left: 0;
right: 0;
margin: auto;
}
<section id="meet-our-team" class="bg-black">
<div class="container">
<h1 class="l-heading text-center py-2">
<span>Meet The Owner</span>
</h1>
</div>
</section>
推荐阅读
- javascript - Vue Owl Carousel 在容器外渲染数据
- javascript - 显示数组项的html有序列表?
- r - 使用带状变量扩展行
- javascript - 如何匹配两个单词之间的字符串,并为字符串中所有两个定义的单词重复此模式,Regex?
- django - Django从列表创建或更新到模型
- javascript - 电子:在无框窗口孩子上添加框架
- html - 我无法使用 html 在我的 /var/www/efs/image/ 文件夹中获取图像
- javascript - 如何使用 node.js 和 Microsoft Graph API 发送附件?
- python - NotImplementedError:尚不支持位打包
- javascript - 如何在 vue js 表中显示 Json 中的第一项?