html – 垂直居中页面内容

前端之家收集整理的这篇文章主要介绍了html – 垂直居中页面内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道如何用CSS水平居中整个页面.但有没有办法垂直居中页面?像这样……

解决方法

在MicroTut中有一篇很棒的文章来做 http://tutorialzine.com/2010/03/centering-div-vertically-and-horizontally/

以CSS为中心:

  1. .className{
  2. width:300px;
  3. height:200px;
  4. position:absolute;
  5. left:50%;
  6. top:50%;
  7. margin:-100px 0 0 -150px;
  8. }

以JQuery为中心:

  1. $(window).resize(function(){
  2.  
  3. $('.className').css({
  4. position:'absolute',left: ($(window).width() - $('.className').outerWidth())/2,top: ($(window).height() - $('.className').outerHeight())/2
  5. });
  6.  
  7. });
  8.  
  9. // To initially run the function:
  10. $(window).resize();

你可以看到一个演示here

猜你在找的HTML相关文章