Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Q758】如何实现父容器中子元素具有相同间距的分布 #815

Open
shfshanyue opened this issue Dec 20, 2024 · 1 comment
Open
Labels

Comments

@shfshanyue
Copy link
Owner

shfshanyue commented Dec 20, 2024

布局如下图所示,使每一项间距 16px:

<div class="container">
  <div class="item">01</div>
  <div class="item">02</div>
  <div class="item">03</div>
</div>
@shfshanyue shfshanyue added the css label Dec 20, 2024
@shfshanyue
Copy link
Owner Author

代码见 codepen

对于实现该布局,有多种方案,最简单的即 flex/gap:

.container {
  display: flex;
  gap: 16px;
}

如果不借助 flex 的话,使用 * + * 猫头鹰选择器也是一个很好的方案:

.container > * + * {
  margin-left: 16px
}

如果对 * + * 以及 flex 均不熟悉的话,也可以使用 :not(:first-child) 来实现

.item:not(:first-child) {
  margin-left: 16px
}

如果在 tailwindcss 中,可以使用以下写法:

<div class="container space-x-4" > </div>

<div class="container gap-4" > </div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant