如何在引导程序中垂直对齐div的中心?

时间:2019-03-31 12:24:08

标签: html css css3 twitter-bootstrap-3

您可以看到我的元素不在垂直对齐的中心。如何根据主div块中的最大元素垂直对齐一行中所有元素的中心位置?

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div style="">
                <div class="col-md-6" style="padding-left: 0;">
                    <div class="form-group">
                        <label style="margin-right: 10px">Marital Status:</label>
                        <div class="radio-inline" style="padding-left: 0;">
                            <label class="radio-inline"><input type="radio" name="marital_status" value="Single">Single</label>
                        </div>
                        <div class="radio-inline">
                            <label class="radio-inline"><input type="radio" name="marital_status" value="Married">Married</label>
                        </div>
                    </div>
                </div>
                <div class="col-md-6" style="padding-right: 0;">
                    <div class="row">
                        <div class="col-md-8">
                            <div class="form-group">
                                <label for="address_1">Hobby:</label>
                                <input type="text" class="form-control" name="hobbies[]">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <button type="button" class="btn btn-primary btn-block">+</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我只在代码中添加了几个 CSS 。试试这个,希望对您有所帮助。谢谢

html, 
body {
  height: 100%;
}

.verticalAlignContainer {
  display: flex;
  align-items: center;
  height: 100%;
}

.verticalAlignContainer .row {
  width: 100%;
}

,并将verticalAlignContainer类与container一起使用。

<div class="container verticalAlignContainer">

html, 
body {
  height: 100%;
}

.verticalAlignContainer {
  display: flex;
  align-items: center;
  height: 100%;
}

.verticalAlignContainer .row {
  width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container verticalAlignContainer">
    <div class="row">
        <div class="col-md-12">
            <div style="">
                <div class="col-md-6" style="padding-left: 0;">
                    <div class="form-group">
                        <label style="margin-right: 10px">Marital Status:</label>
                        <div class="radio-inline" style="padding-left: 0;">
                            <label class="radio-inline"><input type="radio" name="marital_status" value="Single">Single</label>
                        </div>
                        <div class="radio-inline">
                            <label class="radio-inline"><input type="radio" name="marital_status" value="Married">Married</label>
                        </div>
                    </div>
                </div>
                <div class="col-md-6" style="padding-right: 0;">
                    <div class="row">
                        <div class="col-md-8">
                            <div class="form-group">
                                <label for="address_1">Hobby:</label>
                                <input type="text" class="form-control" name="hobbies[]">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <button type="button" class="btn btn-primary btn-block">+</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>