gpt4 book ai didi

javascript - 使选择器有助于元素宽度

转载 作者:行者123 更新时间:2023-11-30 09:23:18 25 4
gpt4 key购买 nike

我正在尝试在文本上方添加吉他和弦,就像在 this question 中一样.虽然该技术在大多数情况下都有效,但我想将其扩展到也适用于以下情况,即和弦重叠:

p {
margin-top:50px;
}
span.chunk {
position:relative;
}
span.chunk:before {
content:attr(data-chord);
position:absolute;
top:-15px;
}
<p>
As
<span class="chunk" data-chord="C">I was going over the</span>
<span class="chunk" data-chord="Am">f</span>
<span class="chunk" data-chord="B">ar</span>
</p>

最终结果应该简单地将整个 block 推过去,如下所示:

enter image description here

有什么方法可以实现这一点,最好只使用 CSS?

最佳答案

这是一个使用 flexbox 的想法。诀窍是让 span 成为具有列方向的 inline-flex 容器,这样我们就可以保持伪元素流入;因此它会影响容器的宽度。

当前内容和伪元素之间最宽的一个将定义宽度。

p {
margin-top: 50px;
}

span.chunk {
position: relative;
display: inline-flex;
flex-direction: column;
vertical-align: bottom; /*This is mandatory to keep the text without chunk in the bottom*/
}

span.chunk:before {
content: attr(data-chord);
position: relative;
}
<p>
As
<span class="chunk" data-chord="CCC">I was going over the</span>
<span class="chunk" data-chord="Am a long text">f</span>
more text here
<span class="chunk" data-chord="BB">ar</span>
</p>

关于javascript - 使选择器有助于元素宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50359121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com