我正在尝试使用bootstrap网格系统制作一个表.我知道我们每行应该只使用12列.但我希望有超过12列,整个表格的水平滚动.所以我尝试使用以下代码片段
<span class="col-md-2" style="text-align: center;"><b>Field 1</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 2</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 3</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 4</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 5</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 6</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 7</b></span> <span class="col-md-2" style="text-align: center;"><b>Field 8</b></span>
我想在一行中显示如上所述的8个字段.但是在第6场之后,其他两个领域正在下降.有没有办法让所有8个字段在单行中与水平滚动.
解决方法
Bootstrap网格的四个技巧
1)8列
您可以使用嵌套网格.没有任何表或自定义.例如:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <div class="container-fluid"> <div class="row"> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 1</b></div> <div class="col-md-6"><b>Field 2</b></div> </div> </div> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 3</b></div> <div class="col-md-6"><b>Field 4</b></div> </div> </div> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 5</b></div> <div class="col-md-6"><b>Field 6</b></div> </div> </div> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 7</b></div> <div class="col-md-6"><b>Field 8</b></div> </div> </div> </div> </div>
2)滚动
@media (min-width: 992px) { .container-scroll { overflow-x: auto; } .container-scroll > .row { width: 133.33333333%; /* = 100% * 4/3 */ } }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <div class="container-fluid container-scroll"> <div class="row"> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 1</b></div> <div class="col-md-6"><b>Field 2</b></div> </div> </div> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 3</b></div> <div class="col-md-6"><b>Field 4</b></div> </div> </div> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 5</b></div> <div class="col-md-6"><b>Field 6</b></div> </div> </div> <div class="col-md-3"> <div class="row text-center"> <div class="col-md-6"><b>Field 7</b></div> <div class="col-md-6"><b>Field 8</b></div> </div> </div> </div> </div>
3)各种数量的列
如果每行具有不同数量的列,但预先知道列数.
在这种情况下,行的宽度可以不同.因此,有必要设置相对于屏幕宽度的列宽,而不是行的宽度.
>使用vw而不是%
>如果行宽超过100vw,则为行设置特殊宽度
@media (min-width: 992px) { .container-scroll { overflow-x: auto; } .container-scroll .columns-16 { width: 133.33333333vw; /* = 100vw * 16/12 */ } .container-scroll .columns-24 { width: 200vw; /* = 100vw * 24/12 */ } .container-scroll .col-md-2 { width: 16.66666667vw !important; } } .container-scroll > .row { margin-top: 24px; } .container-scroll > .row > .col-md-2 { font-weight: bold; text-align: center; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <div class="container-fluid container-scroll"> <div class="row columns-16"> <div class="col-md-2">Field 1</div> <div class="col-md-2">Field 2</div> <div class="col-md-2">Field 3</div> <div class="col-md-2">Field 4</div> <div class="col-md-2">Field 5</div> <div class="col-md-2">Field 6</div> <div class="col-md-2">Field 7</div> <div class="col-md-2">Field 8</div> </div> </div> <div class="container-fluid container-scroll"> <div class="row columns-24"> <div class="col-md-2">Field 1</div> <div class="col-md-2">Field 2</div> <div class="col-md-2">Field 3</div> <div class="col-md-2">Field 4</div> <div class="col-md-2">Field 5</div> <div class="col-md-2">Field 6</div> <div class="col-md-2">Field 7</div> <div class="col-md-2">Field 8</div> <div class="col-md-2">Field 9</div> <div class="col-md-2">Field 10</div> <div class="col-md-2">Field 11</div> <div class="col-md-2">Field 12</div> </div> </div> <div class="container-fluid container-scroll"> <div class="row"> <div class="col-md-2">Field 1</div> <div class="col-md-2">Field 2</div> <div class="col-md-2">Field 3</div> <div class="col-md-2">Field 4</div> </div> </div>
4)列数未知
如果您不知道行中的列数,请将列转换为内联块.
@media (min-width: 992px) { .container-scroll > .row { overflow-x: auto; white-space: nowrap; } .container-scroll > .row > .col-md-2 { display: inline-block; float: none; } } .container-scroll > .row { margin-top: 24px; } .container-scroll > .row > .col-md-2 { font-weight: bold; text-align: center; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <div class="container-fluid container-scroll"> <div class="row"> <div class="col-md-2">Field 1</div> <div class="col-md-2">Field 2</div> <div class="col-md-2">Field 3</div> <div class="col-md-2">Field 4</div> <div class="col-md-2">Field 5</div> <div class="col-md-2">Field 6</div> <div class="col-md-2">Field 7</div> <div class="col-md-2">Field 8</div> </div> <div class="row"> <div class="col-md-2">Field 1</div> <div class="col-md-2">Field 2</div> <div class="col-md-2">Field 3</div> <div class="col-md-2">Field 4</div> <div class="col-md-2">Field 5</div> <div class="col-md-2">Field 6</div> <div class="col-md-2">Field 7</div> <div class="col-md-2">Field 8</div> <div class="col-md-2">Field 9</div> <div class="col-md-2">Field 10</div> <div class="col-md-2">Field 11</div> <div class="col-md-2">Field 12</div> </div> <div class="row"> <div class="col-md-2">Field 1</div> <div class="col-md-2">Field 2</div> <div class="col-md-2">Field 3</div> <div class="col-md-2">Field 4</div> </div> </div>