Home  >  Article  >  Web Front-end  >  CSS drawing based on a single div_html/css_WEB-ITnose

CSS drawing based on a single div_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:56:29994browse

Original text: Single Div Drawings with CSS

Translation: CSS drawing based on a single div

Translator: Front-end Foreign Journal Comments

Translation annotation: Read this article in full , strongly felt the combination of technology and art! I like this sentence from the author: Restricting your available options forces you to re-evaluate the tools you already have. Restricting your available options forces you to re-evaluate the tools you already have.

Why only use one Div?

In May 2013, I attended CSSConf and saw Lea Verou’s speech about border-radius. You may think that this property is very inconspicuous. But this talk opened my eyes to the fact that there is a lot more behavior in CSS that I didn’t understand. Reminiscing about my time as an art student continues to push me to become an expert in my chosen medium. As a web designer, CSS is my medium, so I learn everything I can and explore its limits.

Why only one Div?

I recall that when I was learning painting, I also did experiments on mixing colors in class. We used the three primary colors, red, yellow, and blue, to create a spectrum of other colors. The purpose of this experiment is to teach us about the properties of color, but this limitation also teaches us about the power of mixing. Of course you can buy a green pen, but you can also use blue and yellow to make green. Limiting your options will make you re-evaluate the tools you already have at hand.

I decided to start a project using CSS drawing, and after some time I will come up with a new thing drawn using only CSS. In order to get a greater challenge and explore the potential of CSS, I set the limit for myself and just used a Div. Instead of just buying a green pen (to add more Divs), all I have to do is combine CSS properties as best I can to achieve my purpose.

Toolbox

A Div plus those CSS properties supported by the browser, it seems that there are too few tools available. But I’ve found that the problem isn’t how much you’re using, but how you think about what you’re using.

Pseudo-Elements

Because CSS has pseudo-classes, even though there is only one Div, I can actually use three elements. Therefore, using div, div:before, div:after, we can do this:

div { background: red; }div:before { background: yellow; }div:after { background: blue; }

It is easy to imagine that these three elements can be placed side by side into three stacked layers. So, in my head, it looks like this:

Shapes

Using CSS and a single element, there are three basic shapes we can make. Use width and height properties to make squares/rectangles, border-radius to make circles/ellipses, and border to make triangles/trapezoids.

We can also use CSS to create other graphics, but most of them can be achieved by simply combining these basic graphics. These simple graphics are the easiest to make and the easiest to modify.

Multiple identical shapes

Using overlaid box-shadow, we can create multiple identical shapes with different sizes, colors and blur effects. We can move these shapes on the x or y axis, so almost infinite shapes can be drawn.

div {    box-shadow: 170px 0 10px yellow,                330px 0 0 -20px blue,                330px 5px 5px -20px black;}

We can even add box-shadow to box-shadow . Note the order in which they are declared. Again, it's easier to understand them as layers.

Gradient

Gradient can be used to create light and shade effects by giving a light source, which can make simple flat graphics look more realistic. Combining multiple background-images, we can use many layers of gradients to achieve more complex light and shadow, and even more graphics.

div {    background-image: linear-gradient(to right, gray, white, gray, black);}div:after {    background-image: radial-gradient(circle, yellow 50%, transparent 50%), linear-gradient(to right, blue, red); }

Vision

The hardest part is vision, how to piece these shapes together into a perceptible drawing. As I became more and more focused on the craft of drawing, the visual discovery step was important. To do this, I often stare at the thematic image and cut it into visible parts. They are all shapes and colors. I reduced the entire image to a few small colored shapes or blocks, and I knew (roughly) how to implement them using CSS.

Example

Let’s take a closer look at two drawings and learn how to break them into different blocks to combine into one large shape. The first one is a green crayon.

A crayon consists of two basic shapes: a rectangular body and a triangular tip.

