position-absolute的居中问题

position-absolute的居中问题

按老师的方法书写在浏览器中不能居中老师帮忙看一下呗

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>绝对定位</title>
    <style type="text/css">
        /*.block {
            width: 80px;
            height: 80px;
            line-height: 80px;
            text-align: center;
            border: 2px solid black;
        }*/
            /*.block:nth-child(1){
                position: absolute;
                left: 20px;
                top: 20px;
                border-color: blue;
                z-index: 999999;
            }*/
        /*.block:nth-child(2){
            position: absolute;
            left: 20px;
            top: 20px;
            width: 50%;
            height: 50%;
            border-color: red;
            z-index: 1;
        }*/
        .parent {
            position: relative;
            width: 200px;
            height: 150px;
            background: blue
        }
        .child {
            position: absolute;
            width: 80px;
            height: 80px;
            background:red;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
            margin:auto auto;
        }

    </style>
</head>
<body>
    <!-- <div class="block">A</div>
    <div class="block">B</div>
    <div class="block">C</div>
     -->
     <div class="parent">
         <div class="child">
             
         </div>
     </div>
</body>
</html>

http://img1.sycdn.imooc.com//climg/5b795a940001fb1403000245.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕珊 2018-08-20 11:25:49
改正后的代码如下:注意看一下关于top和left的注释。祝学习愉快!
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>绝对定位</title>
    <style type="text/css">
        /*.block {
            width: 80px;
            height: 80px;
            line-height: 80px;
            text-align: center;
            border: 2px solid black;
        }*/
            /*.block:nth-child(1){
                position: absolute;
                left: 20px;
                top: 20px;
                border-color: blue;
                z-index: 999999;
            }*/
        /*.block:nth-child(2){
            position: absolute;
            left: 20px;
            top: 20px;
            width: 50%;
            height: 50%;
            border-color: red;
            z-index: 1;
        }*/
        .parent {
            position: relative;
            width: 200px;
            height: 150px;
            background: blue
        }
        .child {
            position: absolute;
            width: 80px;
            height: 80px;
            background:red;
            top: 35px;
            /*  top和left都是通过计算得到的,如top
            是parent的宽度减去child的宽度,然后除以2*/
            left: 60px;
            margin:0 auto;
        }
    </style>
</head>
<body>
    <!-- <div class="block">A</div>
    <div class="block">B</div>
    <div class="block">C</div>
     -->
     <div class="parent">
         <div class="child">
             
         </div>
     </div>
</body>
</html>


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师