void dbugAddToProperContainer(CssBox box) { var rectChild = new RectangleF(box.LocalX, box.LocalY, box.InnerContentWidth, box.InnerContentHeight); CssBox parent = box.ParentBox; bool found = false; while (parent != null) { var rectParent = new RectangleF(0, 0, parent.VisualWidth, parent.VisualHeight); if (rectParent.Contains(rectChild)) { found = true; //add to here float bfx, bfy; box.GetGlobalLocation(out bfx, out bfy); float rfx, rfy; parent.GetGlobalLocation(out rfx, out rfy); //diff float nx = bfx - rfx; float ny = bfy - rfy; box.SetLocation(nx, ny); parent.AppendToAbsoluteLayer(box); break; } else { rectChild.Offset(parent.LocalX, parent.LocalY); parent = parent.ParentBox; } } if (!found) { //add to root top float bfx, bfy; box.GetGlobalLocation(out bfx, out bfy); float rfx, rfy; this._rootBox.GetGlobalLocation(out rfx, out rfy); //diff float nx = bfx - rfx; float ny = bfy - rfy; box.SetLocation(nx, ny); this._rootBox.AppendToAbsoluteLayer(box); } }