Bootstrap 3:垂直居中的按钮

时间:2013-11-21 12:56:43

标签: html css twitter-bootstrap twitter-bootstrap-3

我希望在列中垂直居中一组按钮(参见图片)。行的高度是动态的。我尝试了很多我发现的东西,但没有任何作用。

屏幕截图:https://www.dropbox.com/s/ztogupb46o1rfym/bootstrap.PNG

<div class="row question even">
    <div class="col-sm-7 col-md-9 text r-text">
        <div>
            dynamic content
        </div>
    </div>

    <div class="col-sm-5 col-md-3 r-answer" id="radio">
        <input class="answerradio" id="question3" name="question3" type="hidden" value="4">

        <div class="btn-group" data-target="question3" data-toggle="buttons-radio">
            <button class="btn btn-primary" data-toggle="button" type="button" value="1">1</button> <button class="btn btn-primary" data-toggle="button" type="button" value="2">2</button>
            <button class="btn btn-primary" data-toggle="button" type="button" value="3">3</button> <button class="btn btn-primary" data-toggle="button" type="button" value="4">4</button>
            <button class="btn btn-primary" data-toggle="button" type="button" value="5">5</button>
        </div>
    </div>
</div>

以下是我的整个代码:http://bootply.com/95760

有一个想法吗?

我正在使用Bootstrap 3。

提前致谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

.question{
    position: relative;
}
.r-answer{
    position: static;
}
.r-answer .btn-group{
    position: absolute;
    top: 50%;
    margin-top: -17px;
}

只要按钮的大小保持不变,这将使它们对齐中心,否则你需要使用javascript来修复它。

答案 1 :(得分:0)

有点晚了但是尝试使用button-group-vertical类而不是button-group:

    <div class="btn-group-vertical" data-target="question3" data-toggle="buttons-radio">
        <button class="btn btn-primary" data-toggle="button" type="button" value="1">1</button> <button class="btn btn-primary" data-toggle="button" type="button" value="2">2</button>
        <button class="btn btn-primary" data-toggle="button" type="button" value="3">3</button> <button class="btn btn-primary" data-toggle="button" type="button" value="4">4</button>
        <button class="btn btn-primary" data-toggle="button" type="button" value="5">5</button>
    </div>

Here是修改后的代码