如何使用 css 使文本之间的行和间距均匀
How to get even lines and spacing betweeen text using css
正在寻找一种在一组单词之间创建线条的方法。我能够让它有点工作,但我不会总是知道单词的长度,而且我无法在文本和行之间保持均匀的 space。
我想我可以添加一些空的 div 来让它工作,但我试图避免无用的标记被添加到 DOM
.progress-bar {
display: flex;
width: 100%;
justify-content: space-between;
margin-top: 50px;
}
.progress-bar p {
display: block;
position: relative;
width: 100%;
text-align: center;
line-height: 0.1em;
}
.progress-bar p::after {
content: "";
z-index: -1;
position: absolute;
bottom: 0;
top: 0;
right: 0;
width: 25%;
height: 2px;
background: black;
}
.progress-bar p:last-of-type::after {
background: none;
content: "";
}
<div class="progress-bar">
<p>text one</p>
<p> text two</p>
<p>text three</p>
<p>text four</p>
</div>
您可以使用 gap sintaxe 来创建这些空格:
在你的css中,你可以这样做:
.progress-bar{
display: flex;
flex-orientation: column;
gap: 2rem;
}
在这里您可以了解 flex:
https://www.w3schools.com/css/css3_flexbox.asp
我没有找到使用 flex 和 space-between 的合适方法,因为不知何故我们 'lose' 了信息。关于每个文本的宽度和位置。
所以这是一个使用 inline-blocks、填充和边距的方法。
这里的 hack 是将 inter-text 行画成一条连续的线,然后让每个文本左右空白一些(使用填充和背景颜色)。无论文本有多宽,它们的间距都是均匀的。
此代码段可让您设置 3 个变量,以适应您想要的线宽、文本与线之间的间距以及进度条中的项目数。你可以玩这些来获得你想要的间距。
结果是响应式的,因为较长的文本会在需要时分多行,但文本间行保留在文本的中间。
.progress-bar {
width: 100%;
position: relative;
margin-top: 50px;
background-image: linear-gradient(black, black);
background-size: 100% 2px;
background-position: 0 calc(50% - 1px);
background-repeat: no-repeat no-repeat;
display: inline-flex;
align-items: center;
justify-content: center;
overflow: hidden;
/* alter these to suit your use-case */
--lineW: 6%;
/* width of the lines between texts */
--gapW: 3%;
/* width of the gap between text and line */
--n: 4;
/* number of items in the progress bar */
}
p {
display: inline-block;
position: relative;
background: white;
text-align: center;
padding: 0 var(--gapW);
margin: 0 calc(var(--lineW) / 2);
max-width: calc((100% - (var(--n) * (var(--gapW) + var(--lineW)))) / var(--n));
}
p:first-child::before,
p:last-child::after {
content: '';
display: inline-block;
margin-right: 0;
position: absolute;
width: 100vw;
background: white;
z-index: 1;
height: 100%;
}
p:first-child::before {
right: 100%;
}
p:last-child::after {
left: 100%;
}
<div class="progress-bar">
<p>text one</p>
<p>text two</p>
<p>text three is noticeably wider</p>
<p>text four</p>
</div>
最简单的解决方案是对文本使用 display:contents,该行也将成为 flexbox 容器的一部分
.progress-bar {
display: flex;
justify-content: space-around;
align-items:center;
margin-top: 50px;
}
.progress-bar p {
display: contents;
text-align: center;
line-height: 0.1em;
}
.progress-bar p:not(:last-of-type)::after {
content: "";
width: 8%;
height: 2px;
background: black;
}
<div class="progress-bar">
<p>text one</p>
<p> text two</p>
<p>text three</p>
<p>text four</p>
</div>
也喜欢下面:
.progress-bar {
display: flex;
justify-content: center;
align-items: center;
margin-top: 50px;
}
.progress-bar p {
display: contents;
text-align:center;
line-height: 0.1em;
}
.progress-bar p:not(:last-of-type)::after {
content: "";
width: 8%;
margin:0 3%;
height: 2px;
background: black;
}
<div class="progress-bar">
<p>text one</p>
<p> text two</p>
<p>text three</p>
<p>text four</p>
</div>
正在寻找一种在一组单词之间创建线条的方法。我能够让它有点工作,但我不会总是知道单词的长度,而且我无法在文本和行之间保持均匀的 space。
我想我可以添加一些空的 div 来让它工作,但我试图避免无用的标记被添加到 DOM
.progress-bar {
display: flex;
width: 100%;
justify-content: space-between;
margin-top: 50px;
}
.progress-bar p {
display: block;
position: relative;
width: 100%;
text-align: center;
line-height: 0.1em;
}
.progress-bar p::after {
content: "";
z-index: -1;
position: absolute;
bottom: 0;
top: 0;
right: 0;
width: 25%;
height: 2px;
background: black;
}
.progress-bar p:last-of-type::after {
background: none;
content: "";
}
<div class="progress-bar">
<p>text one</p>
<p> text two</p>
<p>text three</p>
<p>text four</p>
</div>
您可以使用 gap sintaxe 来创建这些空格:
在你的css中,你可以这样做:
.progress-bar{
display: flex;
flex-orientation: column;
gap: 2rem;
}
在这里您可以了解 flex: https://www.w3schools.com/css/css3_flexbox.asp
我没有找到使用 flex 和 space-between 的合适方法,因为不知何故我们 'lose' 了信息。关于每个文本的宽度和位置。
所以这是一个使用 inline-blocks、填充和边距的方法。
这里的 hack 是将 inter-text 行画成一条连续的线,然后让每个文本左右空白一些(使用填充和背景颜色)。无论文本有多宽,它们的间距都是均匀的。
此代码段可让您设置 3 个变量,以适应您想要的线宽、文本与线之间的间距以及进度条中的项目数。你可以玩这些来获得你想要的间距。
结果是响应式的,因为较长的文本会在需要时分多行,但文本间行保留在文本的中间。
.progress-bar {
width: 100%;
position: relative;
margin-top: 50px;
background-image: linear-gradient(black, black);
background-size: 100% 2px;
background-position: 0 calc(50% - 1px);
background-repeat: no-repeat no-repeat;
display: inline-flex;
align-items: center;
justify-content: center;
overflow: hidden;
/* alter these to suit your use-case */
--lineW: 6%;
/* width of the lines between texts */
--gapW: 3%;
/* width of the gap between text and line */
--n: 4;
/* number of items in the progress bar */
}
p {
display: inline-block;
position: relative;
background: white;
text-align: center;
padding: 0 var(--gapW);
margin: 0 calc(var(--lineW) / 2);
max-width: calc((100% - (var(--n) * (var(--gapW) + var(--lineW)))) / var(--n));
}
p:first-child::before,
p:last-child::after {
content: '';
display: inline-block;
margin-right: 0;
position: absolute;
width: 100vw;
background: white;
z-index: 1;
height: 100%;
}
p:first-child::before {
right: 100%;
}
p:last-child::after {
left: 100%;
}
<div class="progress-bar">
<p>text one</p>
<p>text two</p>
<p>text three is noticeably wider</p>
<p>text four</p>
</div>
最简单的解决方案是对文本使用 display:contents,该行也将成为 flexbox 容器的一部分
.progress-bar {
display: flex;
justify-content: space-around;
align-items:center;
margin-top: 50px;
}
.progress-bar p {
display: contents;
text-align: center;
line-height: 0.1em;
}
.progress-bar p:not(:last-of-type)::after {
content: "";
width: 8%;
height: 2px;
background: black;
}
<div class="progress-bar">
<p>text one</p>
<p> text two</p>
<p>text three</p>
<p>text four</p>
</div>
也喜欢下面:
.progress-bar {
display: flex;
justify-content: center;
align-items: center;
margin-top: 50px;
}
.progress-bar p {
display: contents;
text-align:center;
line-height: 0.1em;
}
.progress-bar p:not(:last-of-type)::after {
content: "";
width: 8%;
margin:0 3%;
height: 2px;
background: black;
}
<div class="progress-bar">
<p>text one</p>
<p> text two</p>
<p>text three</p>
<p>text four</p>
</div>