老师麻烦帮我检查下 css网页布局基础2-14

老师麻烦帮我检查下 css网页布局基础2-14

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>2-14css网页布局编程练习</title>

    <style>

            *{

                margin0;

                padding0;

                background#fff;

            }

            .header{

                width100%;

                height800px;

                background#3bbbca;

                

                

            }

            .header .one{

                width400px;

                height300px;

                floatleft;

                margin-top150px;

                margin-left200px;

                margin-bottom200px;

                

            }

            .header .one img{

                width100%;

                height100%;

            }

            .header .two{

                width400px;

                height300px;

                floatright;

                margin-top150px;

                margin-right200px;

                margin-bottom200px;

            

            }

            .header .two img{

                width100%;

                height100%;

             

            }

            /* 上半部分 */

            .footer{

                width100%;

                height200px;

                backgroundpink;

                positionrelative;

                overflowhidden;

                

            }

            .footer img{

                width100%;

                height100%;

                

            }

         .footer .three{

             width200px;

             height80px;

             floatleft;

             margin-top60px;

             margin-left15%;

         }

         .footer .four{

            width200px;

             height80px;

             positionabsolute;

             /* top: 50%;

             left: 50%;

             margin-left: -100px;

             margin-top: -40px;  老师麻烦帮我看看 为什么这段敲上去是不能居中的*/

             top0;

             left0;

             right0;

             bottom0;

             marginauto auto;

         

         }

         .footer .five{

            width200px;

             height80px;

             floatright;

             margin-top60px;

             margin-right15%;

             

        }    

           

    </style>

</head>

<body>

    <div class="header">

        <div class="one"> <img src="http://img1.sycdn.imooc.com/climg//58c0f808000129a303600215.jpg" ></div>

        <div class="two"><img src="http://img1.sycdn.imooc.com/climg//58c0f819000198a703600214.jpg" ></div>

    </div>

    <div class="footer">

        <div class="three"><img src="http://img1.sycdn.imooc.com/climg//58c0f81d0001fe4402000060.jpg" ></div>

        <div class="four"><img src="http://img1.sycdn.imooc.com/climg//58c0f8220001dfce02000060.jpg" ></div>

        <div class="five"><img src="http://img1.sycdn.imooc.com/climg//58c0f8780001c74602000060.jpg" ></div>

    </div>

</body>

</html>


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

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

1回答
好帮手慕久久 2021-08-26 15:42:35

同学你好,代码中有如下问题:

1、图片与盒子上下的间距不相等(红框所示间距不相等),左右间距不相等(粉框所示间距不相等):

http://img1.sycdn.imooc.com//climg/6127417b09b595bd19200895.jpg

建议给div.header设置固定宽度,让其整体居中,然后调整其高度以及子元素的间距:

http://img1.sycdn.imooc.com//climg/6127428709f278d905760414.jpg

http://img1.sycdn.imooc.com//climg/6127435a098ea21612080810.jpg

http://img1.sycdn.imooc.com//climg/6127436c09a6d31105980201.jpg

2、footer中的图片,同样的问题,调整如下:

http://img1.sycdn.imooc.com//climg/612743b809b7558d06070308.jpg

http://img1.sycdn.imooc.com//climg/612743db09c8c2e104570159.jpg

http://img1.sycdn.imooc.com//climg/6127446909e419ca06720720.jpg

调整后的样式代码如下:

 * {

            margin: 0;

            padding: 0;

        }


        .header {

            width: 1200px;

            /* 保证图片上下间距相等 */

            padding: 30px 0;

            /* 整体居中 */

            margin: 0 auto;

            /*为了有高度,可以清除浮动 */

            overflow:hidden;


            background: #3bbbca;

        }


        /* 两个子元素的样式可以统一设置(简化代码) */

        .header .one, .header .two {

            float: left;

            /* 父元素1200px,图片每个400px,共3个间隙,每个间隙=(1200-800)/3=133.333 */

            margin-left: 133.3333px;

        }


       

        .header .two img {

            width: 100%;

            height: 100%;

        }


        /* 上半部分 */

        .footer {

            width: 1200px;

            padding: 20px 0;

            background: pink;

            overflow: hidden;

            margin: 0 auto;

        }

        .footer div  {

            float: left;

            /*4个间隙: (1200-600)/=150 */

            margin-left: 150px;

        }

问题解答如下:

如下代码可以让.four居中:

http://img1.sycdn.imooc.com//climg/612745080913535e08160261.jpg

http://img1.sycdn.imooc.com//climg/612745400960506b19100710.jpg

同学再看一下。

如果同学想让整体占满整个屏幕,建议同学将图片所在的结构宽度都设置成百分比,然后计算一下间距应该设置成百分之多少(图片百分比+间距百分比=100%),同学可以参考老师的思路,自己尝试实现一下。

祝学习愉快!

  • 提问者 豆包侠 #1
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2-14css网页布局编程练习</title>
    <style>
    *{

    margin: 0;
    padding: 0;

    }

    .header {
    width: 100%;

    padding: 30px 0;

    margin: 0 auto;

    overflow:hidden;
    background: #3bbbca;
    }


    .header .one, .header .two {
    float: left;

    margin-left: 20%;
    }

    .header .two img {
    width: 100%;
    height: 100%;
    }
    /* 老师这里也应该在需要设置下 .one的图片宽高百分百吧 */
    /* 上半部分 */

    .footer {
    width: 100%;
    padding: 20px 0;
    background: pink;
    overflow: hidden;
    margin: 0 auto;
    }

    .footer div {
    float: left;

    margin-left: 14.285714%;
    }

    </style>
    </head>
    <body>
    <div class="header">
    <div class="one"> <img src="http://img1.sycdn.imooc.com/climg//58c0f808000129a303600215.jpg" ></div>
    <div class="two"><img src="http://img1.sycdn.imooc.com/climg//58c0f819000198a703600214.jpg" ></div>
    </div>
    <div class="footer">
    <div class="three"><img src="http://img1.sycdn.imooc.com/climg//58c0f81d0001fe4402000060.jpg" ></div>
    <div class="four"><img src="http://img1.sycdn.imooc.com/climg//58c0f8220001dfce02000060.jpg" ></div>
    <div class="five"><img src="http://img1.sycdn.imooc.com/climg//58c0f8780001c74602000060.jpg" ></div>
    </div>
    </body>
    </html>

    老师我用百分比从新写了下 麻烦检查下

    2021-08-26 16:56:10
  • 好帮手慕久久 回复 提问者 豆包侠 #2

    同学你好,代码中有如下问题:

    1、优化建议:header和footer的宽度默认就是百分百,可以不设置width:100%;而且也没必要设置margin: 0 auto;:

    http://img1.sycdn.imooc.com//climg/61275f2209cf0d0a04830241.jpg

    http://img1.sycdn.imooc.com//climg/61275f14090afe9c04930254.jpg

    2、当页面宽度变小后,图片之间的间距就不相等了:

    http://img1.sycdn.imooc.com//climg/61275ffc095e63ff18250484.jpg

    这是因为图片的宽度是固定的(原始大小),并没有使用百分比,修改如下:

    http://img1.sycdn.imooc.com//climg/61275fc4095aac6506010432.jpg

    同理,footer区也要这样调整:

    http://img1.sycdn.imooc.com//climg/6127606d099be51c05990324.jpg

    ​问题解答如下:

    .one,.two需要设置百分比,而图片的宽度需要设置100%(与父元素一样宽);高度可设置、也可以不设置,因为图片会根据宽度,自动计算出合适的高度。

    祝学习愉快!

    2021-08-26 17:37:50
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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