首頁 > web前端 > css教學 > 主體

如何在 Flex 項目(包括第一個項目和最後一個項目)之間實現相等間距?

Barbara Streisand
發布: 2024-11-15 08:53:03
原創
847 人瀏覽過

How Can I Achieve Equal Spacing Between Flex Items, Including the First and Last?

Equal Spacing Between Flex Items

Problem

Achieving equal spacing around flexbox children poses a challenge, as highlighted in a previous article where justify-content: space-around is presented as an imperfect solution. This article delves into more comprehensive methods to ensure equal spacing, including the first and last items.

Solution

Pseudo-Elements

Recent browser advancements allow pseudo-elements (::before and ::after) to be treated as flex items within a flex container. This opens up new possibilities for creating equal spacing.

By adding ::before and ::after pseudo-elements to the container and utilizing justify-content: space-between, along with zero-width pseudo-elements, we can create the illusion of equal spacing between visible flex items.

flex-container {
  display: flex;
  justify-content: space-between;
}

flex-container::before {
  content: "";
}

flex-container::after {
  content: "";
}
登入後複製

以上是如何在 Flex 項目(包括第一個項目和最後一個項目)之間實現相等間距?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板