简体   繁体   English

如何与显示水平对齐:内嵌

[英]How to horizontally align with display:inline-block

Seems super simple but I am unable to horizontally center text in a parent element while using display:inline-block on the text. 似乎超级简单,但在文本上使用display:inline-block时,无法在父元素中水平居中放置文本。 I need it to be display:inline-block because I want the border around the text I create to be the size of the text and NOT the parent. 我需要将其display:inline-block因为我希望创建的文本周围的边框为文本的大小,而不是父项的大小。

Is there another way to make the border I give the text the width of the text and not the parent? 有没有其他方法可以使边框的宽度变为文本的宽度而不是父级的宽度?

  a.studio-nav { font-family: 'brandon-reg'; font-weight: 900; color: white; font-size: 20px; display: inline-block; text-align: center; margin:0 auto; padding:7px 4px; } a.studio-nav:hover { border:2px solid white; -webkit-border-radius: 7px; -moz-border-radius: 7px; border-radius: 7px; text-decoration: none; } 
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="row clearfix"> <div class="col-xs-4 col-md-4"> <a href="/" class="studio-nav"> text </a> </div> <div class="col-xs-4 col-md-4"> <a href="/" class="studio-nav"> I want </a> </div> <div class="col-xs-4 col-md-4"> <a href="/" class="studio-nav"> centered </a> </div> </div> 

Could it be as simple as this? 这样简单吗?

.row {
    text-align: center;
}

Example fiddle: https://jsfiddle.net/rczfnf6d/ 小提琴示例: https : //jsfiddle.net/rczfnf6d/

Bootstrap has also a quick way of achieving this with an alignment class called ".text-center" Bootstrap还具有一种通过称为“ .text-center”的对齐类来实现此目的的快速方法。

<div class="col-xs-4 col-md-4 text-center"> <!-- class added-->
    <a href="/" class="studio-nav">
        text
    </a>
</div>

Try 尝试

display: inline-table;

instead of 代替

display: inline-block;

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

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