diff --git a/tests/layout/test_grid.py b/tests/layout/test_grid.py
index 905f32348..20e354621 100644
--- a/tests/layout/test_grid.py
+++ b/tests/layout/test_grid.py
@@ -280,6 +280,40 @@ def test_grid_template_areas_overlap():
assert article.width == 10
+@assert_no_logs
+def test_grid_template_areas_span_overflow():
+ page, = render_pages('''
+
+
+ a
+ a
+ a
+
+ ''')
+ html, = page.children
+ body, = html.children
+ article, = body.children
+ div_a1, div_a2, div_a3 = article.children
+ assert div_a1.position_x == div_a2.position_x == div_a3.position_x == 0
+ assert div_a1.position_y == 0
+ assert div_a2.position_y == 2
+ assert div_a3.position_y == 4
+ assert div_a1.width == div_a3.width == 5
+ assert div_a2.width == 10
+ assert div_a1.height == div_a2.height == div_a3.height == 2
+ assert article.width == 10
+
+
@assert_no_logs
def test_grid_template_areas_extra_span():
page, = render_pages('''
diff --git a/weasyprint/layout/grid.py b/weasyprint/layout/grid.py
index 4a1f9efd7..c052573b1 100644
--- a/weasyprint/layout/grid.py
+++ b/weasyprint/layout/grid.py
@@ -842,8 +842,9 @@ def grid_layout(context, box, bottom_space, skip_stack, containing_block,
width, height = first_size, second_size
intersect = _intersect_with_children(
x, y, width, height, children_positions.values())
- if intersect:
- # Child intersects with a positioned child.
+ overflow = second_i + second_size > implicit_second_2
+ if intersect or overflow:
+ # Child intersects with a positioned child or overflows.
continue
else:
# Free place found.
@@ -947,8 +948,9 @@ def grid_layout(context, box, bottom_space, skip_stack, containing_block,
width, height = first_size, second_size
intersect = _intersect_with_children(
x, y, width, height, children_positions.values())
- if intersect:
- # Child intersects with a positioned child.
+ overflow = second_i + second_size > implicit_second_2
+ if intersect or overflow:
+ # Child intersects with a positioned child or overflows.
continue
else:
# Free place found.