I had to implement the following points to capture the feel of real crayons:

  • Different colors on the paper packaging
  • Shapes printed on the packaging and text
  • stripes to suggest that the crayon is round
  • light and dark effects to suggest round crayons and light sources
  • First, I made the body part of the crayon using div and background colors, Gradient from top to bottom and use a box-shadow to suggest a three-dimensional feel:

    div {    background: #237449;    background-image: linear-gradient(to bottom,                                  transparent 62%,                                  black(.3) 100%);    box-shadow: 2px 2px 3px black(.3);}

    Then, I used a linear-gradient from left to right to create the paper wrapper. The alpha value is .6 so that the previous gradient shows through.

    div {    background-image: linear-gradient(to right,                                  transparent 12px,                                  rgba(41,237,133,.6) 12px,                                  rgba(41,237,133,.6) 235px,                                  transparent 235px);}

    接下来,我继续使用同样的方式,从左到右渐变,制作蜡笔上的条纹。

    div {    background-image: linear-gradient(to right,                                  transparent 25px,                                  black(.6) 25px,                                  black(.6) 30px,                                  transparent 30px,                                  transparent 35px,                                  black(.6) 35px,                                  black(.6) 40px,                                  transparent 40px,                                  transparent 210px,                                  black(.6) 210px,                                  black(.6) 215px,                                  transparent 215px,                                  transparent 220px,                                  black(.6) 220px,                                  black(.6) 225px,                                  transparent 225px);}

    纸包装上印刷的椭圆,使用一个 radial-gradient 轻松搞定!

    div {    background-image: radial-gradient(ellipse at top,                                  black(.6) 50px,                                  transparent 54px);}

    我刚才单独展示了各个部分,不过别忘了 background-image 看起来是这样的:

    div {                      // ellipse printed on wrapper    background-image: radial-gradient(ellipse at top,                                  black(.6) 50px,                                  transparent 54px),                      // printed stripes                      linear-gradient(to right,                                  transparent 25px,                                  black(.6) 25px,                                  black(.6) 30px,                                  transparent 30px,                                  transparent 35px,                                  black(.6) 35px,                                  black(.6) 40px,                                  transparent 40px,                                  transparent 210px,                                  black(.6) 210px,                                  black(.6) 215px,                                  transparent 215px,                                  transparent 220px,                                  black(.6) 220px,                                  black(.6) 225px,                                  transparent 225px),                      // wrapper                      linear-gradient(to right,                                  transparent 12px,                                  rgba(41,237,133,.6) 12px,                                  rgba(41,237,133,.6) 235px,                                  transparent 235px),                      // crayon body shading                      linear-gradient(to bottom,                                  transparent 62%,                                  black(.3) 100%)}

    完成了 div,我们把注意力转移到 :before 伪类元素上,创建蜡笔的笔头。使用实心和透明的边框,我制作了一个三角形,把它和我之前绘制的 div 放到一起。

    div:before {    height: 10px;    border-right: 48px solid #237449;    border-bottom: 13px solid transparent;    border-top: 13px solid transparent;}

    比起蜡笔笔杆,笔头看起来有点平,我们可以使用 :after 伪类元素来修复这个问题。我添加一个从顶部到底部的 linear-gradient,制作了一个反光效果,贯穿整只蜡笔。

    div:after {    background-image: linear-gradient(to bottom,                                    white(0) 12px,                                    white(.2) 17px,                                    white(.2) 19px,                                    white(0) 24px);}

    这给那个扁平的三角形添加更多的层次感,更加真实。制作接近尾声,我给 :after 添加一些文字,定位,使得看起来像是印刷在蜡笔包装上的一样。

    div:after {    content: 'green';    font-family: Arial, sans-serif;    font-size: 12px;    font-weight: bold;    color: black(.3);    text-align: right;    padding-right: 47px;    padding-top: 17px;}

    大功告成!

    另外一个实例

    蜡笔作为一个不错的例子,很好地展示了如何使用 background-image 和 gradient 来产生真实的效果。下面这个例子将展示多个 box-shadow 的强大之处:单 div 的照相机。

    这是照相机的主体部分,使用 background-image 和 border-image 制作的。

    下面是一张 gif,展示 :before 伪类元素(黑色的那个矩形),以及使用它的 box-shadow 创建出来的很多照相机的细节部分。

    div:before {    background: #333;    box-shadow: 0 0 0 2px #eee,                -1px -1px 1px 3px #333,                -95px 6px 0 0 #ccc,                30px 3px 0 12px #ccc,                -18px 37px 0 46px #ccc,                -96px -6px 0 -6px #555,                -96px -9px 0 -6px #ddd,                -155px -10px 1px 3px #888,                -165px -10px 1px 3px #999,                -170px -10px 1px 3px #666,                -162px -8px 0 5px #555,                85px -4px 1px -3px #ccc,                79px -4px 1px -3px #888,                82px 1px 0 -4px #555;}

    类似的,下面是 :after(灰色的圆)以及使用它的 box-shadow 制作的几个细节部分。

    div:after {    background: linear-gradient(45deg, #ccc 40%, #ddd 100%);    border-radius: 50%;    box-shadow: 0 3px 2px #999,                1px -2px 0 white,                -1px -3px 2px #555,                0 0 0 15px #c2c2c2,                0 -2px 0 15px white,                -2px -5px 1px 17px #666,                0 10px 10px 15px black(.3),                -90px -51px 1px -43px #aaa,                -90px -50px 1px -40px #888,                -90px -51px 0 -34px #ccc,                -90px -50px 0 -30px #aaa,                -90px -48px 1px -28px black(.2),                -124px -73px 1px -48px #eee,                -125px -72px 0 -46px #666,                -85px -73px 1px -48px #eee,                -86px -72px 0 -46px #666,                42px -82px 1px -48px #eee,                41px -81px 0 -46px #777,                67px -73px 1px -48px #eee,                66px -72px 0 -46px #666,                -46px -86px 1px -45px #444,                -44px -87px 0 -38px #333,                -44px -86px 0 -37px #ccc,                -44px -85px 0 -34px #999,                14px -89px 1px -48px #eee,                12px -84px 1px -48px #999,                23px -85px 0 -47px #444,                23px -87px 0 -46px #888;}

    有点疯狂?不过你看到了吧, 多个 box-shadow 确实可以给使用单个 div 绘图添加很多细节部分。

    最大的挑战

    我碰到了两个最大的挑战,三角形的限制和 gradient 独特的行为。

    三角形的问题

    因为三角形是使用 border 创建的,这极大地限制了我对它的利用。使用 border-image 给 border 添加gradient,不能单独添加其中一边。无法给 border 创建出来的三角形添加 box-shadow,因为 box-shadow 是添加在盒模型上的。因此要创建多个三角形就会很困难。看起来就是下面这样:

    div {    border-left: 80px solid transparent;    border-right: 80px solid transparent;    border-bottom: 80px solid red;}div:before {    border-left: 80px solid transparent;    border-right: 80px solid transparent;    border-bottom: 80px solid red;    border-image: linear-gradient(to right, red, blue);}div:after {    border-left: 80px solid transparent;    border-right: 80px solid transparent;    border-bottom: 80px solid red;    box-shadow: 5px 5px 5px gray;}

    多层渐变

    渐变的行为就是会填满整个 background。在堆叠多个 gradient 的时候就显得很讲技巧。需要花费额外的时间思考透明度、z-index这些事,还要搞清楚什么要可见,什么不要。不过若能有效地使用 gradient,我们的绘图可以包含很多令人惊叹的细节。

    Tardis 就是一个很好的例子,显示或隐藏渐变,创建了一张细节极强的图片。下图显示的是绘制的中间过程,可以看到数个从顶部到底部的渐变,宽度填满整个容器。

    使用从左到右和从右到左的 gradient,我可以遮住一部分渐变,同时把其他部分渐变显示出来。

    最终的结果看上去包含了很多图形来构成 Tardis 的前面,但实际上它就是层叠的 linear-gradient。很多时候不得不伪造呀。

    动态地查看它们

    源于这个项目,有一个非常酷非常有用的好东西突然出现,那就是 Rafael Carício(@rafaelcaricio) 开发的名为CSS Gradient Inspector 的 Chrome 浏览器插件。这个开发工具可以探测且可以开关元素上的每一个 gradient,看起来就像开关一个个层。(它在日常项目中也非常有用。)

    我希望设计师和开发者使用动画或者 JavaScript 的功能来做类似的尝试,或者对这些绘画做一些变形。你可以到http://div.justjavac.com 或者 GitHub 上把玩一下这些 CSS。

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn