首页 > Web开发 > 详细

css如何让子元素在父元素中水平垂直居中

时间:2019-12-27 10:57:17      阅读:121      评论:0      收藏:0      [点我收藏+]

方法一: display:flex

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css_com/reset.css">
    <style>
        .parents{
            /*添加部分*/
            display: flex;
            align-items: center;
            justify-content: center;
            
            margin:0 auto;
            width: 600px;
            height: 600px;
            background-color: #4eff5e;
        }
        .son{
            width: 300px;
            height: 300px;
            background-color: #ff4236;
        }
    </style>
</head>
<body>
    <div class="parents">
        <div class="son"></div>
    </div>
</body>
</html>

方法二:display:table-cel

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css_com/reset.css">
    <style>
        .parents{
            /*添加部分*/
            display: table-cell;
            vertical-align: middle;

            margin:0 auto;
            width: 600px;
            height: 600px;
            background-color: #4eff5e;
        }
        .son{
            /*添加部分*/
            margin: auto;
            
            width: 300px;
            height: 300px;
            background-color: #ff4236;
        }
    </style>
</head>
<body>
    <div class="parents">
        <div class="son"></div>
    </div>
</body>
</html>

方法三:绝对定位和0

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css_com/reset.css">
    <style>
        .parents{
            /*添加部分*/
            position: relative;

            margin:0 auto;
            width: 600px;
            height: 600px;
            background-color: #4eff5e;
        }
        .son{
            /*添加部分*/
            margin: auto;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;

            width: 300px;
            height: 300px;
            background-color: #ff4236;
        }
    </style>
</head>
<body>
    <div class="parents">
        <div class="son"></div>
    </div>
</body>
</html>

方法四:负边距

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css_com/reset.css">
    <style>
        .parents{
            /*添加部分*/
            position: relative;

            margin:0 auto;
            width: 600px;
            height: 600px;
            background-color: #4eff5e;
        }
        .son{
            /*添加部分*/
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -150px;
            margin-top: -150px;

            width: 300px;
            height: 300px;
            background-color: #ff4236;
        }
    </style>
</head>
<body>
    <div class="parents">
        <div class="son"></div>
    </div>
</body>
</html>

方法四:css3属性中的平移

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css_com/reset.css">
    <style>
        .parents{
            /*添加部分*/
            position: relative;

            margin:0 auto;
            width: 600px;
            height: 600px;
            background-color: #4eff5e;
        }
        .son{
            /*添加部分*/
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);

            width: 300px;
            height: 300px;
            background-color: #ff4236;
        }
    </style>
</head>
<body>
    <div class="parents">
        <div class="son"></div>
    </div>
</body>
</html>

目前总结这四种方法,实际使用的时候根据实际情况选取不同的方法

 

css如何让子元素在父元素中水平垂直居中

原文:https://www.cnblogs.com/SpringAndMoon/p/12105674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!