我希望我的红色矩形位于数字的中间,为此我添加了一个样式为div
的包装display:table
,box div
样式为display:table-cell, vertical-align:middle
。以下是我的HTML
<div class="repeat_div" style="border-right: 0px solid rgb(158, 158, 158); padding: 0px 15px; float: left;">
<div class="topLabel" style="margin-left: 25px; font-size: 12px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158); background-color: transparent;"><span>Youtube Top</span></div>
<div class="boxStatsContainer" style="float: left;">
<div style="float: left; display:table">
<div class="box" style="float: left; background-color: rgb(235, 134, 79); width: 15px; height: 20px; display:table-cell; vertical-align:middle"></div>
</div>
<div class="stats" style="font-size: 48px; float: left; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: black;"><span style="margin-left: 10px;">$620</span></div>
</div>
<div class="bottomLabel" style="font-size: 10px; margin-left: 25px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158);"><span>Youtube</span></div>
</div>
&#13;
但是红色框不在中间
答案 0 :(得分:3)
您可以尝试将display: flex; align-items: center
用于boxStatsContainer
div元素;
<div class="repeat_div" style="border-right: 0px solid rgb(158, 158, 158); padding: 0px 15px; float: left;">
<div class="topLabel" style="margin-left: 25px; font-size: 12px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158); background-color: transparent;"><span>Youtube Top</span></div>
<div class="boxStatsContainer" style="float: left; display: flex; align-items: center;">
<div style="float: left; display:table">
<div class="box" style="float: left; background-color: rgb(235, 134, 79); width: 15px; height: 20px; display:table-cell; vertical-align:middle"></div>
</div>
<div class="stats" style="font-size: 48px; float: left; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: black;"><span style="margin-left: 10px;">$620</span></div>
</div>
<div class="bottomLabel" style="font-size: 10px; margin-left: 25px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158);"><span>Youtube</span></div>
</div>
&#13;
答案 1 :(得分:2)
我使用了display: table-cell
和vertical-align : middle
来使其发挥作用。
<div class="repeat_div" style="border-right: 0px solid rgb(158, 158, 158); padding: 0px 15px; float: left;">
<div class="topLabel" style="margin-left: 25px; font-size: 12px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158); background-color: transparent;"><span>Youtube Top</span></div>
<div class="boxStatsContainer" style="float: left;">
<div style="float: none; display:table-cell;vertical-align:middle;">
<div class="box" style="float: left; background-color: rgb(235, 134, 79); width: 15px; height: 20px; display:table-cell; vertical-align:middle"></div>
</div>
<div class="stats" style="font-size: 48px; float: none;display: table-cell; vertical-align: middle; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: black;"><span style="margin-left: 10px;">$620</span></div>
</div>
<div class="bottomLabel" style="font-size: 10px; margin-left: 25px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158);"><span>Youtube</span></div>
</div>
&#13;
答案 2 :(得分:2)
将display:inline-block
和vertical-align:middle
作为中心。
<div class="repeat_div" style="border-right: 0px solid rgb(158, 158, 158); padding: 0px 15px; float: left;">
<div class="topLabel" style="margin-left: 25px; font-size: 12px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158); background-color: transparent;"><span>Youtube Top</span></div>
<div class="boxStatsContainer" style="float: left;">
<div style="display: inline-block; vertical-align:middle">
<div class="box" style=" float:left;background-color: rgb(235, 134, 79); width: 15px; height: 20px; display:table-cell; vertical-align:middle"></div>
</div>
<div class="stats" style="font-size: 48px; display: inline-block; vertical-align:middle; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: black;"><span style="margin-left: 10px;">$620</span></div>
</div>
<div class="bottomLabel" style="font-size: 10px; margin-left: 25px; font-family: Arial; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; text-transform: none; color: rgb(158, 158, 158);"><span>Youtube</span></div>
</div>
&#13;