代码之家  ›  专栏  ›  技术社区  ›  Michiel Borkent

如何使bootstrap 4 flex表不超过页面宽度的100%?

  •  0
  • Michiel Borkent  · 技术社区  · 7 年前

    在下面的示例中,我想将div格式化为一个表,但我不想将文本分布在页面宽度的100%上。我希望这些列彼此尽可能靠近,以便它能像普通文本一样阅读。对于下面的示例,我希望将其显示为:

    aaa   bb   ccc ddd 
    aaaaa bbbb c   dd
    

    当我尝试使用Bootstrap 4时 .row .col 我得到了我想要的表格格式,但是表格将文本扩展到页面宽度的100%。如何获得上述格式?

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
    <div style="font-family: Consolas;">
       <div class="d-flex p-2">
          <div>aaa&nbsp;</div>
          <div>bb&nbsp;</div>
          <div>ccc&nbsp;</div>
          <div>ddd&nbsp;</div>
       </div>
       <div class="d-flex p-2">
          <div>aaaaa&nbsp;</div>
          <div>bbbb&nbsp;</div>
          <div>c&nbsp;</div>
          <div>dd&nbsp;</div>
       </div>
    </div>

    编辑:我意识到使用一个普通的 table 没有CSS的引导可能会做我想要的。

    1 回复  |  直到 7 年前
        1
  •  2
  •   tao    7 年前

    如果你想要一个 <table> ,这就是你应该使用的。
    仅引导样式 <表> table ,以允许使用 <表> s出于任何其他目的:

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    <table>
      <tbody>
        <tr>
          <td>aaa&nbsp;</td>
          <td>bb&nbsp;</td>
          <td>ccc&nbsp;</td>
          <td>ddd&nbsp;</td>
        </tr>
        <tr>
          <td>aaaaa&nbsp;</td>
          <td>bbbb&nbsp;</td>
          <td>c&nbsp;</td>
          <td>dd&nbsp;</td>
        </tr>
      </tbody>
    </table>

    <表> 特定样式 摆桌子 不伸展 您可以为表指定其父级的整个宽度 width: auto :

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    <table class="table" style="width: auto">
      <tbody>
        <tr>
          <td>aaa&nbsp;</td>
          <td>bb&nbsp;</td>
          <td>ccc&nbsp;</td>
          <td>ddd&nbsp;</td>
        </tr>
        <tr>
          <td>aaaaa&nbsp;</td>
          <td>bbbb&nbsp;</td>
          <td>c&nbsp;</td>
          <td>dd&nbsp;</td>
        </tr>
      </tbody>
    </table>

    您还可以依靠Bootstrap的网格系统来限制网格的宽度 <表> 或者它的父母。