简体   繁体   English

垂直对齐按钮中间文本旁边

[英]Vertical align button middle next to text

I am trying to vertically align the button to the middle, so it fits better with the text. 我试图将按钮垂直对齐到中间,因此它更适合文本。 I have tried center-block and text-center without any luck. 我试过没有运气的中心区和文本中心。 I would like a generic solution so I do not hard-code margin, padding and similar. 我想要一个通用的解决方案,所以我不硬编码边距,填充和类似。

Here is my fiddle: http://jsfiddle.net/jhqjumgL/5/ And my code: 这是我的小提琴: http//jsfiddle.net/jhqjumgL/5/我的代码:

<h3>FMUs
  <button type="button" class="btn btn-default btn-xs">
    <span class="glyphicon glyphicon-plus"></span>
  </button>
</h3>

You can use Flexbox 您可以使用Flexbox

 h3 { display: flex; align-items: center; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <h3>FMUs <button type="button" class="btn btn-default btn-xs"> <span class="glyphicon glyphicon-plus"></span> </button> </h3> 

You could wrap the "FMUs" text in an element and vertical-align that as well: 您可以将“FMU”文本包装在元素中并vertical-align它:

 h3 > span { vertical-align: middle; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <h3><span>FMUs</span> <button type="button" class="btn btn-default btn-xs"> <span class="glyphicon glyphicon-plus"></span> </button> </h3> 

The vertical-align property is relative to siblings, not to the container. vertical-align属性是相对于兄弟,而不是容器。

What may be of interest is to divide the area up further so you have more control: 可能感兴趣的是将区域进一步划分,以便您拥有更多控制权:

    <h3>
  <div class="container">

  <span class="text"> 
   FMUs

  </span>
  <span class=="button">
  <button type="button" class="btn btn-default btn-xs">
    <span class="glyphicon glyphicon-plus"></span>
  </button>
  </span>
  </div>
</h3>

And the styles: 和风格:

button,
textarea {
  vertical-align: middle;
}
.container {
  display: inline-block;
  height: 100px;
}
.text, .button {
  vertical-align: middle;
}

What this allows is a better control over your elements and means that you can specify better position as a result. 这允许更好地控制元素,这意味着您可以指定更好的位置。

The fiddle for you to try is here: 您尝试的小提琴在这里:

fiddle 小提琴

You can add margin-bottom css-property to button element, for example margin-bottom: 5px; 您可以将margin-bottom css-property添加到button元素,例如margin-bottom: 5px; http://jsfiddle.net/jhqjumgL/26/ http://jsfiddle.net/jhqjumgL/26/

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

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