My css illustarion

综合技术 2017-11-09

Have you heard of the css daily challenge ? I had signed up for it a while ago, but never actually took it. Instead, it inspired me to challenge myself to recreate an illustration using only css. It is this challenge I want to share with you in this post.

When I design, I often use freepik.com as my images resource. Therefore it was only natural that I search there for a set of illustrations to create in css. It was a week before my family vacation, and so it was no coincidence that I picked this image:

I started with what seemed to be the simplest object, the glasses. In retrospect and after working on several other objects, I realized that there were simpler objects that I could have started with, such as the suitcase, but the glasses were certainly a nice start. While recreating the illustrations, I had a few guidelines: First of all, I tried to use as few div s as possible and to use pseudo-elements like :before and :after instead. Secondly, I tried to target responsiveness but gave it up along the way: At first I tried using dynamic sizes (usually percentages), however somewhere along the way it got too tedious, and since I had no vision (or need) for resizing, I decided to let go. My work was only for “playing in the sandbox”, so pixels weren’t a dirty word. I was encountering enough challenges as it was, so I preferred concentrating on them rather than creating responsive illustrations. Maybe one day I'll go back and make my illustration responsive.

Challenges encountered ...

The First Challenge - css variables

After easily picking my desired illustration set and starting writing css, it was clear to me that I was going to use this opportunity to experiment with css variables , hence I declare them as my first challenge. In case you’re not familiar with css variables you can read or watch the following interesting lectures:

I don’t think I got even close to utilizing the full power of css variables , but for a first time use it was good enough for me. The illustration I’d picked had only several colors in it, and that suited me perfectly in using variables for color names - it’s so much easier to remember ‘pink’ and ‘dark pink’ rather than their hexadecimal code. It came in the most handy when creating gradients or complex box-shadows, where the color values are sometimes repeated:

//VARIABLES
:root {
 --very-dark-pink: #b2184e;
 --dark-pink: #ca325b;
 --pink: #e55171;
 --orange: #f68f19;
 --yellow: #fcbb21;
 --dark-blue: #139da7;
}
//STYLE
background-image: linear-gradient(
 to bottom,
 var(--white) 50%,
 var(--dark-pink) 50%,
 var(--dark-pink) 65%,
 var(--pink) 65%,
 var(--pink) 100%
 );

While writing my scss I found a very handy aspect of css variables - the ability to declare a new value for a variable within a css class. I’ll illustrate with an example: I defined the --size variable under the :root selector, giving it the value of 1px. Now, many of my elements were squares - that is, their width and height were the same. In order to avoid repeating the same value of pixels twice (when defining their width, and when defining their height) and hence needing to make a change in two places if the sizes change, I set the height and width values as --size . So far no news here. However, when a certain element needed a different size (but was still square), I changed the --size variable within that element selector , and the new value applied only to that selector. If none of this makes sense yet, I hope this will surely clarify it:

//VARIABLES
:root {
--size: 1px;
}
/*STYLE*/
.lens:before {
....
 --size: 12px;
 width: var(--size);
 height: var(--size);
}

The Second Challenge - Shapes

