• CSS画出的图


    简单图形

    矩形

    div{
         100px;
        height: 100px;
        background-color: red;
    }

    圆形

    div{
         100px;
        height: 100px;
        background-color: red;
        border-radius: 50%;
    }

    椭圆

    【整个椭圆】

    div{
         100px;
        height: 50px;
        background-color: red;
        border-radius: 50%;
    }

    【半椭圆或半圆】

    div{
       100px;
      height: 100px;
      background:red;
      border-radius:50% /100% 100% 0 0 ;
    }

    【四分之一椭圆】

    div{
       100px;
      height: 100px;
      background:red;
      border-radius:100% 0 0 0 ;
    }

    三角形

    【直角三角形】

    div{
         0;
        height: 0;
        border: 50px solid transparent;
        border-bottom-color: red;
    }

    【正三角形】

    div{
         0;
        height: 0;
        border: 50px solid transparent;
        border- 86.6px 50px;
        border-bottom-color: red;
    }

    平行四边形

    【基本图形】

    div{
        margin-left: 50px;
         100px;
        height: 100px;
        background-color: red;
        transform: skew(30deg);
    }

    【改进版本】

      上面的代码中,不仅形状发生了变形,内容也发生了变形。有两种方法改进

      1、元素嵌套

    .outer{
      margin-left:20px;
       100px;
      height: 100px;
      background:red;
      transform:skewX(-30deg);
    }  
    .inner{
       transform:skewX(30deg);
    }
    </style>
    <div class="outer">
      <div class="inner">小火柴</div>
    </div>

      2、伪元素

      把所有样式都应用到伪元素上,然后再对伪元素进行变形

    div{
      position:relative; 
      margin-left:20px;
       100px;
      height: 100px;
      text-indent: 30px; 
    }  
    div::before{
      content:'';
      position:absolute;
      top: 0;right: 0;left: 0;bottom: 0;
      transform:skewX(-30deg);
      background:red; 
      z-index:-1;
    }

    梯形

    【基本版本】

    div{
         50px;
        border: 50px solid transparent;
        border-bottom-color: red;
    }

    【增强版本】

      上面的代码虽然简单且巧妙,但无法在图形内容填充文本。下面是比较复杂的增强版本 

    div{
      height: 100px;
       200px;
      line-height: 100px;
      position:relative;
      display:inline-block;
      text-align: center;
      color:white;
      padding: .5em 1em .35em;
    }
    div:before{
      content:'';
      position:absolute;
      top: 0;right: 0;bottom: 0;left: 0;
      z-index:-1;
      background:#58a;
      transform:scaleY(1.3) perspective(.5em) rotateX(5deg);
      transform-origin: bottom;
    }

    【梯形选项卡】

    <style>
    a{
      width: 120px;
      text-align: center;
      text-decoration: none;
      color:inherit;
      font-size: 20px
    }
    nav > a{
      position:relative;
      display:inline-block;
      padding:.3em .1em 0;
    }
    nav > a:before{
      content:'';
      position:absolute;
      top: 0;right: 0;left: 0;bottom: 0;
      z-index:-1;
      background:#ccc;
      border:1px solid black;
      border-radius: .5em .5em 0 0 ;
      box-shadow: 0 .15em white inset;
    
      transform:scaleY(1.3) perspective(.5em) rotateX(5deg);
      transform-origin: bottom;
    }
    </style>
    <nav>
      <a href="#">HTML</a>
      <a href="#">CSS</a>
      <a href="#">Javascript</a>
    </nav>

    复杂图形

    多角星

    【五角星】

      一个大三角形底部掏掉一个小三角形;两个前面的相同图形叠压

      经计算,五角星的夹角为36度,若小三角形的侧边为a,则大三角形的侧边为2a(1+sin18),两个三角形共同的底边为2a(sin54)

    <div class="box">
      <div class="in"></div>
      <div class="in"></div>
    </div>

    .box{
        position: relative;
    }
    .in{ 
        margin-left: 100px;
        position: relative; 
         0px; 
        border: 10px solid transparent; 
        border- 249px 81px;
        border-bottom-color: red; 
        position: absolute;
    } 
    .in:after{
        content: "";
        position:absolute;
        border: 10px solid transparent;
        border- 59px 81px;
        border-bottom-color: white;
        top: 133px;
        left: -81px;
    }
    .in:last-child{
        top: 100px;
        left: -134px;
        transform: rotate(-73deg);
    }

    【六角星】

      两个三角形叠压

    div{
        position: relative;
         0;
        border: 50px solid transparent;
        border- 50px 43.4px;
        border-bottom-color: red;
    }
    div:after{
        position: absolute;
        content:"";
         0;
        border: 50px solid transparent;
        border- 50px 43.4px;
        border-top-color: red;
        top: 16px;
        left: -42px;
    }

    多边形

    【六边形】

      两个梯形拼接

    div{
        position: relative;
         50px;
        border: 50px solid transparent;
        border-bottom-color: red;
    }
    div:after{
        position: absolute;
        content:"";
         50px;
        border: 50px solid transparent;
        border-top-color: red;    
        top:50px;
        left: -50px;
    }

    【八边形】

    .outer{
      100px;
      height: 100px;
      transform:rotate(45deg);
      overflow: hidden;
    }  
    .inner{
      100%;
      height: 100%;
      transform:rotate(-45deg);
      background:red;
    }

    【菱形】

    <style>
    .outer{
      margin:50px 0 0 50px;
      width:100px;
      height: 100px;
      transform:rotate(45deg);
      overflow: hidden;
    }  
    .inner{
      width:100%;
      height: 100%;
      transform:rotate(-45deg) scale(1.42);
      background:red;
    }
    </style>

    桃心

      创建一个方形div,分别用css控制div的两个伪元素平移到正方形相邻两边,圆形与边中点重合。最后将总的div旋转45度

    <style>
    div{
        display:inline-block;
        margin:50px;
        height: 100px;
        width: 100px;
        background-color: red;
        transform: rotate(-45deg);
    }
    div:before,div:after{
        display:block;
        content:"";
        width: 100px;
        height: 100px;
        background-color: red;
        border-radius: 50%;
        margin-top:-50%;
    }
    div:after{
        margin-left:50%;
    }
    </style>
    <div></div>

    切角效果

    【单角】

    background: linear-gradient(-45deg,transparent 5%,#58a 0);

    【双角】

      background: linear-gradient(-45deg,transparent 5%,#58a 0) right,linear-gradient(45deg,transparent 5%,#58a 0) left;
      background-size:50% 100%;
      background-repeat:no-repeat;

    【四角】

      background: linear-gradient(-135deg,transparent 5%,#58a 0) top right,linear-gradient(135deg,transparent 5%,#58a 0) top left,linear-gradient(-45deg,transparent 5%,#58a 0) bottom right,linear-gradient(45deg,transparent 5%,#58a 0) bottom left;
      background-size:50% 50%;
      background-repeat:no-repeat;

    【弧形切角】

      background: radial-gradient(circle at top right,transparent 5%,#58a 0) top right,radial-gradient(circle at top left,transparent 5%,#58a 0) top left,radial-gradient(circle at bottom right,transparent 5%,#58a 0) bottom right,radial-gradient(circle at bottom left,transparent 5%,#58a 0) bottom left;
      background-size:50% 50%;
      background-repeat:no-repeat;

    折角效果

    background:linear-gradient(to left bottom,transparent 50%,rgba(0,0,0,0.4) 0) no-repeat 100% 0 /2em 2em,linear-gradient(-135deg,transparent 1.414em ,#58a 0);

    饼图效果

      饼图在网页中的运用极为普遍,比如简单的统计图表、进度指示器、定时器等

    【静态效果】

    <style>
    @keyframes spin{
      to{transform: rotate(.5turn);}
    }
    @keyframes bg{50%{background:#655;}}
    .pie{
      width: 100px;line-height: 100px;
      position:relative;
      background-color: yellowgreen;
      border-radius: 50%;
      color:transparent;
      text-align:center;
      background-image: linear-gradient(to right, transparent 50%,#655 0);
    }
    .pie:before{
      content:'';
      position:absolute;
      top: 0;left: 50%;
      width: 50%;height: 100%;
      border-radius: 0 100% 100% 0 /50%;
      background-color: inherit;
      transform-origin: left;
      animation: spin 50s linear infinite,bg 100s step-end infinite;
      animation-play-state:paused;
      animation-delay:inherit;
    }
    </style>
    <div class="pie" style="animation-delay:-20s">20%</div>
    <div class="pie" style="animation-delay:-90s">90%</div>

    【动态效果】

    <style>
    @keyframes spin{
      to{transform: rotate(.5turn);}
    }
    @keyframes bg{50%{background:#655;}}
    
    .pie{
      width: 100px;height: 100px;
      position:relative;
      background-color: yellowgreen;
      border-radius: 50%;
      background-image: linear-gradient(to right, transparent 50%,#655 0);
    }
    .pie:before{
      content:'';
      position:absolute;
      top: 0;left: 50%;
      width: 50%;height: 100%;
      border-radius: 0 100% 100% 0 /50%;
      background-color: inherit;
      transform-origin: left;
      animation: spin 3s linear infinite,bg 6s step-end infinite;
      animation-play-state: paused;
    }
    .pie:hover:before{
      animation-play-state: running;
    }
    </style>
    <div class="pie"></div>

      鼠标移入时,饼图发生移动

    【SVG实现】

       让圆形的周长接近于100,方便计算。r = 100/(2*PI) = 16

    <style>
    @keyframes fillup{to{stroke-dasharray:100 100;}}
    svg{
      width: 100px;
      height: 100px;
      transform: rotate(-90deg);
      background: yellowgreen;
      border-radius: 50%;
    }
    circle{
      fill:yellowgreen;
      stroke:#655;
      stroke-width:32;
      stroke-dasharray: 0 100;
      animation: fillup 5s linear infinite;
    }
    </style>
    <svg viewbox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
      <circle r=16 cx=16 cy=16></circle>
    </svg>

    最后

      除了使用CSS画图之后,实现各种形状更简单的方法是使用clip-path路径裁剪样式

      欢迎交流

  • 相关阅读:
    内存访问可以使用段地址+冒号+偏移地址的形式吗?
    更改Word、PowerPoint、Excel(Office套件)的主题为暗黑色
    埃拉托斯特尼筛法找质数
    引水入城
    线段树 区间乘
    会场预约
    借教室 线段树and二分
    数轴染色
    线段树 区间加
    树集
  • 原文地址:https://www.cnblogs.com/xiaohuochai/p/5028101.html
Copyright © 2020-2023  润新知