网页元素居中攻略记_图片水平垂直居中

Posted crper

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网页元素居中攻略记_图片水平垂直居中相关的知识,希望对你有一定的参考价值。

前言

借助table的特性来实现


代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css" media="screen">
        .test{
            display:table;
            height:800px;
            width:800px;
            background:#99cc99;
            border-radius:5px;
            border:1px solid #f70;

        }
        .test>span{
            display:table-cell;
            vertical-align:middle;
            text-align:center;
        }
        .test>span>img{
            height:400px;
            width:400px;
            border-radius:50%;
            box-shadow:1px 1px 3px rgba(0,0,0,.5);
        }
    </style>
</head>
<body>
    <div class="test">
        <span><img src="http://d.hiphotos.baidu.com/image/h%3D200/sign=8663264274f082023292963f7bfbfb8a/f3d3572c11dfa9eca13b947665d0f703918fc1be.jpg" alt=""></span>
    </div>
</body>
</html>

效果图

技术分享


结语

用position来定位也可以…这里只是展示另一种图片居中的方式,,感觉相当方便

以上是关于网页元素居中攻略记_图片水平垂直居中的主要内容,如果未能解决你的问题,请参考以下文章

图片水平垂直居中

图片垂直水平居中

CSS水平居中与垂直居中的总结

HTML CSS中如何实现DIV中的图片水平垂直居中对齐

网页制作图片怎么居中

css 单行图片文字水平垂直居中汇总