css居中

(1)莫名其妙的居中

<style>
        #outer{
            width: 400px;
            height: 400px;
            border: 2px solid #333;
            position: relative;
            margin: 100px auto;
        }
        #inner{
            width: 50%;
            height: 50%;
            background-color: #2e0088;
            left: 0;
            right: 0;
            bottom: 0;
            top: 0;
            position: absolute;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="outer">
        <div id="inner"></div>
    </div>
</body>

(2)absolute+translate

 <style>
        #outer{
            width: 400px;
            height: 400px;
            border: 2px solid #333;
            position: relative;
            margin: 100px auto;
        }
        #inner{
            width: 50%;
            height: 50%;
            background-color: #2e0088;
            left: 50%;
            top: 50%;
            transform: translate(-100px,-100px);
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="outer">
        <div id="inner"></div>
    </div>
</body>
css居中.PNG
(1)百度前端技术学院css居中
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    .box{
        width: 400px;
        height: 200px;
        background-color: #ccc;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-top: -100px;
        margin-left: -200px;
        overflow: hidden;
    }
    .box div{
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: #fc0;
        border-radius: 50%;
    }
    .box .left{
        left: -50px;
        top: -50px;
    }
    .box .right{
        right: -50px;
        bottom: -50px;
    }
    </style>
</head>
<body>
    <div class="box">
        <div class="left"></div>
        <div class="right"></div>        
    </div>
</body>
</html>
image.png

最佳实践

* {
    margin: 0;
    padding: 0;
}
.main {
    position: absolute;
    width: 400px;
    height: 200px;
    background-color: #ccc;
    top: 50%;
    margin-top: -100px;
    left: 50%;
    margin-left: -200px;
}
.left-top-area {
    width: 50px;
    height: 50px;
    border-radius: 0 0 50px 0;
    position: absolute;
    background-color: #FC0;
}
.right-bottom-area {
    width: 50px;
    height: 50px;
    border-radius: 50px 0 0 0;
    position: absolute;
    background-color: #FC0;
    right: 0;
    bottom: 0;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>task-04</title>
    <link rel="stylesheet" href="styles/main.css">
</head>
<body>
    <div class="main">
        <div class="left-top-area"></div>
        <div class="right-bottom-area"></div>
    </div>  
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在CSS中如何居中这事儿是CSS为人所抱怨的典型代表之一。“为毛它这么难?%>_<%”,开发者们抱怨道。我认为这个...
    红绿灯的黄阅读 1,053评论 0 1
  • 本文主要总结几种常见的CSS居中方式,下面我准备分为三个方向来写,分别是水平居中,垂直居中,水平垂直居中。水平居中...
    IrisLong阅读 720评论 0 2
  • CSS中在不同场景下,解决居中的方式有很多,经常让人无从下手,所有我们将CSS居中进行一次小结,方便以后我们布局使...
    字母31阅读 286评论 0 1
  • 文/凉予 多久未曾如此放肆享受夜晚,总是早早睡去,一夜安好,便是幸福。 在两个城市中穿梭,一边是工作,一边是家庭,...
  • 带着孩子跟老公出去了一趟,走到哪都会有朋友说,你家儿子看着像谁啊,两个都不像但两个都又像,我笑着说:“那说明我和老...
    做自己命运的主宰阅读 259评论 0 0