简体   繁体   English

如何在Bootstrap 3中将一个div与另一个对齐?

[英]How do I align one div to another in Bootstrap 3?

I have the following html. 我有以下html。 My label is not aligned vertically in the center compare to the input field. input字段相比,我的label在中心未垂直对齐。

<div class="row">
    <div class="col-sm-5">
        <label for="investmentbalance" class="control-label pull-right">Investment Balance</label>
    </div>
    <div class="col-sm-2">
        <input type="text" readonly="readonly" value="28500.00" class="form-control" name="investmentbalance" id="investmentbalance">
    </div>
</div>

How can I set both input and label have the same height ? 如何设置inputlabel的高度相同?

JSFiddle JSFiddle

https://jsfiddle.net/mLcrv19z/ https://jsfiddle.net/mLcrv19z/

You can use both padding or line-height . 您可以同时使用paddingline-height

Add to label : 添加到label

padding: 7px;

or: 要么:

line-height: 34px;

The input height is 20px; 输入高度为20px; the padding 6 and the margin 1. So here's the calculation: 边距6和边距1。因此,计算如下:

padding : 6 + 1 = 7px 填充 :6 +1 = 7px

line-height : 20 + 7 (top) + 7 (bottom) = 34px 行高 :20 + 7(顶部)+ 7(底部)= 34px

To get the two elements (label and input) to align vertically, try using the form-inline class, along with form-group . 要使两个元素(标签和输入)垂直对齐,请尝试使用form-inline类以及form-group Here's what that looks like: 看起来像这样:

<div class="form-inline">
        <div class="form-group">
            <label for="investmentbalance">Investment Balance</label>
            <input type="text" readonly="readonly" value="28500.00" class="form-control" name="investmentbalance" id="investmentbalance" />
        </div>
</div>

https://jsfiddle.net/mLcrv19z/10/ https://jsfiddle.net/mLcrv19z/10/

You may use margin-top:3%; 您可以使用margin-top:3%; to the label CSS label CSS

The value is based on the jsfiddle example. 该值基于jsfiddle示例。 It may change depending on the application you are developing. 它可能会根据您正在开发的应用程序而变化。

Issue is not with your alignment but the classes you have defined. 问题不在于对齐方式,而在于您定义的类。 You need to define bootstrap grid class for every width. 您需要为每个宽度定义bootstrap grid类。 Otherwise grid would appear different in different resolutions. 否则,网格将以不同的分辨率出现不同。 Check this out. 看一下这个。

<div class="col-sm-10 col-md-10 col-xs-10">

<div class="col-sm-2 col-md-2 col-xs-2">

Fiddle Solution 小提琴解决方案

Another solution is to use a custom class, setting it's line-height equal to the parent's height and aligning it in the middle. 另一种解决方案是使用自定义类,将其line-height设置为等于父级的高度,并在中间对齐。

 .middle { vertical-align: middle; line-height: 34px; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <div class="container-fluid"> <div class="row middle"> <div class="col-xs-5"> <label for="investmentbalance" class="control-label pull-right">Investment Balance</label> </div> <div class="col-xs-7"> <input type="text" readonly="readonly" value="28500.00" class="form-control" name="investmentbalance" id="investmentbalance"> </div> </div> </div> 

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

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