简体   繁体   English

垂直居中后将水平跨度居中

[英]Centering a span horizontally after centering it vertically

After going through the long process of learning how to vertically centre a span in a div, at the end, my text content is no longer centered horizontally despite having set text-align: center. 经过学习如何在div中垂直居中居中的漫长过程之后,最后,尽管设置了text-align:center,我的文本内容也不再水平居中。 Also, very strangely to note, the horizontal centering DOES WORK only when the text within the span wraps to a second line. 另外,非常奇怪的是,水平居中仅在跨度内的文本换行到第二行时才起作用。

enter code here

<div class="tier_blurb dh"> 
   <div class="blurb_title">
      <span>Assign ownership</span>
   </div>Engage your team to align business process responsibilities to their respective owners while performing initial cost benefits analysis on the business opportunities available</div>
</div>

.blurb_title {
    text-align: center;
    vertical-align: middle;
    text-transform: uppercase;
    font-weight: 700;
    font-size: 0.95em;
    letter-spacing: -1px;
    margin-bottom: 10px;
    height: 43px;
    display: table-cell;
}

.blurb_title span {
    vertical-align: middle;
}

You need to define width attribute for .blurb_title because any table cell without specified width will get width of its content. 您需要为.blurb_title定义width属性,因为任何未指定宽度的表单元格都会获得其内容的宽度。

 .blurb_title { vertical-align: middle; text-align: center; text-transform: uppercase; font-weight: 700; font-size: 0.95em; letter-spacing: -1px; margin-bottom: 10px; height: 43px; display: table-cell; border: 1px solid green; width: 100vw; } 
 <div class="tier_blurb dh"> <div class="blurb_title"> <span>Assign ownership</span> </div>Engage your team to align business process responsibilities to their respective owners while performing initial cost benefits analysis on the business opportunities available</div> </div> 

You could use Flex-box: 您可以使用Flex-box:

.blurb_title {
    align-items: center;
    display: flex;
    vertical-align: middle;
}

请注意,如果未指定width属性或将其设置为100%,则中心对齐将不起作用

Follow the references CSS Flex-box and CSS Centering Things 遵循参考CSS Flex-boxCSS Centering Things

You can also use CSS Flex-box (My Recommendation) 您还可以使用CSS Flex-box(我的建议)

display: flex; align-items: center; justify-content: center;

 .blurb_title { border: 1px solid green; text-align: center; line-height: 50px; font-weight: bold; text-transform: uppercase; } 
 <div class="tier_blurb dh"> <div class="blurb_title"> <span>Assign ownership</span> </div> Engage your team to align business process responsibilities to their respective owners while performing initial cost benefits analysis on the business opportunities available </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM