简体   繁体   English

在页面加载中将DIV水平和垂直居中

[英]Centering DIV Horizontally & Vertically on Page Load

How can I center a DIV both horizontally and vertically immediately when the page loads? 页面加载后,如何立即在水平和垂直方向上使DIV居中?

I am currently using the following solution: 我目前正在使用以下解决方案:

HTML: HTML:

<div class="container">
  <div class="visitorSelect">
    <a href="/visitorLog/boeing">
      <div class="tile double icon bg-color-blue">
        <div class="tile-content">
          <i class="icon-plane"></i>
        </div>
        <div class="brand">
          <span class="name">Employee</span>
        </div>
      </div>
    </a>

    <a href="/visitorLog/guest">
      <div class="tile double icon bg-color-orange">
        <div class="tile-content">
          <i class="icon-group"></i>
        </div>
        <div class="brand">
          <span class="name">Guest</span>
        </div>
      </div>
    </a>
  </div> <!-- visitorSelect -->
</div> <!-- container -->

JavaScript: JavaScript的:

<script>
$(document).ready(function()
{
  $(window).resize(function()
  {
    $('.visitorSelect').css(
    {
      position: 'absolute',
      left: ($(window).width() - $('.visitorSelect').outerWidth()) / 2,
      top: ($(window).height() - $('.visitorSelect').outerHeight()) / 2
    });
  });

  // call `resize` to center elements
  $(window).resize();
});
</script>

When I initially load the page, the DIV to center shows up at the right of the page and slightly below the center of vertical. 最初加载页面时, DIV居中显示在页面的右侧,略低于垂直中心。 However, when I resize the page manually it snaps to exactly where it should. 但是,当我手动调整页面大小时,它会精确地对齐到应有的位置。

What additional steps do I need to take to cause the centering properly place the element at the time the document loads? 我需要采取什么其他步骤来使元素在文档加载时正确居中?

Unless this is some kind of modal, or something unusual, I would not do this entirely with script when CSS Margin Auto will work for this. 除非这是某种形式的模式或不寻常的方式,否则当CSS Margin Auto可以为此工作时,我不会完全使用脚本来完成此操作。 Here is a tutorial http://demo.tutorialzine.com/2010/03/centering-div-vertically-and-horizontally/demo.html 这是一个教程http://demo.tutorialzine.com/2010/03/centering-div-vertically-and-horizo​​ntally/demo.html

Here is an example of the CSS: 这是CSS的示例:

div.container{
  width:100%;
  height:100%;
}
div.visitorSelect {
  width:200px;
  height:200px;
  margin:auto;
}

Set the .visitor element to absolute positioning before you calculate the width. 计算宽度之前 ,请将.visitor元素设置为绝对定位。

By default static divs will stretch to the full width of the parent; 默认情况下,静态div会拉伸到父级的整个宽度; absolute divs will not. 绝对div不会。 This is messing up your first calculation. 这弄乱了您的第一个计算。

You should probably set the position css rule outside of the resize event function, in either CSS or in the ready() function. 您可能应该在CSS或ready()函数的resize事件函数之外设置位置css规则。 Here's a fix with the least changes to your code: 这是对代码更改最少的修复程序:

$(document).ready(function()
{
  $(window).resize(function()
  {
    $('.visitorSelect').css(
    {
      position: 'absolute'
    });

    $('.visitorSelect').css(
    {
      left: ($(window).width() - $('.visitorSelect').outerWidth()) / 2,
      top: ($(window).height() - $('.visitorSelect').outerHeight()) / 2
    });
  });

  // call `resize` to center elements
  $(window).resize();
});

You should position the div once the page loads: 页面加载后,您应该定位div

<script>
$(document).ready(function()
{
  $('.visitorSelect').css(
    {
      position: 'absolute',
      left: ($(window).width() - $('.visitorSelect').outerWidth()) / 2,
      top: ($(window).height() - $('.visitorSelect').outerHeight()) / 2
    });

  $(window).resize(function()
  {
    $('.visitorSelect').css(
    {
      position: 'absolute',
      left: ($(window).width() - $('.visitorSelect').outerWidth()) / 2,
      top: ($(window).height() - $('.visitorSelect').outerHeight()) / 2
    });
  });

  // call `resize` to center elements
  $(window).resize();
});
</script>

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

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