Your code will work if you add units to the width and height:
newElm.style.width = Math.floor((Math.random()*100)+2) + "px";
newElm.style.height = Math.floor((Math.random()*100)+2) + "px";
The reason this is needed is a css size without a unit is discarded, so the divs are empty and thus you can only see their borders stacked as a thin black bar at the top of the page.
EDIT: the reason it works if you add something before the doctype is that then the browser goes into quirks mode (as opposed to standards-compliant mode with the doctype) where it is a lot more accepting of non-standard things like broken css units.