使用 jQuery 使元素填充浏览器视口的整个高度

Making an element fill the entire height of browser viewport using jQuery

代码

演示

HTML 的基本形式如下所示:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

W.r.t HTML,我正在尝试使用 JavaScript/jQuery 使元素 #main 填充浏览器视口的整个高度,如下所示:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

正如上面代码中的注释中清楚地解释的那样,代码会自动计算要应用于 #main 的必要填充,使其高度等于浏览器视口的高度。

它工作得很好,除了在我能够识别的一种情况下计算的填充(以及由此产生的高度)是错误的。

当您将浏览器窗口大小调整为 567x724 px(这意味着 551x611 px 视口大小)时,至少在 Windows 7、Google Chrome 浏览器(最新)上很容易重现(您可以使用 Window Resizer 之类的扩展程序),您会注意到元素的计算填充导致其高度大于浏览器视口的高度。

为什么会这样?我无法在任何其他分辨率下重现相同的内容。我可能会在这里遗漏什么?


首先,Jquery 的 .outerheight() 函数包含填充,这意味着当您在此函数第一次运行后测量 #Main 元素的高度时,它将等于 window.height。换句话说 - 当您调整浏览器大小时,它看起来会很糟糕。当您以老式方式调整浏览器窗口的大小时,您可以在这个小提琴上看到这一点。您可以改用边距,但是您必须对 CSS 进行相当多的调整。即便如此,调整窗口大小仍然看起来很糟糕且有问题,并且跨浏览器的结果不一致。你可以在这个小提琴上看到。

您所指的具体错误可能是由于您调整窗口大小时的数学不一致 - 您使用填充(包含在上面提到的 .outerheight() 中)和视口大小不容易分割的组合2(不可能有半个像素,不同的浏览器会以不同的方式呈现半个像素)。

我还要指出这行代码:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

这会强制您的页面始终为 #main.height() + 60,它可以大于您的可视窗口,具体取决于您的窗口大小。 #main.height() 大约为 200.5 像素(我们还有半个像素)。

假设您的目标是使 #Main 元素垂直居中,您最好的选择是使用许多可用的直接 CSS 方法之一。 table 方法在这里似乎最适用,因为它是完全动态的(因此可以响应) - 只需创建一个单元格 CSS 表格,使用 CSS valign,并在该单元格内构建整个页面。对于旧版本的 IE,您需要使用一个小技巧 (display:Inline-block;) 才能使其工作。

HTML:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

CSS:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

IE 修复:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

我最近一直在做很多关于居中内容的实验。此技术无需在任何元素上设置高度即可工作。

这适用于 ie9 及更高版本。


根据@Thomas 的回答,我确定了两种可能的解决方案。考虑到更好的浏览器支持,我将使用#2 解决方案。

1。使用单位 vh(视口高度)。浏览器支持:IE9及以上。

CSS:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

2。动态设置父级(.home #primary)的高度等于浏览器视口的高度。

JS:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

CSS:

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

在你的 CSS 中

<main id="main" class="site-main" role="main">



      <!-- blah, blah, blah! -->



    </main>
jQuery(document).ready(function($){



  // get height of browser viewport

  var window_h = $(window).height();



  // get height of the jumbotron, i.e. element #main

  var jumbotron_h = $('.home #main').outerHeight(true);



  // calculate necessary padding (top/bottom) to apply on #main so that the

  // element's height is equal to that of the browser's viewport,

  // and its contents are centered vertically

  if (window_h > (jumbotron_h + 60)) {

    var jumbotron_padding = (window_h - jumbotron_h)/2

  } else {

    var jumbotron_padding = 30

  }



  // apply calculated padding on the element dynamically

  $('.home #main').attr('style', 'padding-top:'+jumbotron_padding+'px;padding-bottom:'+jumbotron_padding+'px;');



});

if (window_h > (jumbotron_h + 60)) {

  var jumbotron_padding = (window_h - jumbotron_h)/2

} else {

  var jumbotron_padding = 30

}
  Content here#parent {display: table;}



#child {

  display: table-cell;

  vertical-align: middle;

}
#child {

  display: inline-block;

}
   body {

    height:100vh;

   }



   .element-to-be-centered { 

    font-size:1em; 

    text-align:center; 

    top: 49%;

    -webkit-transform: translateY(-49%);

    -ms-transform: translateY(-49%);

    transform: translateY(-49%);

   }
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100vh; /* 100% viewport height */

  vertical-align: middle;

}
jQuery(document).ready(function($){



  var window_h = $(window).height();

  var jumbotron_h = $('.home #main').height();



  if (window_h <= (jumbotron_h + 60)) {

    var window_h = jumbotron_h + 60

  }



  $('.home #primary').attr('style', 'height:'+window_h+'px;');



});
.home #primary { /* Parent */

  display: table;

  width: 100%;

}



.home #main { /* Child */

  display: table-cell;

  height: 100%; /* 100% height of parent */

  vertical-align: middle;

}
html, body {

  height: 100%;

}



div, #main {

  height: 100%;

}

相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161