网格系统无法在移动设备或小屏幕上正确调整大小

时间:2017-10-04 11:55:28

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

命令col-xs-whatever不起作用,并在移动视图或小屏幕上创建断行。

注意:我还测试了类.col-作为Bootstrap 4在文档中指定但它仍然不起作用。

它没有正确调整大小并堆积,这是相关代码:

.navbar{
    background-image: cornflowerblue;
}
#mainNav{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
body{
    background-color: grey;
}
#navContent{
    background-color: white;
    margin: 3%;
}
#final{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
#searchFormContent{
    margin: auto;
    width: 80%;
}
#navToggler{
    float: right;
}
.navbar-toggler{
    float:right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<body>
    <div class="container-fluid">
        <section class="row" id="mainNav">
            <div class="col-12">
                <nav class="navbar sticky-top navbar-expand-md">
                    <div class="col-xs-1 col-md-2">
                        <a class="navbar-brand" href="#">Brand</a>
                    </div>

                    <div class="col-xs-10 col-md-7">
                        <form class="form-inline" id="searchForm" role="search">
                            <div class="input-group" id="searchFormContent">
                                <input class="form-control" type="text" placeholder="Busqueda">
                                <div class="input-group-btn">
                                    <button class="btn btn-secondary" type="button"><i class="fa fa-search"></i></button>
                                </div>
                            </div>
                        </form>
                        <span id="destiny1"></span>
                    </div>
                    <div class="col-xs-1 col-md-3">
                        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
                            <i class="fa fa-bars"></i>
                        </button>
                        <div class="collapse navbar-collapse" id="navToggler">
                            <ul class="navbar-nav">
                                <li class="nav-item"><a class="nav-link" href="#">Option 1</a></li>
                                <li class="nav-item"><a class="nav-link" href="#">Option 2</a></li>
                            </ul>
                        </div>
                        <span id="destiny2"></span>
                    </div>
                </nav>
            </div>
            </section>
            </div>
            </body>

2 个答案:

答案 0 :(得分:4)

我不知道这是否是你想要的结果,因为你没有指明。

我已将col-xs-whatever更改为col-whatever,之后您将row上的边距和边框应用于#mainNav,这就是为什么所有列都无法放入内部的原因。

因此,我将row移到了包装导航栏中,并从row

中删除了#mainNav

以下是更改的代码:

.navbar{
    background-image: cornflowerblue;
}
#mainNav{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
body{
    background-color: grey;
}
#navContent{
    background-color: white;
    margin: 3%;
}
#final{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
#searchFormContent{
    margin: auto;
    width: 80%;
}
#navToggler{
    float: right;
}
.navbar-toggler{
    float:right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<div class="container-fluid">
        <section id="mainNav">    
            <nav class="navbar sticky-top navbar-expand-md row">
                <div class="col-2 col-md-2">
                    <a class="navbar-brand" href="#">Brand</a>
                </div>    
                <div class="col-8 col-md-7">
                    <form class="form-inline" id="searchForm" role="search">
                        <div class="input-group" id="searchFormContent">
                            <input class="form-control" type="text" placeholder="Busqueda">
                            <div class="input-group-btn">
                                <button class="btn btn-secondary" type="button"><i class="fa fa-search"></i></button>
                            </div>
                        </div>
                    </form>
                    <span id="destiny1"></span>
                </div>
                <div class="col-2 col-md-3">
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
                                <i class="fa fa-bars"></i>
                            </button>
                    <div class="collapse navbar-collapse" id="navToggler">
                        <ul class="navbar-nav">
                            <li class="nav-item"><a class="nav-link" href="#">Option 1</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Option 2</a></li>
                        </ul>
                    </div>
                    <span id="destiny2"></span>
                </div>
            </nav>    
        </section>
    </div>

答案 1 :(得分:0)

根据iamwtk的建议,问题的一部分是你必须使用col-XYZ代替col-xs-XYZ,但最大的问题是网格的内容:Brand和Busqueda都超过了col,打破行...尝试播放一点col分配并改变&#34; searchFormContent&#34;的宽度。让它变得动态;)