绝对定位解决方案
在这个片段中,我们将尝试在 CSS :before 伪元素中居中文本,从而使文本位于 <span> 元素的中间。
我们需要使用众所周知的居中技术(使用顶部、左侧和变换属性)绝对定位 :before 伪元素。
其中-25px 将偏移圆圈上方的文本。
请注意,对于 <span> 元素,我们将位置属性设置为其“相对”值,并将显示设置为“block”。
在 :before 伪元素中居中文本的示例:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <style> span { border-radius: 50%; background-color: #c7dbc1; border: 4px solid #47962f; width: 25px; height: 25px; margin: 30px 0 0 40px; display: block; position: relative; } span:before { content: attr(data-value); position: absolute; white-space: pre; display: inline; top: 0; left: 50%; transform: translate(-50%, -25px); } </style> </head> <body> <span data-value="生活终归还得继续。"></span> </body> </html>
日期:2020-06-02 22:14:58 来源:oir作者:oir