html - 试图垂直居中一些跨度
问题描述
这是我的代码:
<div style="text-align: center">
<span style="display: inline-block; font-size: 38px; float: left; cursor: pointer; color: #ccc">❮</span>
<span style="margin-top: 12px; background: #ccc; border: 1px solid #ccc; border-radius: 50%; height: 13px; width: 13px; cursor: pointer; display: inline-block"></span>
<span style="border: 1px solid #ccc; border-radius: 50%; height: 13px; width: 13px; cursor: pointer; display: inline-block"></span>
<span style="border: 1px solid #ccc; border-radius: 50%; height: 13px; width: 13px; cursor: pointer; display: inline-block"></span>
<span style="display: inline-block; font-size: 38px; float: right; cursor: pointer; color: #ccc">❯</span>
</div>
这是在 JSFiddle 上:https ://jsfiddle.net/nwvxaofc/
第一个和最后一个跨度左右浮动,而中间的三个跨度水平居中。我希望它们也垂直居中,而且我并不立即明白如何做到这一点。
设置margin-top: 12px
似乎没有做任何事情。
如果我设置position: absolute
最外面的 div 然后将三个中间跨度包裹在一个<div>
with中position: relative
,top: 50%
那么浮动就会变得混乱。
解决方案
抛弃所有的浮动display:inline-block
并使用 flexbox
div {
display: flex;
align-items: center;
}
span:first-of-type {
margin-right: auto;
}
span:last-of-type {
margin-left: auto;
}
<div style="text-align: center">
<span style="font-size: 38px; cursor: pointer; color: #ccc">❮</span>
<span style=" background: #ccc; border: 1px solid #ccc; border-radius: 50%; height: 13px; width: 13px; cursor: pointer;"></span>
<span style="border: 1px solid #ccc; border-radius: 50%; height: 13px; width: 13px; cursor: pointer;"></span>
<span style="border: 1px solid #ccc; border-radius: 50%; height: 13px; width: 13px; cursor: pointer;"></span>
<span style=" font-size: 38px; cursor: pointer; color: #ccc">❯</span>
</div>
推荐阅读
- python - 如何通过在 Python 3.8 中的两个文件中对相同关键字的值求和来组合两个非常简单的数据集(关键字、数值)?
- javascript - 网站可以用electronjs吗
- azure-devops - 如何在 AzureDevops SAAS 上恢复已删除的管道
- javascript - Object.assign(module.exports, {...}) 与 module.exports = {...}
- ruby-on-rails - Rails 循环显示重复的结果
- node.js - 如何在单个函数中使用 async/await
- node.js - 无法流式传输和上传图像
- rss - 如何在 Google 新闻 RSS 提要中选择日期范围或使用 Google 搜索中的日期范围创建 RSS 提要?
- python-3.x - opencv 没有正确输出图像
- r - 将 ggplot2 打印重定向到 parLapply 内的单个 pdf 文件