简体   繁体   English

如何将div放在屏幕中央?

[英]How to position div in the center of the screen?

Actually in the center of current view, above all other objects. 实际上在当前视图的中心,首先是所有其他对象。 Cross-browser and without 3rd party plugins and etc. 跨浏览器,没有第三方插件等

Via CSS or Javascript 通过CSS或Javascript

UPDATE: 更新:

I tried to use Javascript's Sys.UI.DomElement.setLocation() but this positioning object not in absolute location but relative to it's parent. 我试图使用Javascript的Sys.UI.DomElement.setLocation(),但这个定位对象不在绝对位置,而是相对于它的父级。
Is there a function to put in absolute location? 是否存在放置绝对位置的功能?

To have it center in the browser, regardless of the page scroll (which is probably what you want), position: fixed will be more reliable. 要让它在浏览器中居中,无论页面滚动(可能是你想要的),position:fixed都会更可靠。 Amending mike108's code: 修改mike108的代码:

<style type="text/css"> 
.centered {  
  position:fixed;
  z-index: 100;  
  top:50%;  
  left:50%;  
  margin:-100px 0 0 -100px;  
  width:200px;  
  height:200px;  
}  
</style>

This assumes you have a fixed size box you are showing. 这假设您有一个固定大小的盒子。 If you don't have a fixed size box, you'll need to use Javascript to measure the window and the div and position it explicitly (again, fixed is probably the way to go). 如果你没有固定大小的框,你需要使用Javascript来测量窗口和div并明确定位它(再次,修复可能是要走的路)。

Do test on all your browsers, though. 不过,请在所有浏览器上进行测试。 There may be something we're not thinking of. 可能有一些我们没有想到的东西。

I'd actually recommend you look at one of the Javascript plugins that does this, at least for a starting point. 我实际上建议你看一下这样做的Javascript插件,至少是一个起点。 They have figured it out, even if you don't want to use their code. 他们已经弄明白了,即使你不想使用他们的代码。 I think I used jqModal as a starting point. 我想我用jqModal作为起点。

<style type="text/css"> 
.Div1 {  
position:absolute;  
top:50%;  
left:50%;  
margin:-100px 0 0 -100px;  
width:200px;  
height:200px;  
border:1px solid #9999FF;  
}  
</style> 


<div class="Div1">
</div>

It must have a width assigned ( ie width:500px;) and then margin:auto; 它必须具有指定的宽度(即宽度:500px;),然后是margin:auto; should do it. 应该这样做。

I'm using this block of js code; 我正在使用这块js代码;

// call function for both height and width center //高度和宽度中心的调用功能

    setToCenterOfParent( $('#myDiv'), document.body, false, false);

// call function for both height center only //仅适用于两个身高中心的呼叫功能

    setToCenterOfParent( $('#myDiv'), document.body, false, true);

// call function for both width center only //仅为宽度中心调用函数

    setToCenterOfParent( $('#myDiv'), document.body, true, false);

//function definition //函数定义

    function setToCenterOfParent(element, parent, ignoreWidth, ignoreHeight){
        parentWidth = $(parent).width();
        parentHeight = $(parent).height();  
        elementWidth = $(element).width();
        elementHeight = $(element).height();
        if(!ignoreWidth)
            $(element).css('left', parentWidth/2 - elementWidth/2);
        if(!ignoreHeight)
            $(element).css('top', parentHeight/2 - elementHeight/2);
    }

Please check this solution. 请检查此解决方案。 There is no need to define a width or a height of the div box. 无需定义div框的宽度或高度。 It will be always in the middle/centre of the screen. 它将始终位于屏幕的中间/中心。 I hope my solution will be usefull for everyone. 我希望我的解决方案对每个人都有用。

<style type="text/css"> 

  .mask{
    position:fixed;
    z-index:100;
    width:100%;
    height:100%;
    text-align: center;
    color: white;
    background-color: rgba(0,0,0,0.75);
    overflow:hidden;
  }

  .contener{
    height:100%;
    display:inline-table;
  }

  .contener .content{
    vertical-align: middle;
    display:table-cell;
  }

  .contener .content p{
    padding:2em;    
    border:1px solid #3d3d3d;
    background-color: #1d1d1d;
    border-radius: 10px;
    -moz-box-shadow: 0px 10px 10px black;
    -webkit-box-shadow: 0px 10px 10px black;
    box-shadow: 0px 10px 10px black;
  }

</style> 

<div class="mask">  
   <div class="contener">
     <div class="content">
       <p>Test string</p>
     </div>
   </div>
</div>

This can be easily achieved now with flex. 现在可以通过flex轻松实现这一点。 Microsoft is no longer supporting older versions of windows. Microsoft不再支持旧版Windows。 Plus, Mozilla and Chrome are constantly updating their engines, so flexbox is firmly rooted in all modern browsers. 此外,Mozilla和Chrome不断更新其引擎,因此flexbox牢牢扎根于所有现代浏览器。

#container{
   display: flex;
   justify-content: center;
   align-items: center;
}

Everything set as an immediate child of that #container element will be centered. 设置为#container元素的直接子元素的所有内容都将居中。

This short bit of code is absolutely perfect for things like login menus or centering text over a background image. 这一小段代码绝对适用于登录菜单或在背景图像上居中文本等内容。

EDIT - CODE BELOW IS OUTDATED SEE UPDATED CODE ABOVE 编辑 - 下面的代码是过时的,见上面的更新代码

If you want something vertically and horizontally centered, try this... 如果你想要垂直和水平居中的东西,试试这个......

 #shell{
width:100%;
height:100%
position:absolute;
z-index:100;
background: rgba(255,255,255,.8);
display:table;
}

#inner{
 margin:0 auto;
 display:table-cell;
 vertically-align:middle;
 width: /* input width here */
}


#overlay_container{
   width: /* input width here again */
   height: /* input height here */
   additional-styles: /* self explanatory */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM