首页 建站教程 WEB前端 正文

DIV内容居中

方法一:<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>任意盒子居中calc</title> <style type="t...

方法一:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>任意盒子居中calc</title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
        }
        html,body{
            height:100%;
        }
        .element{
            height:100%;
            width: 100%;
            background-color:blue;
            position: relative;
        }

        .element img{
            position: absolute;
            /* css3可以计算属性 */
            top:calc(50% - 150px);
            left:calc(50% - 225px);
        }
 
    </style>
</head>
<body>

<div>
    <img src="https://www.umtheme.com/zb_users/upload/2017/03/201703261490505576415367.jpg" alt="" />
</div>
</body>
</html>


方法二

<!DOCTYPE html>
 <html>
 <head>
     <meta charset="UTF-8" />
     <title>任意盒子居中CSS3</title>
     <style type="text/css">
         *{
             margin:0;
             padding:0;
         }
         html,body{
             height:100%;
             background-color:red;
         }
         .element{
             height:100%;
             width: 100%;
             background-color:blue;
             position:relative;
         }
         .element img{
             position:absolute;
             top:50%;
             left:50%;
             
             -webkit-transform: translate(-50%,-50%);
                -moz-transform: translate(-50%,-50%);
                 -ms-transform: translate(-50%,-50%);
                  -o-transform: translate(-50%,-50%);
                     transform: translate(-50%,-50%);
         }
 
     </style>
 </head>
 <body>
 <div>
     <img src="https://www.umtheme.com/zb_users/upload/2017/03/201703261490505576415367.jpg" alt="" />
 </div>
 </body>
 </html>


定位居中:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>定位与居中</title>
    <style>
*{
    padding:0;
    margin:0;
}
.wrapper{
    width:400px;
    height:200px;
    background-color:#ccc;
    position:absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;
    overflow:hidden;
    margin:auto;
}
.style{
    position:absolute;
    width:100px;
    height:100px;
    border-radius:50px;
    background:#fc0;
}
.rt{
    top:-50px;
    left:-50px;
}
.lb{
    bottom:-50px;
    right:-50px;
}
    </style>
</head>
<body>
    <div>
        <div class="style rt"></div>
        <div class="style lb"></div>
    </div>
</body>
</html>


任意居中:

<html>
<head>
    <meta charset="UTF-8" />
    <title>任意盒子居中display:table</title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
        }
        html,body{
            height:100%;
        }
        .element{
            height:100%;
            width: 100%;
            background-color:blue;
            text-align: center;
            display:table;
        }
        .element .img{
            display:table-cell;
            vertical-align:middle;
        }
    </style>
</head>
<body>
<div>
   <div>
        <img src="1.jpg" alt="" />
   </div>
</div>
</body>
</html>


打赏
海报

本文由优美主题原创或收集发布

转载请注明本文地址:https://www.umtheme.com/web/20.html

相关推荐

CSS心形缠绕转动效果

CSS心形缠绕转动效果

CSS心形缠绕转动效果是一种有趣的动画效果,能够给网页带来浪漫而动感的氛围。这个效果将一个心形图案无限旋转,并且同时绕着一个中心点缠绕旋...
WEB前端 2023.09.13 0 2918
制作h5常用的css3动画效果

制作h5常用的css3动画效果

css3提供了许多强大的特效,可以用来实现各种各样的效果,下面是我总结的一些常用的css3效果,有需要的可以直接引用:1.悬浮时放大:.one{transition:All 0.4s&nb...
WEB前端 2023.09.13 1 2857
发布评论

感谢您的支持