def margin_box_content_layout(context, page, box):
    """Layout a margin box’s content once the box has dimensions."""
    positioned_boxes = []
    box, resume_at, next_page, _, _, _ = block_container_layout(
        context, box, bottom_space=-inf, skip_stack=None, page_is_empty=True,
        absolute_boxes=positioned_boxes, fixed_boxes=positioned_boxes,
        adjoining_margins=None, discard=False, max_lines=None)
    assert resume_at is None
    for absolute_box in positioned_boxes:
        absolute_layout(
            context, absolute_box, box, positioned_boxes, bottom_space=0,
            skip_stack=None)

    vertical_align = box.style['vertical_align']
    # Every other value is read as 'top', ie. no change.
    if vertical_align in ('middle', 'bottom') and box.children:
        first_child = box.children[0]
        last_child = box.children[-1]
        top = first_child.position_y
        # Not always exact because floating point errors
        # assert top == box.content_box_y()
        bottom = last_child.position_y + last_child.margin_height()
        content_height = bottom - top
        offset = box.height - content_height
        if vertical_align == 'middle':
            offset /= 2
        for child in box.children:
            child.translate(0, offset)
    return box
Layout a margin box’s content once the box has dimensions.
weasyprint/layout/page.py