使用Bootstrap将两个按钮居中

时间:2014-09-27 15:36:54

标签: twitter-bootstrap twitter-bootstrap-3

我正在尝试使用Bootstrap将两个表单按钮居中,但我无法让它工作!

这是我的代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Form</title>

    <!-- Bootstrap -->
    <link href="static/fw/css/bootstrap.min.css" rel="stylesheet">
    <link href="static/fw/css/bootstrap-theme.min.css" rel="stylesheet">

    <!-- Custom styles -->
    <link href="static/code/style.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>


    <div class="container" role="main">

        <div  class="center-block">
            <button type="submit" class="btn btn-success">Save</button>
            <button type="submit" class="btn btn-danger">Cancel</button>
        </div>


    </div> <!-- /container -->

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="static/fw/js/bootstrap.min.js"></script>
    <script src="static/code/script.js"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:5)

将.text-center添加到包含按钮的div。

http://jsbin.com/sosab/1/edit?html,output

答案 1 :(得分:3)

您需要添加行和col类,也使用文本中心

    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <div  class="text-center">
                    <button type="submit" class="btn btn-success">Save</button>
                    <button type="submit" class="btn btn-danger">Cancel</button>
                </div>
            </div>
        </div>
    </div>