In my day to day work I don’t usually encounter the need to create shapes using scss. Yes, I do often set border-radius: 50%; in order to create a circle (especially for images), or use borders to create different kinds of triangles, but that’s more or less it. In the illustration set, however, there was hardly any illustration that was a simple square. I used the border-radius a lot, sometimes using percentage values and sometimes fixed pixels. In some shapes I defined the same value of border-radius for all of the corners, and in others I defined a different value for each corner. I had totally forgotten how rich border-radius could be - this sent me back in time to three years ago, when I sat, fascinated, watching a youtube video of The Humble Border-Radius by Lea Verou . I strongly recommend watching that video. Even though it’s been a few years, everything there is still relevant (except, perhaps ,for the perifix 🙂

I also created many triangles using a CSS triangle generator , and created other shapes as well, such as the trapezoid. I have learned, for example, that a trapezoid can be created either by using triangles - you can read about it here - or by using transform: perspective() rotateX(); . You can read the explanation here .

In some complex shapes, like the plane on the pink flight ticket or the globe icon, I ditched css and used inline svg.

The Third Challenge - use as few divs as possible

Why? To be honest, there was no practical reason: I already created something that was obviously inaccessible and lacked any semantic significance, so why should I be ‘div-stingy’? The answer, I think, is that it's inherent in me to refrain from adding html elements that only serve the design and have no semantic meaning. I love using :before and :after anyway, and the challenge in creating as few div s as possible was fun! Some of the div s I had added were necessary because of the capacity of elements I needed to create, and some of the div s were utterly unnecessary, but they solved a z-index problem I encountered. Funny, I'd thought I'd had a fairly good grasp of z-index , but apparently I was wrong. If I fully understood what was going on there, I would have written a nice explanation here, but unfortunately I don’t. Those of you who are interested can read this excellent post about z-index .

Another trick I used to simulate several elements using only one selector, was to define a multiple-box-shadow. Box-shadow gets the width and / or height of the element (depending on where it is shifted to), which means that if, for example, my element is 20 pixels wide and I shift the box-shadow by 20 pixels on the X axis (to the right or to the left), I will get a duplication of the element. Using this method, I created the stripes on the pink ticket.

.flying-ticket:before {
 width: 20px;
 height: 3px;
 color: var(--dark-pink);
 box-shadow: inset 0 4px 0 0, 0 5px 0 0, 
                   20px 5px 0 0, 0 10px 0 0, 
                   20px 10px 0 0, 63px 0px 0 0, 
                   63px 3px 0 0, 63px 6px 0 0, 
                   63px 9px 0 0;
}

Another way of avoiding adding html elements when creating stripes is to use a linear-gradient . There are two ways to do so: The first is by setting a repetitive gradient (can be seen used in the top of the compass) and the second is by using a background-size setting to define the sizes of each color of the gradient.

/*first option (used to make the vertical lines in the top part of the compass):*/
background-image: repeating-linear-gradient( to left,
                                             transparent, transparent 5px, 
                                             var(--very-dark-blue) 7px, var(--very-dark-blue) 8px);

/*second option (used to make the dashed lines in the airline ticket):*/
 background-image: linear-gradient( to bottom, var(--dark-pink) 50%, transparent 50% );
 background-size: 100% 8px;

External Resources

Throughout my work on the illustrations, I used css generators to create triangles, gradients and more, and I Googled a lot. Many of the solutions were found thanks to questions and answers on StackOverflow (for example, in my quest for a way to create the shine on the lens in the camera, I came across code to create a smile ). Various codepens helped me, such as this pen to create a hot-balloon . I obviously always first tried to cope with each challenge on my own, but whenever I exhausted my creativity and succumbed to search, the Internet never disappointed me.

To Summarize

To be honest, there really is no need for all these scss-illustrations. You may raise your eyebrows in wonder and ask - then why did you make them? Well, there are several reasons:

First- I had fun, and it's important to do things for fun!

Second- is was challenging, and I like css challenges.

Third- even though there is no "useful" purpose for what I did, I believe that it’s important to practice css. It’s important for me to experiment and test new properties that are not always used and not necessarily supported in every browser. I enjoyed using css variables, filters, transforms and other features.

Fourth and last- hey, I got a post out of it, so why not? 🙂

It's really fun to stop for a minute (or rather, for a few hours every day for a few days) and do something for fun, for practice, to challenge the mind with new things. I'm sure it improved my ability to write code. I warmly recommend it to you all 🙂

If you’re a Hebrew speaker- you can read the whole post here .

I thank Lea Cohen for refining my english in the post.

您可能感兴趣的

CSS样式表 CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素。 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染)。 CSS语法 CSS实例 每个CSS样式由两个部分组成:选择器和声明。声明又包括属性和属...
可扩展、模块化CSS–应用性深度(翻译文)... SMACSS is becoming one of the most useful contributions to front-end discussions in years 当我们学习到 css 的内在运作时,我们知道是通过选择器来选择html的元素的。随着 css...
Gleec Chat by Immersive Garden wins August Site of... Gleec contacted us to create the brand identity and graphic universe around its communication on the applicationGleec Chat. The client gaveImmersive ...
Openlayers3中如何优雅的表示等值面 概述: 等值面,顾名思义,就是值相等的面,在水文或气象中会有很多这样的需求。本文不讲如何做等值面,本文将如何展示等值面。 效果: 栅格表达 矢量表达 实现: 等值面的实现,无外乎两种:矢量和栅格。栅格是将数据做成png等位图的格式,矢量是将数据做成json...
Parsing the Hard Way Hello again and sorry for the long downtime! My current Emacs project is an EPUB reader, something that requires parsing XML and validating/extractin...
CodePen

责编内容来自:CodePen (本文源链)

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。