Short Answer
In CSS, there is currently no 100% reliable way to prevent pseudo-elements from impacting the justify-content: space-between
calculation.
Explanation
::before
and ::after
pseudo elements on a flex container become flex items.
From the spec:
4. Flex Items
Each in-flow child of a flex container becomes a flex item.
In other words, each child of a flex container that is in the normal flow (i.e., not absolutely positioned), is considered a flex item.
Most, if not all, browsers interpret this to include pseudo-elements. The ::before
pseudo is the first flex item. The ::after
item is the last.
Here is further confirmation of this rendering behavior from Firefox documentation:
In-flow ::after
and ::before
pseudo-elements are now flex
items
(bug 867454).
One possible solution to your problem is to remove the pseudo-elements from the normal flow with absolute positioning. However, this method may not work in all browsers:
See my answer here for illustrations of pseudo elements messing up justify-content
:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…