css 如何使用d-flex在项目之间留出空间

vs91vp4v  于 2023-02-06  发布在  其他
关注(0)|答案(4)|浏览(180)

我仍然不知道我应该在哪里用网格系统和列来分隔我的项目,以及何时使用d-flex?假设我有4个按钮:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex justify-content-center">
  <button class="btn">First</button>
  <button class="btn">second</button>
  <button class="btn">Third</button>
  <button class="btn">fourth</button>
  <button class="btn">Fifth</button>
  <button class="btn">six</button>
</div>

我有项目在行和中心,但没有填补了他们之间的空间,我该怎么做呢?我应该使用列和行或伸缩?

j8ag8udp

j8ag8udp1#

在Bootstrap 5中,有一个使用间隙的选项。使用间隙可以在Flex项目之间提供空间。我也在为同样的问题而挣扎,我不想使用边距或填充。所以间隙对我来说是最好的解决方案。

.d-flex{
  gap:20px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex justify-content-center">
  <button class="btn">First</button>
  <button class="btn">second</button>
  <button class="btn">Third</button>
  <button class="btn">fourth</button>
  <button class="btn">Fifth</button>
  <button class="btn">six</button>
</div>
wko9yo5t

wko9yo5t2#

不使用justify-content: center,而是查看它的其他值:
一米一米一,一米二米一,一米三米一。
它们提供了元素之间的间距,应该会有所帮助。

yzuktlbb

yzuktlbb3#

.d-flex {
width: 100%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="d-flex justify-content-between" >
<button class="btn">First</button>
<button class="btn">second</button>
<button class="btn">Third</button>
<button class="btn">fourth</button>
<button class="btn">Fifth</button>
 <button class="btn">six</button>
</div>

<br>
OR
<br>

<div class="d-flex justify-content-around" >
<button class="btn">First</button>
<button class="btn">second</button>
<button class="btn">Third</button>
<button class="btn">fourth</button>
<button class="btn">Fifth</button>
 <button class="btn">six</button>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

如果您使用的是 Bootstrap ,则需要添加类。

nnt7mjpx

nnt7mjpx4#

也许您需要迁移到bootstap 5并使用差距间距实用程序。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="d-flex justify-content-center gap-3">
  <button class="btn btn-outline-secondary">First</button>
  <button class="btn btn-outline-secondary">second</button>
  <button class="btn btn-outline-secondary">Third</button>
  <button class="btn btn-outline-secondary">fourth</button>
  <button class="btn btn-outline-secondary">Fifth</button>
  <button class="btn btn-outline-secondary">six</button>
</div>

请看:https://getbootstrap.com/docs/5.3/utilities/spacing/#gap
另请参见:https://getbootstrap.com/docs/5.0/migration/

相关问题