2-4-编程练习的居中方法?

2-4-编程练习的居中方法?

如以下代码所示,使用居中方法1,当浏览器窗口缩小时,慕课网logo能够完整显示。

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

使用居中方法2,当浏览器窗口缩小时,慕课网logo不能够完整显示。

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

这是为什么呢?

代码中存在的其它问题,请老师批评指正。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

   <head>
      <title>2-4-编程练习.html</title>
      <meta charset="UTF-8">
      <meta name="renderer" content="webkit">
      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      <style type="text/css">
            *
            {
                margin: 0;
                padding: 0;
            }

            a
            {text-decoration: none;}

            a:link,
            a:visited,
            a:active
            {color: #FFF;}

            a:hover
            {color: #F00;}

            nav
            {
                position: absolute;
                min-width: 1100px; /* 定宽1100px */
                width: 1100px;
                min-height: 100px;
                height: 100px; /* 定高100px */
                line-height: 100px;
                color: #FFF;
                background-color: #000;

                /* 居中方法1 */
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                margin: auto;

                /* 居中方法2 */
                /*top: 50%;*/
                /*left: 50%;*/
                /*margin-top: -50px;*/
                /*margin-left: -550px;*/
            }

            h1,
            h1 > a /* 导航logo定高 */
            {
                display: inline-block;
                width: 300px;
                height: 100px;
            }

            #nav-list /* 导航列表 */
            {
                display: inline-block;
                position: absolute;
                left: 500px; /* 因为父元素nav定宽1100px */
                width: 600px; /* 若left定为500px,则width=1100-500=600px */
                height: 100px;
            }

            #nav-list > li /* 导航列表项 */
            {
                display: inline-block;
                font: 16px "微软雅黑";
            }

            #nav-list > li:nth-child(n) /* 导航项文字之间距离70px */
            {margin-left: 70px;}
        </style>
   </head>

   <body>
      <nav>
         <h1><a href="http://www.imooc.com" target="_blank"><img src="http://img1.sycdn.imooc.com/climg//58c0d2d900016ce303000100.png"></a></h1>
         <ul id="nav-list">
            <li><a href="https://www.imooc.com/course/list" target="_blank">课程</a></li>
            <li><a href="https://class.imooc.com/" target="_blank">职业路径</a></li>
            <li><a href="https://coding.imooc.com/" target="_blank">实战</a></li>
            <li><a href="https://www.imooc.com/wenda" target="_blank">猿问</a></li>
            <li><a href="https://www.imooc.com/article" target="_blank">手记</a></li>
            </ul>
      </nav>
   </body>

</html>


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

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

1回答
好帮手慕星星 2019-06-12 10:13:36

你好,这两种方式在100%页面下效果都是正确的。

第一种方式是因为有left:0;存在,所以视口缩小之后,显示在左边。

第二种方式是因为设置了margin-left值大于left定位的值,导致部分看不见。例如:

视口的宽度是600px,那么nav元素设置left为50%,是相对于视口来说的,就会在300的位置上,而设置margin-left值为-550px,向左移动的距离会大于向右移动的距离,所以会有部分看不见。

自己可以测试理解下,祝学习愉快!

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

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

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

0 星
请稍等 ...
微信客服

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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