zoukankan      html  css  js  c++  java
  • css实现子层在父层中水平垂直居中

    例子:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>css实现子层在父层中水平垂直居中</title>
    <style type="text/css">
    #parent {
        position:absolute;
        top:50%;
        left:50%;
        margin:-250px 0 0 -200px;
        height:500px;
        width:400px;
        background-color:red;
    }
    #child {
        position:absolute;
        top:50%;
        left:50%;
        margin:-100px 0 0 -150px;
        height:200px;
        width:300px;
        background-color:green;
    }
    </style>
    </head>
    <body>
    <div id="parent">
        <div id="child"></div>
    </div>
    </body>
    </html>

    解释:
        id为parent的层的父层是body,id为child的层的父层是parent。
    需要注意点:
        一:子层必须设置position为absolute,父层必须设置position为relative;absolute可以作为另一个absolute的父层。
        二:子层top设置为50%,left也设置为50%。
        三:子层的margin-top设置为本身高度的一半,并且为负值;margin-left设置为本身宽度的一半,并且为负值。
    效果:
        子层水平垂直居中于父层。

  • 相关阅读:
    Oracle基础知识
    tomcat服务器
    jquery实现常用UI布局
    css画布
    css布局
    jquery快速常用技能
    css快速浏览
    css选择器
    spring boot项目mybatis配置注解+配置文件
    sass的安装和基础语法
  • 原文地址:https://www.cnblogs.com/kelly007/p/5072144.html
Copyright © 2011-2022 走看看