为什么我的容器没有进入中心

nit*_*rma 0 javascript css jquery

我想在屏幕中央打开一个div(水平和垂直).

var documnetWidth = $(document).width(),
  documentHeight = $(document).height(),
  widgetFormHeight = widgetForm.height(),
  widgetFormWidth = widgetForm.width();

widgetForm.css({
  top: documentHeight / 2 - widgetFormHeight / 2,
  left: documnetWidth / 2 - widgetFormWidth / 2
});
Run Code Online (Sandbox Code Playgroud)

我的小部件是水平中心,但垂直方向需要一些偏移.

rah*_*hul 6

你可以这样做

定义DIV的大小和位置固定,如下所示:

div {
     position: fixed;
     top: 50%; 
     left: 50%; 
     width: 200px; 
     height: 100px; 
     margin: -100px 0 0 -50px;
     z-index: 99;
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想将其放置在绝对位置,您可以给它一个宽度,并将其设置为:

div { margin: 0 auto; }
Run Code Online (Sandbox Code Playgroud)