如何在bootstrap 4中集中内联表单?

时间:2018-02-05 11:01:23

标签: html css alignment bootstrap-4

这是我的HTML:



<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    </head>
    <body>
        <center class="container" style="margin:auto; background-color:black;">
            <img src=test + "0" id=image style="width:900px; height:900px;"><br/>
            <form class="form-inline" style="text-align:center;background-color:red;">
                <label id="out">wait...</label>
                <select class="form-control" style="width:111px;" id="sel1">
                    <option>flags 1</option>
                    <option>flags 2</option>
                    <option>people 1</option>
                    <option>people 2</option>
                    <option>people 3</option>
                </select>
                <div class="btn-group">
                    <button class="btn btn-outline-success" type="button" onclick='clearInterval(player);document.getElementById("image").src = "counter.gif";setTimeout(function(){ start(); },5000)'>play</button>
                    <button class="btn btn-outline-danger" type="button" onclick='clearInterval(player);document.getElementById("result").innerHTML = new Date().getTime() - timestamp'>stop</button>
                </div>
                <input class="form-control" type="number" id="delay" style="width:80px;">ms</input>
                <label id='result'></label>
            </form>
        </center>
    </body>
</html>
&#13;
&#13;
&#13;

正如您所看到的,我已经在表单标记上使用了text-align:center,但它仍然无法正常工作。如何让我的表单在页面中心对齐?

为什么我的代码无法正常工作?

2 个答案:

答案 0 :(得分:1)

请立即检查

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    </head>
    <body>
        <center class="container" style="margin:auto; background-color:black;">
            <img src=test + "0" id=image style="width:900px; height:900px;"><br/>
            <form class="form-inline justify-content-center" style="text-align:center;background-color:red;">
                <label id="out">wait...</label>
                <select class="form-control" style="width:111px;" id="sel1">
                    <option>flags 1</option>
                    <option>flags 2</option>
                    <option>people 1</option>
                    <option>people 2</option>
                    <option>people 3</option>
                </select>
                <div class="btn-group">
                    <button class="btn btn-outline-success" type="button" onclick='clearInterval(player);document.getElementById("image").src = "counter.gif";setTimeout(function(){ start(); },5000)'>play</button>
                    <button class="btn btn-outline-danger" type="button" onclick='clearInterval(player);document.getElementById("result").innerHTML = new Date().getTime() - timestamp'>stop</button>
                </div>
                <input class="form-control" type="number" id="delay" style="width:80px;">ms</input>
                <label id='result'></label>
            </form>
        </center>
    </body>
</html>

答案 1 :(得分:0)

添加justify-content: center;即可。

有关flexbox的更多信息:Flexbox