Cross browser div center alignment using CSS

前端 未结 4 1059
醉话见心
醉话见心 2020-12-08 03:22

What is the easiest way to align a div whose position is relative horizontally and vertically using CSS ? The width and the height of the div

4条回答
  •  误落风尘
    2020-12-08 03:43

    "If the width and height are really unknown beforehand, then you'll need to grab Javascript/jQuery to set the margin-left and margin-top values and live with the fact that client will see the div quickly be shifted during page load, which might cause a "wtf?" experience."

    You could .hide() the div when the DOM is ready, wait for the page to load, set the div margin-left and margin-top values, and .show() the div again.

    $(function(){
       $("#content").hide();
    )};
    $(window).bind("load", function() {
       $("#content").getDimSetMargins();
       $("#content").show();
    });
    

提交回复
热议问题