Flex Gap Polyfill

Below are a list of test cases demonstrating scenarios this polyfill works with. Github

Polyfill: on Red lines show container with negative margins.

Basic

.container {
	display: flex;
	flex-wrap: wrap;
	gap: 20px;
}
	
One
Two
Three
Four
Five

Nested

.container {
	display: flex;
	flex-wrap: wrap;
	gap: 20px;
}

.container .container {
	display: flex;
	flex-wrap: wrap;
	gap: 40px;
}
	
A
B
C
D
A
B
C
D

Percentages

(Percentage row gaps only appear when height is specified, disabled in polyfill for now).

.container
	display: flex;
	flex-wrap: wrap;
	gap: 6%;
}
	
A
B
C
D

Ems

.container
	display: flex;
	flex-wrap: wrap;
	gap: 2em;
}
	
A
B
C
D

Nested Combination (% > px, px > %, em > px, etc)

.container
	display: flex;
	flex-wrap: wrap;
	gap: 6%;
}

.container .container {
	display: flex;
	flex-wrap: wrap;
	gap: 20px;
}
	
A
B
C
D
A
B
C
D

Percentage gaps aren't that reliable if the width of the container is not 100% of it's parent.

.container
	display: flex;
	flex-wrap: wrap;
	gap: 6%;
}

.container .container {
	display: flex;
	flex-wrap: wrap;
	gap: 3%;
}
	
A
B
C
D
A
B
C
D

Margin + Gap

.container
	display: flex;
	flex-wrap: wrap;
	margin: 20px;
	gap: 3%;
}
	
A
B
C
D

Justify End

.container
	display: flex;
	flex-wrap: wrap;
	justify-content: flex-end;
	gap: 3%;
}
	
A
B
C
D

Space Evenly

.container
	display: flex;
	flex-wrap: wrap;
	gap: 3%;
	justify-content: space-evenly;
}
	
A
B
C
D

Grid

.container {
	display: flex;
	flex-wrap: wrap;
	flex-grow: 1;
	--gap: 20px;
	gap: var(--gap);
	max-width: 400px;
}

.item {
	flex: calc(50% - var(--gap));
}
	
A
B
C
D

Columns and Rows Independently

.container {
	display: flex;
	flex-wrap: wrap;
	row-gap: 40px;
}

.container .container {
	display: flex;
	flex-wrap: wrap;
	column-gap: 20px;
}
	
A
B
C
D
A
B
C
D

When container doesn't have flex

.container {
	display: grid;
	gap: 40px;
	margin-left: 40px;
}
	
A
B
C
D