CSS Single Div Drawing - 形状

时间:2017-12-29 08:53:43

标签: css css3 gradient linear-gradients radial-gradients

我正在尝试使用CSS渐变和单个div进行绘制。

不幸的是,没有足够的信息,所以我需要通过阅读其他人的代码来自己学习。以下是一些例子:

https://codepen.io/PButcher/pen/JMRKpG

https://codepen.io/jkantner/pen/zdeJYg

https://codepen.io/jkantner/pen/ppjRBP

https://codepen.io/jkantner/pen/XMLVXa

到目前为止,我已经学会了如何只创建几个基本形状:

div
{
    height: 20em;
    width: 20em;
    
    background:
        linear-gradient( 0deg, #000 0%, #000 100% ) 1em 1em / 5em 5em,
        radial-gradient( circle at center, #000 70%, transparent 70% ) 1em 7em / 5em 5em,
        linear-gradient( 45deg, #000 0%, #000 50%, transparent 50% ) 1em 13em / 5em 5em,
        linear-gradient( -45deg, #000 0%, #000 50%, transparent 50% ) 7em 1em / 5em 5em,
        linear-gradient( 135deg, #000 0%, #000 50%, transparent 50% ) 7em 7em / 5em 5em,
        linear-gradient( -135deg, #000 0%, #000 50%, transparent 50% ) 7em 13em / 5em 5em,
        radial-gradient( 100% 100% at top, #000 50%, transparent 50% ) 14em 1em / 5em 5em,
        radial-gradient( 100% 100% at bottom, #000 50%, transparent 50% ) 14em 2em / 5em 5em,
        linear-gradient( 45deg, #000 0%, #000 50%, transparent 50% ) 14em 7em / 4em 7em,
        radial-gradient( 100% 50%, #000 0%, #000 50%, transparent 50% ) 14em 14em / 5em 5em;

    background-repeat: no-repeat;
}
<div></div>

但是,我想创造更复杂的形状,例如,这只鹰的喙和羽毛,或它上面的阴影。

enter image description here

可以使用CSS和单个div来实现吗?

1 个答案:

答案 0 :(得分:1)

正如我上面评论的那样,我们可以使用多种技术来实现这样的目标:

  1. radial-gradient 椭圆,并调整尺寸位置
  2. .elem {
     width:300px;
      height:200px;
      border:1px solid;
      background:
           radial-gradient(ellipse at center, red 15%,transparent 15%),
           radial-gradient(circle at 40px 120px, #ffc20f 20%,transparent 20%),
           radial-gradient(circle at right bottom, #000 25%,transparent 25%);
    }
    <div class="elem"></div>

    1. Linear-gradient我们旋转并调整尺寸位置
    2. .elem {
        width:300px;
        height:200px;
        border:1px solid;
        background:
             linear-gradient(20deg,transparent 20%,#ffc20f 20%,#ffc20f 30%,transparent 30%) no-repeat 98px -50px / 51px 151px,
             linear-gradient(-20deg,transparent 20%,#ffc20f 20%,#ffc20f 30%,transparent 30%) no-repeat 98px -50px /51px 151px,
             linear-gradient(30deg,red 20%,transparent 20%) no-repeat 10px 30px / 50px 50px,
             linear-gradient(to right,blue 20%,transparent 20%) no-repeat 70px 20px / 50px 60px,
             linear-gradient(40deg,#000 20%,transparent 20%) no-repeat 200px 20px / 15px 100px,
             linear-gradient(-40deg,#000 20%,transparent 20%) no-repeat 200px 20px / 15px 100px;
                    
      
      }
      <div class="elem" ></div>

      1. 还有clip-path可以帮助您剪切元素并调整整体形状。这也将允许您将元素置于任何背景之上。 (但所有浏览器的此功能均为not yet supported):
      2. body {
          background: #f2f2f5;
        }
        
        .elem {
          background: #000;
          width: 300px;
          height: 200px;
          -webkit-clip-path: polygon(3% 39%, 13% 21%, 26% 13%, 47% 14%, 69% 21%, 83% 25%, 90% 33%, 72% 37%, 87% 45%, 62% 49%, 87% 58%, 72% 61%, 58% 62%, 42% 62%, 29% 59%, 17% 64%, 18% 50%, 21% 42%, 10% 48%, 5% 59%);
          clip-path: polygon(3% 39%, 13% 21%, 26% 13%, 47% 14%, 69% 21%, 83% 25%, 90% 33%, 72% 37%, 87% 45%, 62% 49%, 87% 58%, 72% 61%, 58% 62%, 42% 62%, 29% 59%, 17% 64%, 18% 50%, 21% 42%, 10% 48%, 5% 59%);
        }
        <div class="elem"></div>

        1. 你也可以使用伪元素,你可以添加最多2个新元素,你可以在上面添加3种技术,还可以添加其他CSS属性,如box-shadow,opacity,filter,transform等。允许你更容易地划分形状。
        2. 然后,主要技巧是将它们彼此叠加以获得最终形状。

          这是一个丑陋的开始,表明你可以将设计切割成不同的部分并计算不同的尺寸/位置:

          body {
            background: red;
          }
          
          .eagle {
            width: 300px;
            height: 200px;
            position: relative;
            background: radial-gradient(circle at 100px 70px, #000 2.5%, transparent 2.5%), linear-gradient(10deg, transparent 20%, #000 20%, #000 29%, transparent 30%) no-repeat 98px -8px / 142px 86px, linear-gradient(6deg, transparent 20%, #ffc20f 20%, #ffc20f 35%, transparent 35%) no-repeat 98px -50px / 141px 168px, radial-gradient(circle at 150px 100px, #000 15%, transparent 15%), radial-gradient(circle at 110px 130px, #000 15%, transparent 15%), radial-gradient(circle at 100px 100px, #ffc20f 20%, transparent 20%), radial-gradient(circle at 100px 100px, #000 25%, transparent 25%), #ffffff;
            clip-path: polygon(18% 35%, 23% 27%, 43% 14%, 64% 10%, 81% 27%, 79% 41%, 62% 66%, 48% 74%, 63% 49%, 46% 47%, 43% 53%, 33% 53%, 34% 52%, 29% 64%, 31% 77%, 20% 69%, 20% 69%, 17% 62%, 15% 52%, 16% 41%);
            ;
          }
          
          .eagle:before {
            content: "";
            position: absolute;
            top: 17%;
            left: 60%;
            height: 22px;
            width: 30px;
            border-radius: 50%;
            background-image: radial-gradient(circle at center, #000 29%, #ffc20f 20%);
          }
          
          .eagle:after {
            content: "";
            position: absolute;
            top: 36%;
            left: 58%;
            height: 84px;
            width: 27px;
            transform: rotate(45deg) skewY(-61deg);
            background-image: linear-gradient(to right, #000 29%, #ffc20f 20%, #ffc20f 80%, #000 80%);
          }
          <div class="eagle"></div>