【发布时间】:2022-02-15 05:24:50
【问题描述】:
我需要使用 width: fit-content 来获取 h2 的宽度,并在此基础上在左右 10% 之前和之后添加。应该是文字前后的一些装饰线,但文字长度会有所不同,所以这就是为什么我有适合内容并尝试在其上添加边距的原因。
.type-of-food {
color: #fff;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
letter-spacing: 3px;
margin-top: 100px;
margin-bottom: 50px;
position: relative;
h2 {
font-size: 1.5rem;
height: 30px;
width: fit-content;
&::before {
content: "";
position: absolute;
left: calc(width-of-h2 - 10%);
top: 50%;
width: 50px;
height: 1px;
background-color: white;
}
&::after {
content: "";
position: absolute;
right: calc(width-of-h2 + 10%);
top: 50%;
width: 50px;
height: 1px;
background-color: white;
}
}
}
【问题讨论】:
-
所以你希望文本和行之间的间隙等于 h1 的 10%?
-
@TemaniAfif 是
-
不完全确定它只能使用 CSS/SASS 来完成。 JS会是一种选择吗?