如何让div水平垂直居中

如何让div水平垂直居中

@(css)[妙瞳]

引子

我们经常遇到需要把div中的内容进行水平和垂直居中。所以,这里介绍一种方法,可以使div水平居中和垂直居中。

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div水平垂直居中</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div.box{
            background-color:pink;
            border:2px solid #000;
            width:960px;
            height:500px;
            margin-left:50px;
        }
    </style>
</head>
<body>
    <div class="box">
            <img src="girl.jpg" alt="美女">
    </div>   
</body>
</html>

效果图:
191504043003528.png

现在先让图片在div中水平居中
我们可以先给图片套一层盒子。
代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div水平垂直居中</title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
        }
        div.container{
            background-color:pink;
            border:2px solid #000;
            width:500px;
            height:500px;
            margin:50px auto;
            display:table;    
        }
        div.wrapper{
            text-align:center;
            display:table-cell;
            vertical-align:middle;
        }
        div.wrapper img{
            border:1px solid #ddd;
        }  
    </style>
</head>
<body>
    <div class="container">
            <div class="wrapper">
                <img src="girl.jpg" alt="美女"/>                
            </div>
    </div>  
</body>
</html>

IE8/Firefox/Chrome/Safari/Opera页面效果:

191610535359539.png

IE6/IE7页面效果:

191614508002658.png

由此可见要做IE6/IE7的兼容:

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div水平垂直居中</title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
        }
        div.container{
            background-color:pink;
            border:2px solid #000;
            width:500px;
            height:500px;
            margin:0 auto;
            display:table;
            margin-top:20px;
            
        }
        div.wrapper{
            text-align:center;
            display:table-cell;
            vertical-align:middle;
        }
        div.wrapper img{
            border:1px solid #ddd;
        } 
    </style>
    <!--[if lte IE 7]>
        <style type="text/css">
            div.container{
                position:relative;
            }
            div.wrapper{
                position:absolute;
                left:50%;top:50%;
            }
            div.wrapper img{
                position:relative;
                left:-50%;top:-50%;
            }
        </style>
    <![endif]-->
</head>
<body>
    <div class="container">
            <div class="wrapper">
                <img src="girl.jpg" alt="美女"/>                
            </div>
    </div>  
</body>
</html>

IE6/IE7效果图:

191623236441169.png

综上所述,要让div里面的内容水平居中,可以使用text-align:center;
要实现垂直居中,container 的display:table;而wrapper的display:table-cell;同时vertical-align:middle;就可以实现div里的图片水平垂直居中。

假如是多张图片,要实现居中:

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div水平垂直居中</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div.container{
            background-color:pink;
            border:2px solid #000;
            width:700px;
            height:500px;
            margin:0 auto;
            margin-top:50px;
        }
        div.wrapper{
            text-align:center;
            margin-top:28px;
        }
        div.wrapper img{
            border:1px solid #ddd;
            width:200px;
            margin:10px;
        }  
    </style>
</head>
<body>
    <div class="container">
            <div class="wrapper">
                <img src="girl3.jpg" alt="美女"/>  
                <img src="girl3.jpg" alt="美女"/>
                <img src="girl3.jpg" alt="美女"/>
                <img src="girl3.jpg" alt="美女"/>
                <img src="girl3.jpg" alt="美女"/>
                <img src="girl3.jpg" alt="美女"/>
            </div>
    </div>   
</body>
</html>

IE6/IE7/IE8/Firefox/Chrome/Safari/Opera页面效果:

191749559255172.png

div.wrapper中的text-align:center;实现水平居中,margin-top:28px;实现垂直居中。
28px=[500-(200+1+1+10+10)*2]/2,即外层的高度减去里面的高度,然后除以2,设置margin-top,即可居中。

假如有错误或者建议的地方,欢迎指正!-----妙瞳。

转载于:https://www.cnblogs.com/WebShare-hilda/p/4743002.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值