全屏垂直居中HTML页面的方法?

编程入门 行业动态 更新时间:2024-10-19 08:49:45
本文介绍了全屏垂直居中HTML页面的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为HTML网页寻找有效的跨浏览器解决方案:

I'm looking for a valid cross-browser solution for an HTML page which:

  • 使用100%的屏幕高度,没有溢出(即没有滚动)
  • 具有垂直(和水平)居中的< div>
  • Consumes 100% of the screen height, with no overflow (i.e. no scrolling)
  • has a vertically (and horizontally) centered <div> which will hold the main content
  • 我知道当包装容器具有静态高度时,可以进行垂直居中。是调整这个高度到浏览器窗口高度可行吗? (最好不要使用JS。)

    I know vertical centering is possible when the wrapping container has a static height. Is adjusting this height to browser window height something feasable? (Preferably, no JS should be used.)

    推荐答案

    取决于cross browser的意思。下面的工作正常与所有当前,标准兼容的(因此不是IE6):

    Depends on what you mean with "cross browser". Following works fine with all current, standards compatible ones (thus not IE6):

    HTML:

    <div id="a"> <div id="b"> <div id="content"></div> </div> </div>

    CSS:

    html, body, #a { margin: 0; padding: 0; height: 100%; width: 100%; } #a { display: table; } #b { display: table-cell; margin: 0; padding: 0; text-align: center; vertical-align: middle; } #content { border: 5px solid red; width: 100px; height: 100px; margin: auto; }

    活动示例:

    jsfiddle/mGPmr/1/

    更多推荐

    全屏垂直居中HTML页面的方法?

    本文发布于:2023-10-08 01:31:13,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1471119.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:全屏   页面   方法   HTML

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!