html - 如何在未知大小的文本父对象上居中伪元素
问题描述
我正在尝试围绕段落中出现的数字画圈。数字将是一位数或两位数。我希望这些圆圈不会影响我的文本的行高。
我可以对绝对定位进行硬编码,如果数字是一位数,它可以正常工作,但相同的值不适用于两位数。有没有办法定位伪元素,无论它是一位数还是两位数,它都会居中?
.container {
max-width: 600px;
padding: 32px;
background-color: #DDDDDD;
}
p {
font-size: 1.15em;
font-family: Proxima Nova, sans-serif;
line-height: 1.5;
}
.number-pick--in-text {
display: inline-block;
position: relative;
z-index: 1;
margin: 0 10px;
}
.number-pick--in-text::before {
content: '';
position: absolute;
z-index: -1;
width: 1.5em;
height: 1.5em;
top: -1px;
right: -12px;
border-radius: 50%;
border: 1px solid #79818B;
}
<section class="container">
<p class="article--text">
Brandon used his own birthday for his lucky lotto numbers. He picked
<span class="number-pick--in-text">1</span>
<span class="number-pick--in-text">3</span>
<span class="number-pick--in-text">9</span>
<span class="number-pick--in-text">12</span>
<span class="number-pick--in-text">43</span>
<span class="number-pick--in-text">4</span>.
He bought his ticket at a local convience store and found out the next day that he won the $150 million jackpot.
</p>
</section>
解决方案
你真的不需要伪垂直对齐+大小就可以了
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
vertical-align
CSS 属性设置内联或表格单元框的垂直对齐方式。
.container {
max-width: 600px;
padding: 32px;
background-color: #DDDDDD;
}
p {
font-size: 1.15em;
font-family: Proxima Nova, sans-serif;
line-height: 1.5;
}
.number-pick--in-text {
display: inline-block;
vertical-align:middle;/* adjust value to your needs */
text-align:center;
width: 1.5em;
line-height: 1.5em;
border-radius: 50%;
border: 1px solid #79818B;
}
<section class="container">
<p class="article--text">
Brandon used his own birthday for his lucky lotto numbers. He picked
<span class="number-pick--in-text">1</span>
<span class="number-pick--in-text">3</span>
<span class="number-pick--in-text">9</span>
<span class="number-pick--in-text">12</span>
<span class="number-pick--in-text">43</span>
<span class="number-pick--in-text">4</span>.
He bought his ticket at a local convience store and found out the next day that he won the $150 million jackpot.
</p>
</section>
推荐阅读
- python - 如何在 python 中访问字典的第二个元素?
- javascript - Cypress.io 在自定义登录命令中发出异步请求
- android - Appium - 无法在字段中输入文本
- java - 我正在制作一个应用程序,我需要使用 Parcelable 接口,但面临问题,不知道如何实现它
- c# - C# 我应该将 bool 初始化为 false 还是应该在 else 语句中将其设为 false?最佳实践?
- r - 使用 R 在逻辑回归中将交互的特定组合作为变量
- python - Qtreewidget Pyqt5如何恢复正常?
- microsoft-graph-api - 团队 listChannel Graph API 导致“未经授权的错误 - “无法执行 Aad 后端请求 GetUsersByObjectIdsRequest”
- html - 如何使具有定位元素的块响应?
- python - 尝试从 Gmail 中阅读具有特定主题的电子邮件