             let relevantAfterWidth = afterWithMargins?.width.add(spacing) ?? 0
             let availableWidth = max(size.width - relevantBeforeWidth - relevantAfterWidth, 0)
             measures.content = elements.content.sizeThatFitsWithMargins(CGSize(width: availableWidth, height: size.height))
+            let contentWithMargins = measures.content!.addMargins(ofElement: elements.content)
             return CGSize(
-                width: relevantBeforeWidth + relevantAfterWidth + measures.content!.width,
-                height: max(beforeWithMargins?.height ?? 0, afterWithMargins?.height ?? 0, measures.content!.height))
+                width: relevantBeforeWidth + relevantAfterWidth + contentWithMargins.width,
+                height: max(beforeWithMargins?.height ?? 0, afterWithMargins?.height ?? 0, contentWithMargins.height))
 
         case .vertical:
             let relevantBeforeHeight = beforeWithMargins?.height.add(spacing) ?? 0
             let relevantAfterHeight = afterWithMargins?.height.add(spacing) ?? 0
             let availableHeight = max(size.width - relevantBeforeHeight - relevantAfterHeight, 0)
             measures.content = elements.content.sizeThatFitsWithMargins(CGSize(width: size.width, height: availableHeight))
+            let contentWithMargins = measures.content!.addMargins(ofElement: elements.content)
             return CGSize(
-                width: max(beforeWithMargins?.width ?? 0, afterWithMargins?.width ?? 0, measures.content!.width),
-                height: relevantBeforeHeight + relevantAfterHeight + measures.content!.height)
+                width: max(beforeWithMargins?.width ?? 0, afterWithMargins?.width ?? 0, contentWithMargins.width),
+                height: relevantBeforeHeight + relevantAfterHeight + contentWithMargins.height)
         }
     }
 }

        if measures.after == nil {
            measures.after = elements.after?.sizeThatFitsWithMargins(frame.size)
        }
        if measures.content == nil {
            measures.content = elements.content.sizeThatFitsWithMargins(frame.size)
        }

        switch orientation {

                    x: frame.minX + elements.before!.layoutMargins.left,
                    y: frame.minY + elements.before!.layoutMargins.top,
                    width: measures.before!.width,
                    height: measures.before!.height))
            }

            if elements.after != nil {
                elements.after!.layoutWith(frame: CGRect(
                    x: frame.maxX - measures.after!.width - elements.after!.layoutMargins.right,
                    y: frame.minY + elements.after!.layoutMargins.top,
                    width: measures.after!.width,
                    height: measures.after!.height))
            }

            elements.content.layoutWith(frame: CGRect(
                x: frame.minX
                    + relevantBeforeWidth
                    + elements.content.layoutMargins.left,
                y: frame.minY + elements.content.layoutMargins.top,
                width: measures.content!.width,
                height: measures.content!.height))
        case .vertical:
            if elements.before != nil {
                elements.before!.layoutWith(frame: CGRect(
                    x: frame.minX + elements.before!.layoutMargins.left,
                    y: frame.minY + elements.before!.layoutMargins.top,
                    width:  measures.before!.width,
                    height: measures.before!.height))
            }

            if elements.after != nil {
                elements.after!.layoutWith(frame: CGRect(
                    x: frame.minX + elements.after!.layoutMargins.left,
                    y: frame.maxY - measures.after!.height - elements.after!.layoutMargins.bottom,
                    width: measures.before!.width,
                    height: measures.after!.height))
            }

            elements.content.layoutWith(frame: CGRect(
                x: frame.minX + elements.content.layoutMargins.left,
                y: frame.minY
                    + relevantBeforeHeight
                    + elements.content.layoutMargins.top,
                width: measures.content!.width,
                height: measures.content!.height))
        }
    }

