html - 如何将 "scoreBox"内的元素水平居中?

标签 html css flexbox

如何将“scoreBox”内的元素水平居中?

它们目前只是左对齐。

.voteButtons {
  display: inline-block;
  text-align: center;
}

.HP {
  display: inline-block;
  text-align: center;
  width: auto !important;
  margin-left: 5px !important;
}

.scoreBox {
  display: flex;
  margin-top: 10px;
  padding-top: 10px;
  text-align: center;
  width: auto !important;
}
<div class="scoreBox">
  <span class="voteButtons"><img class="UpvoteButton" id="voteUp" src="..."><img class="DownvoteButton" id="voteDown" src="..."></span>
  <div class="HP">
    <%= post.upvotes - post.downvotes%> score </div>
</div>

最佳答案

justify-content: center 添加到您的 scoreBox - 请参阅下面的演示:

.voteButtons {
  display: inline-block;
  text-align: center;
}

.HP {
  display: inline-block;
  text-align: center;
  width: auto !important;
  margin-left: 5px !important;
}

.scoreBox {
  display: flex;
  margin-top: 10px;
  padding-top: 10px;
  text-align: center;
  width: auto !important;
  justify-content: center;
}
<div class="scoreBox">
  <span class="voteButtons"><img class="UpvoteButton" id="voteUp" src="..."><img class="DownvoteButton" id="voteDown" src="..."></span>
  <div class="HP">
    <%= post.upvotes - post.downvotes%> score </div>
</div>

关于html - 如何将 "scoreBox"内的元素水平居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45761451/

相关文章:

javascript - 在非全屏元素上使用 fullPage.js 插件?

javascript - 根据属性更改选择选项

css - Firefox 和 IE 在 5 个页面中的 3 个页面的导航区域中添加了空间,但不是全部

html - 奇怪的 CSS 过渡问题

php - 如何将 HTML 截断为一定数量的字符?

javascript - HTML标签<audio>在不同浏览器中的表现

php - 带有 PHP 代码的元素显示错误

html - 包裹在 div 中时 SVG 内部 flex 未对齐

html - 在 flexbox 中从纵向布局切换到横向布局

html - 如何设置 div 宽度等于响应式 img