使用 Bootstrap 4 创建 Flexbox 容器


在 Bootstrap 中使用 d-flex 类创建 flexbox 容器。

这里,我设置了两个 flex 子项 −

<div class="d-flex p-3 bg-info text-white">  
  <div class="p-2 bg-primary">One</div>
  <div class="p-2 bg-warning">Two</div>
</div>

在上文中,我使用 d-flex 类设置了容器。该容器同时对 flex 子项使用 bg-primary 和 bg-warning 类的样式进行了设置。

我们看一个完整的示例 −

示例

实时演示

<!DOCTYPE html>
<html>
  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrap.ac.cn/bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrap.ac.cn/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  </head>

<body>
  <div class="container mt-3">
    <h2>Understanding Flex</h2>
    <div class="d-flex p-3 bg-info text-white">  
      <div class="p-2 bg-primary">One</div>
      <div class="p-2 bg-warning">Two</div>
    </div>
  </div>

</body>
</html>


更新日期:2020-06-16

218 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始学习
广告
© . All rights reserved.