DEV Community

Cover image for Dive into the art of CSS drawing
Carmen Ansio
Carmen Ansio

Posted on

Dive into the art of CSS drawing

CSS, or Cascading Style Sheets, isn't just about giving your web pages a neat appearance. It's a robust language that lets you unleash your creativity far beyond mere layout and design. Imagine crafting stunning drawings and illustrations using nothing but CSS! In this piece, we're going to delve into the exciting journey of learning CSS drawing by actually creating art with CSS.


Embarking on the journey

Before we immerse ourselves in the artistic side of CSS, it's crucial to have a solid grasp of its basics. If CSS still feels new to you, I'd suggest starting with some beginner tutorials or courses. Get comfortable with its syntax and fundamental concepts – it's going to be your canvas and paintbrush for your digital art!

 

Mastering CSS positioning

At the heart of CSS drawing lies the mastery of CSS positioning. CSS offers a variety of positioning properties, letting you meticulously place elements on your web page. The most frequently used ones include position: static, relative, absolute, and fixed.

 

Static positioning

In static positioning, elements follow the normal flow of the document. They don't react to top, bottom, left, or right properties.

<div class="box"></div>
Enter fullscreen mode Exit fullscreen mode
.box {
  position: static;
}
Enter fullscreen mode Exit fullscreen mode

 

Relative positioning

Relative positioning nudges an element from its normal spot. You can use top, bottom, left, and right properties to adjust its position.

<div class="box"></div>
Enter fullscreen mode Exit fullscreen mode
.box {
  position: relative;
  top: 20px;
  left: 50px;
}
Enter fullscreen mode Exit fullscreen mode

 

Absolute positioning

With absolute positioning, an element is placed relative to its nearest positioned ancestor. Without such an ancestor, it's positioned relative to the initial containing block, often the viewport.

<div class="box"></div>
Enter fullscreen mode Exit fullscreen mode
.box {
  position: absolute;
  top: 100px;
  left: 200px;
}
Enter fullscreen mode Exit fullscreen mode

 

Fixed positioning

Fixed positioning is akin to absolute positioning, but the element is fixed relative to the viewport. It remains in place even when the page is scrolled.

<div class="box"></div>
Enter fullscreen mode Exit fullscreen mode
.box {
  position: fixed;
  top: 50px;
  right: 0;
}
Enter fullscreen mode Exit fullscreen mode

 

The art of CSS drawing

With a good grasp of CSS positioning, we're ready to start our artistic journey. CSS drawing is all about creatively using properties and values to craft shapes, patterns, and illustrations.

 

Crafting basic shapes

CSS lets you create basic shapes like squares, circles, and triangles. For instance, a circle can be made using the border-radius property:

<div class="circle"></div>
Enter fullscreen mode Exit fullscreen mode
.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: red;
}
Enter fullscreen mode Exit fullscreen mode

 

Designing complex illustrations

For more intricate illustrations, combine various CSS properties and values. Use transform to rotate and scale elements, and box-shadow to add depth:

<div class="illustration"></div>
Enter fullscreen mode Exit fullscreen mode
.illustration {
  width: 200px;
  height: 200px;
  background-color: blue;
  transform: rotate(45deg) scale(1.5);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
Enter fullscreen mode Exit fullscreen mode

Codepen screenshots with the illustration example

 

Animating your CSS creations

What's more thrilling than bringing your CSS drawings to life? CSS animations let you animate properties over time, adding dynamism to your art. Use the @keyframes rule for defining animations and the animation property to apply them:

<div class="spinner"></div>
Enter fullscreen mode Exit fullscreen mode
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.spinner {
  width: 100px;
  height: 100px;
  border: 5px solid black;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
Enter fullscreen mode Exit fullscreen mode

 

Wrapping up

CSS drawing isn't just a skill, it's a doorway to a world where code meets art. By mastering CSS positioning and getting creative with properties, you can create stunning visuals right in your stylesheet. So, why not start your CSS drawing adventure today?

 

Remember, this is more than just an educational article. It's a reminder that CSS drawing complements, not replaces, traditional graphic design tools. Let your imagination run wild with CSS!

Top comments (0)