元素水平或垂直居中问题

一、元素的水平居中
⑴.行内元素的水平居中:text-align
⑵.块级元素的水平居中:margin:0 auto
二、元素的垂直居中
⑴.行内元素垂直居中:
    line-height设置为与父级元素的高度一样
⑵.、块级元素垂直居中:
1.可以给父级使用相对定位,子级使用绝对定位,将top、left、right、bottom为0    
    a.给父级相对定位,子级绝对定位:left:50%;top:50%;margin-left:-子级元素宽度一半;margin-top:-子级元素高度一半
    b.给父级相对定位,子级绝对定位 height:百分比x; x属于0~100%    margin:auto;

 eg:

                                <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <title>垂直居中</title>
                    <style type="text/css">
                        /*IE版本低于7时不能正常使用*/
                        .outer{
                            height: 200px;
                            position: relative;
                            background: #bfe;
                        }
                        .outer .inner{
                            position: absolute;
                            top: 0;
                            bottom: 0;
                            left: 0;
                            right: 0;
                            width: 50%;
                            height: 30%;
                            margin: auto;
                            background: coral;
                        }

                    </style>
                </head>
                <body>
                    <div class="outer">
                        <div class="inner">inner content</div>
                    </div>
                </body>
                </html>                        
View Code

    c.给父级和子级都加绝对定位,再给子级添加top:calc(50% - 子级元素高度一半)、left:calc(50% - 子级元素宽度一半)
            
    d.给父级一个display:flex; align-items:center;再给子级添加:margin:0 auto;(使用弹性布局垂直轴居中)
        
⑶.样式表格方式:【CSS Table Method】(父元素与子元素等高等宽;父元素背景颜色无效;二者为行内块级元素)
    eg:

            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>垂直居中</title>
                <style type="text/css">
                    .outer{
                        height: 200px;
                        display: table;
                        /*background: #bfe;  该背景会被子元素覆盖不会显示出来*/
                    }
                    .outer .inner{
                        display: table-cell;
                        vertical-align:middle;
                        background: coral;
                        
                        /*若为版本较低的ie6使用下面样式*/
                        display: inline-block;
                    }
                </style>
            </head>
            <body>
                <div class="outer">
                    <div class="inner">inner content</div>
                </div>
            </body>
            </html>
                           
View Code

          
⑷.图片在div中垂直居中:
    eg:   

<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>垂直居中</title>
            <style type="text/css">
                .parent{
                    line-height: 200px;
                    background: #bfe;
                }
                .parent img{
                    vertical-align: middle;
                    border: 1px solid black;
                }
            </style>
        </head>
        <body>
            <div class="parent">
                <img width="100px" src="../html_css/day06/images/开心果.png" alt="图片迷路了" />
            </div>
        </body>
        </html>
View Code

   
⑸.借用兄弟元素float-div来设置:
    eg:

        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Floater Div</title>
            <style type="text/css">
                .outer{
                    height: 200px;
                    background: #bfe;
                }
                .floater{
                    float: left;
                    height: 50%;
                    width: 100%;
                    margin-bottom: -50px;
                    /*background: teal;*/
                }
                .inner{
                    clear: both;
                    height: 100px;
                    background: coral;
                }
            </style>
        </head>
        <body>
            <div class="outer">
                <div class="floater"></div>
                <div class="inner"></div>
            </div>
        </body>
        </html>
View Code

 

转载于:https://www.cnblogs.com/nzcblogs/p/11128625.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值