【问题标题】:Three equal sized buttons in BootstraperBootstrap 中三个大小相等的按钮
【发布时间】:2012-11-06 21:43:26
【问题描述】:

我已经开始使用引导程序。我想要一个带有三个按钮的行。按钮应具有相同的高度和宽度。我怎样才能做到这一点?

我想出了以下方法,但这给了我不同的按钮高度。

<div class="row-fluid">
    <div class="span2 offset1">
        <a href="#" class="btn btn-info btn-block">
            <div class="buttonbody">
                <img src=".." />
                <p>Button1<br />Second row<p>
            </div>
        </a>
    </div>
    <div class="span2 offset1">
        <a href="#" class="btn btn-info btn-block">
            <div class="buttonbody">
                <img src=".." />
                <p>Button2<p>
            </div>
        </a>
    </div>
    <div class="span2 offset1">
        <a href="#" class="btn btn-info btn-block">
            <div class="buttonbody">
                <img src=".." />
                <p>Button3<p>
            </div>
        </a>
    </div>
</div>

【问题讨论】:

    标签: html css twitter-bootstrap


    【解决方案1】:

    要获得相同的高度,您必须使用 jQuery 计算最大值,然后将其应用于应该具有相同高度的所有元素:

    var selector = ".row-fluid .buttonbody";
    var maxHeight = 0;
    $(selector).each(function() {
        var selfHeight = $(this).height();
        if (selfHeight > maxHeight) {
            maxHeight = selfHeight;
        }
    });
    // set the same height on all elements
    $(selector).height(maxHeight);
    

    更新:要在每次调整窗口大小时调整按钮大小,您可以执行以下操作:

    // declare a function to be called
    // each time buttons need to be resized
    var resizeButtons = function() {
        var selector = ".row-fluid .buttonbody";
        var maxHeight = 0;
        $(selector).each(function() {
            var selfHeight = $(this).height();
            if (selfHeight > maxHeight) {
                maxHeight = selfHeight;
            }
        });
        // set the same height on all elements
        $(selector).height(maxHeight);
    }
    
    $(function() {
        // attach function to the resize event
        $(window).resize(resizeButtons);
    
        // call function the first time window is loaded;
        resizeButtons();
    });
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢!这按预期工作。当页面调整大小时,我可以得到一个事件,以便我可以再次运行此代码吗?
    【解决方案2】:

    简答:http://jsfiddle.net/D2RLR/2942/

    长答案:

    以下代码可以满足您的需求。

    <div class="container">
        <a href="#" class="btn"><strong>Button 1</strong></a>
        <a href="#" class="btn"><strong>Button 2</strong></a>
        <a href="#" class="btn"><strong>Button 3</strong></a>
    </div>​
    

    这里是tutorial

    我建议您通过 twitter 引导文档和bootsnipp.com 了解更多信息。

    从您的 cmets 中,正如您所说,您正在使用 &lt;br/&gt;,您可以使用以下内容:fiddle

    <div class="container">
    <a href="#" class="btn"><strong>Button 1<br/>Second row</strong></a>
    <a href="#" class="btn"><strong>Button 2<br/>&nbsp;</strong></a>
    <a href="#" class="btn"><strong>Button 3<br/>&nbsp;</strong></a>
    </diV>​
    

    【讨论】:

    • 谢谢!但我似乎没有工作。请注意,在第一个按钮中,我有一个
      和第二行。这给了我一个比其他按钮更高的按钮。我希望所有按钮都具有相同的高度!
    • @ErikZ 更新了我的答案,请查看fiddle
    • 你很聪明。 :) 但是如果图像的高度不同怎么办?我的目标是从代码中填充这些按钮,无论它们包含什么,都应该具有相同的高度。有可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多