我正在接收API的内容,该API包含几个span元素,后面跟着未标记的文本。
示例:
<span>1</span> Some text. <span>2</span> Some text.我想要跨度有一个固定的宽度和线中断之前。
示例:
1 Some text.
2 Some text.span上的"display: block“允许宽度属性,但在每个跨度之后也会创建一个中断。
“显示:内联块”允许宽度属性,但我不知道如何在此之前创建行中断。
将跨度保留为"display: inline“,允许在每个跨度之前中断行(使用::)。但是,您不能为内联元素指定宽度属性(据我所见)。
发布于 2019-03-27 09:49:02
如果为该内容设置了包装器,则可以将其设置为网格容器,并轻松实现所需的功能:
.wrapper {
display:grid;
grid-template-columns:30px 1fr;
}
span {
text-align:center;
outline:1px solid green;
}<div class="wrapper">
<span>1</span> Some text. <span>2</span> Some text.
</div>
柔性盒和display:contents (https://caniuse.com/#feat=css-display-contents)的另一个想法
.wrapper {
display:flex;
flex-wrap:wrap;
padding-left:10px;
}
span {
display:contents;
outline:1px solid green;
}
/*create line break*/
span:before {
content:"";
flex-basis:100%;
}
/*control the width*/
span:after {
content:"";
width:20px;
}<div class="wrapper">
<span>1</span> Some text. <span>2</span> Some text.
</div>
这里还有另一种使用计数器的方法:
body {
counter-reset: count;
}
span {
counter-increment: count;
font-size:0; /*remove default content*/
}
/*add line break*/
span::before {
content: "\A";
white-space: pre;
}
/*add content with counter*/
span::after {
content:counter(count);
font-size:initial;
display:inline-block;
width:30px; /*control the width*/
text-align:center;
}<span>1</span> Some text. <span>2</span> Some text.
发布于 2019-03-27 04:05:27
为了达到预期的效果,请使用下面的内容和空白选项。
span::before {
content: "\A";
white-space: pre;
}<span>1</span> Some text. <span>2</span> Some text.
https://stackoverflow.com/questions/55369535
复制相似问题