简体   繁体   English

如何在按钮旁边对齐此文本?

[英]How to middle-align this text next to a button?

jsFiddle 的jsfiddle

screenshot 截图

html HTML

<div class="client-box">
  <div class="box-header clearfix">
    <h6 class="pull-left">General Information</h6>
    <button class="btn btn-small pull-right"><i class="icon-pencil"></i> Edit</button>
  </div>
  <p>box content</p>
</div>

How can I get the header <h6> vertically-centered with respect to the Edit button? 如何相对于“编辑”按钮将标题<h6>垂直居中?

Putting vertical-align on the box-header doesn't work. vertical-align放在box-header上不起作用。 I don't want to hard-code a line-height because I might change the button sizing or something later and then it will break. 我不想对行高进行硬编码,因为我可能会更改按钮大小或稍后的某些内容,然后它会中断。

You could use css display: table and display: table-cell and vertical-align: middle . 您可以使用css display: tabledisplay: table-cellvertical-align: middle

HTML HTML

<div class="box-header clearfix">
    <div class="left-cell">
        <h6>General Information</h6>
    </div>
    <div class="right-cell">
        <button class="btn btn-small"><i class="icon-pencil"></i> Edit</button>
    </div>
</div>

CSS CSS

.box-header {
    border-bottom: 1px solid #aac7ef;
    padding-bottom: 5px;
    display: table;
    width: 100%;
}
.left-cell {
    display: table-cell;
    vertical-align: middle;
}
.right-cell {
    display: table-cell;
    vertical-align: middle;
    text-align: right;
}

Demo 演示

vertical-align only applies to two type of objects: 1, table cells and 2, inline elements. vertical-align仅适用于两种类型的对象:1,表格单元格和2,内联元素。

It has no bearing on any other type of HTML element. 它与任何其他类型的HTML元素无关。

It's not that it's 'spotty' or 'a problem' it's just that it doesn't do what most people want it to do. 并不是说它“不稳定”或“问题”只是因为它没有做到大多数人希望它做的事情。

Ideally, you'd avoid vertically aligning text altogether, but sometimes we need to. 理想情况下,您可以完全避免垂直对齐文本,但有时我们需要。 There are various ways to do it and as for deciding which way to do it, it usually depends on the particular situation you're working in. 有多种方法可以做到这一点,至于决定采用哪种方式,通常取决于您正在处理的特定情况。

In this situation, if you can rely on it being one line of text, I think your best bet is to just optically adjust with either some extra top padding, or position-relative and drop it down a few pixels. 在这种情况下,如果你可以依赖它是一行文本,我认为你最好的选择就是用一些额外的顶部填充或position-relative光学调整,然后将其降低几个像素。

if you don't want to hard code it, maybe try set it dynamically. 如果您不想硬编码,可以尝试动态设置它。

$(function () {
  $('h6.pull-left').css('line-height', $('button.btn.pull-right').outerHeight() + 'px');
});

I created a jsfiddle 我创造了一个jsfiddle

you are using pull-right and pull-left which floats the elements left and right and so vertical alignment will not work in that case. 你正在使用pull-rightpull-left pull-right的元素,因此在这种情况下垂直对齐将不起作用。 Best you can do is play with the padding, or as others suggested set line-height on your h6 element to something like 24px. 你可以做的最好是使用填充,或者像其他人建议你的h6元素上设置的line-height为24px。 And yes vertical-align:middle is spotty at best, and never satisfying to those who seek pixel perfection 并且是vertical-align:middle充其量是不稳定的,并且永远不会满足那些寻求像素完美的人

yeah..vertical alignment has been a problem in css. 是的..垂直对齐在css中一直是个问题。 So what i suggest you is to set the margin-top of the box-header... like margin-top:1px .. adjusting the box header wrt margin top will solve the problem. 所以我建议你设置盒头的边缘顶部...像margin-top:1px ..调整盒头wrt边缘顶部将解决问题。 Also you can check with the Firebug in Firefox that by how many pixels u need to set the margin-top. 您还可以使用Firefox中的Firebug查看您需要设置margin-top的像素数。

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

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