请老师检查五星红旗代码

请老师检查五星红旗代码

<!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>Document</title>

    <style>

      * {

        margin0;

        padding0;

      }

      .hongqi {

        /* 怎么控制3:2 */

        width300px;

        height200px;

        background-colorred;

        margin60px auto;

        positionrelative;

      }

      .star1::before {

        content"★";

        coloryellow;

        font-size90px;

        positionabsolute;

       /* 这里为什么是负数才行呢? */

        top-10px;

        left20px;

      }

      .star2::before {

        content"★";

        coloryellow;

        font-size20px;

        positionabsolute;

        top0px;

        left95px;

        transformrotate(10deg);

        

    }

    .star3::before {

        content"★";

        coloryellow;

        font-size20px;

        positionabsolute;

        top27px;

        left110px;

        transformrotate(-20deg);

    }

    .star4::before {

        content"★";

        coloryellow;

        font-size20px;

        positionabsolute;

        top54px;

        left110px;

    }

      .star5::before {

        content"★";

        coloryellow;

        font-size20px;

        positionabsolute;

        top77px;

        left100px;

        transformrotate(20deg);

    }


    </style>

  </head>

  <body>

      <div class="hongqi">

        <div class="star1"></div>

        <div class="star2"></div>

        <div class="star3"></div>

        <div class="star4"></div>

        <div class="star5"></div>

    </div>

  </body>

</html>



正在回答

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

1回答

同学你好,效果实现是可以的,不过练习要求使用css设置边框实现五角星,具体参考如下解析:

(1)先看下三角的绘制思路,如下:

http://img1.sycdn.imooc.com//climg/60fb713309f9418106220457.jpg

给一个元素设置宽高以及边框

http://img1.sycdn.imooc.com//climg/60fb714709ae8e2601590160.jpg

当宽高不断减少时 , border会越趋向于三角形 , 如下:

http://img1.sycdn.imooc.com//climg/60fb717a099a94a200730064.jpg

当宽高为0时,border变成了三角 , 如下:

http://img1.sycdn.imooc.com//climg/60fb718e09c6e1f700680059.jpg

一个三角就是把其他角度的边框都设置为透明transparent,留一个角度的边框即可。

(2)五角星实现如下:


http://img1.sycdn.imooc.com//climg/60fb71e4091785f906770552.jpg

效果:
http://img1.sycdn.imooc.com//climg/60fb71f1095f60a402190151.jpg

然后通过伪元素,再添加一个三角形:

http://img1.sycdn.imooc.com//climg/60fb72540904a33205690332.jpg

效果:
http://img1.sycdn.imooc.com//climg/60fb725f096c2f8502720144.jpg

然后通过旋转、偏移值等方式让它们重叠在一起:

http://img1.sycdn.imooc.com//climg/60fb72b109deea3805800448.jpg

效果:

http://img1.sycdn.imooc.com//climg/60fb72bc09b09d1002370157.jpg

添加伪元素,再添加一个三角形:

http://img1.sycdn.imooc.com//climg/60fb72e709fc7b7105650332.jpg

效果:
http://img1.sycdn.imooc.com//climg/60fb72f309e1752502710161.jpg

旋转并调整偏移值:

http://img1.sycdn.imooc.com//climg/60fb7320090d606605660426.jpg

这样一个五角星就布局完成了:

http://img1.sycdn.imooc.com//climg/60fb732c091448a702090170.jpg

可以结合如下示例代码测试理解

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.box1 {
width: 300px;
height: 200px;
background-color: red;
position: absolute;
left: 50%;
margin-left: -150px;
}
.star {
width: 0;
height: 0;
border-top: 40px solid yellow;
border-left: 65px solid transparent;
border-right: 65px solid transparent;
margin-top: 100px;
position: absolute;
}
.star::before,
.star::after {
content: "";
display: block;
width: 0;
height: 0;
border-top: 40px solid yellow;
border-left: 65px solid transparent;
border-right: 65px solid transparent;
position: absolute;
top: -40px;
left: -65px;
}
.star::before {
transform: rotate(-75deg);
}
.star::after {
transform: rotate(-146deg);
}
.star1 {
transform: scale(0.5) translate(-40px, -130px);
}
.star2 {
transform: scale(0.2) translate(120px, -520px) rotate(30deg);
}
.star3 {
transform: scale(0.2) translate(190px, -380px) rotate(30deg);
}
.star4 {
transform: scale(0.2) translate(200px, -240px) rotate(30deg);
}
.star5 {
transform: scale(0.2) translate(120px, -120px) rotate(30deg);
}
</style>
</head>
<body>
<!-- 此处写代码 -->
<div class="box1">
<div class="star star1"></div>
<div class="star star2"></div>
<div class="star star3"></div>
<div class="star star4"></div>
<div class="star star5"></div>
</div>
</body>
</html>

​具体的旋转角度和移动距离,同学也可以自己一点一点调整,让效果更美观。

祝学习愉快~

  • qq_慕仰20210716 提问者 #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>Document</title>

        <style>

          .box{

            width600px;

            height400px;

            background-colorred;

            margin40px auto;

            positionrelative;

          }

          .box .star{

            width0px;

            height0px;

            border-left65px solid transparent;

            border-right65px solid transparent;

            border-top40px solid yellow;


          }

          .box .star::before{

            content"";

            displayblock;

            width0px;

            height0px;

            border-left65px solid transparent;

            border-right65px solid transparent;

            border-top40px solid yellow;

            transformrotate(71deg);

            positionabsolute;

            left-62px;

            bottom:-3px;

          }

          .box .star::after{

            content"";

            displayblock;

            width0px;

            height0px;

            border-left65px solid transparent;

            border-right65px solid transparent;

            border-top40px solid yellow;

            transformrotate(-74deg);

            positionabsolute;

            left-63px;

            bottom-3px;

          }

          .box .star1{

            transformscale(0.7translate(10px,70px);

          }

          .box .star2{

            transformscale(0.2translate(417px,-131pxrotate(23deg);

          }

          .box .star3{

            transformscale(0.2)  translate(617px,-181pxrotate(56deg);

          }

          .box .star4{

            transformscale(0.2)  translate(617px,-193pxrotate(79deg);

          }

          .box .star5{

            transformscale(0.2)  translate(417px,-256pxrotate(92deg);

          }


          </style>

      </head>

      <body>

        <div class="box">

          <div class="star star1"></div>

          <div class="star star2"></div>

          <div class="star star3"></div>

          <div class="star star4"></div>

          <div class="star star5"></div>

        </div>

      </body>

    </html>



    2021-08-14 21:05:36
  • 同学你好,再次粘贴的代码效果实现的很棒!!!祝学习愉快~

    2021-08-15 10:21:18
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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