引导显示列彼此相邻

时间:2018-07-03 02:45:35

标签: css bootstrap-4 multiple-columns

我正在使用Bootstrap 4和php编写示例应用程序。我试图让两列在某个断点处彼此相邻显示。这是一个例子。我该怎么解决?

<html>
<head>
    <!--bootstrap 4.0-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-3" style="margin: 20px 0;">
                <h4>Search</h4>
            </div>
            <div class="col-lg-3" style="margin: 20px 0;">
                 <h4>Displaying ### results</h4>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-3 col-sm-12">
                <p>Search field</p>
                <p>Search field</p>
                <p>Search field</p>
                <p>Search field</p>
            </div>
            <!-- at col-sm-6 I want two columns to display. how can i solve this? -->
            <div class="col-lg-9 col-sm-6">
            <?php for($i=0; $i<10; $i++) 
            {
            ?>
                <div>
                    Record
                    <hr/>
                </div>
            <?php
            }
            ?>
            </div>
        </div>
    </div>
    <!--bootstrap 4 javascript-->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

关于Bootstrap要记住的最重要的事情是,每行必须的列数必须总计为12。与其将col-sm-6放在每个记录容器上,不如说是要做的是在其上放 row ,然后将col-6添加到PHP循环内输出的每个<div>记录元素中。

这可以在下面看到:

<html>

<head>
  <!--bootstrap 4.0-->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-lg-3" style="margin: 20px 0;">
        <h4>Search</h4>
      </div>
      <div class="col-lg-3" style="margin: 20px 0;">
        <h4>Displaying ### results</h4>
      </div>
    </div>
    <div class="row">
      <div class="col-lg-3 col-sm-12">
        <p>Search field</p>
        <p>Search field</p>
        <p>Search field</p>
        <p>Search field</p>
      </div>
      <!-- at col-sm-6 I want two columns to display. how can i solve this? -->
      <div class="col-lg-9 row">
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
        <div class="col-6">
          Record
          <hr/>
        </div>
      </div>
    </div>
  </div>
  <!--bootstrap 4 javascript-->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>

</html>

此外,请注意,col-6将适用于Bootstrap 4中的所有 宽度,而col-sm-6仅适用于更大大于575px的宽度:

Grid System

答案 1 :(得分:0)

看来您的列数不匹配。

我怀疑这点...

<div class="col-lg-3 col-sm-12">

应该是...

<div class="col-lg-3 col-sm-6">

答案 2 :(得分:0)

您想要这个吗?

<html>
<head>
    <!--bootstrap 4.0-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-3" style="margin: 20px 0;">
                <h4>Search</h4>
            </div>
            <div class="col-lg-3" style="margin: 20px 0;">
                 <h4>Displaying ### results</h4>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-3 col-sm-9">
                <p>Search field</p>
                <p>Search field</p>
                <p>Search field</p>
                <p>Search field</p>
            </div>
            <!-- at col-sm-6 I want two columns to display. how can i solve this? -->
            <div class="col-lg-9 col-sm-3">
            <?php for($i=0; $i<10; $i++) 
            {
            ?>
                <div>
                    Record
                    <hr/>
                </div>
            <?php
            }
            ?>
            </div>
        </div>
    </div>
    <!--bootstrap 4 javascript-->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>
</html>

https://jsfiddle.net/48n2hvyd/3/

答案 3 :(得分:0)

感谢您的帮助。阅读您的答案后,我便解决了。这就是我需要的:

<html>
<head>
  <!--bootstrap 4.0-->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
  <div class="container">
    <div class="row">
      <div class="col-lg-6" style="margin: 20px 0;">
        <h4>Search</h4>
      </div>
      <div class="col-lg-6" style="margin: 20px 0;">
        <h4>Displaying ### results</h4>
      </div>
    </div>
    <div class="row">
      <div class="col-lg-3">
        <p>Search field</p>
        <p>Search field</p>
        <p>Search field</p>
        <p>Search field</p>
      </div>
      <div class="col-lg-9">
        <div class="row">
             <?php for($i=0; $i<10; $i++) 
                {
                ?>
                    <div class="col-lg-12 col-sm-6">
                        Record
                        <hr/>
                    </div>
                <?php
                }
             ?>
         </div>
      </div>
    </div>
  </div>
  <!--bootstrap 4 javascript-->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>
</html>