SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

Draw with CSS: Using CSS To Draw Elements

October 22, 2018 by Alex Fox

With clever use of CSS properties, it is in fact possible to draw very complex shapes. All the shapes below were drawn with CSS alone. It might be that CSS isn’t the best way to render shapes in the browser window, but it’s very fast and included for “free” on all modern browsers. Learn how to draw with CSS below.

Draw with CSS: Border-Radius

Setting the border-radius of an object can round off the corners. When used with more dramatic values, it can draw circular and elliptical shapes.

Circle

If we want to start a simple circle, we could use something like the following:

draw with CSS circle shape

#circle {
  background: lightskyblue;
  width: 100px;
  height: 100px;
  border-radius: 100%
}

Ellipse/Oval

We can use the same properties to draw a simple ellipse.

draw with CSS ellipse shape

#ellipse {
  background: lightskyblue;
  width: 150px;
  height: 100px;
  border-radius: 100%
}

Because the height and width of the object are different, the circle is slightly cockeyed. This produces an ellipse, which is really nothing more than a drunk circle, if you think about it.

Cylinder

Using this principle, we can fake a cylinder too:

draw with CSS cylinder shape

#cylinder {
  background: lightskyblue;
  width: 150px;
  height: 100px;
  border-radius: 100% / 30%;
}
#cylinder:after {
  content:"";
  position: absolute;
  background: grey;
  border: 0 solid transparent;
  border-radius: 100%;
  z-index: 500;
  width: 150px;
  height: 40px;
  top:  6px;
}

And if we add in another pseudo-element, we can give our cylinder a little shine:

draw with CSS shiny cylinder shape

#cylinder:after {
  content:"";
  width: 150px;
  height: 100px;
  border: 0 solid transparent;
  background-image: linear-gradient(to right, rgba(255,255,255,0) 0%, rgba(255,255,255,0.36) 50%, rgba(255,255,255,0) 100%);
  position: absolute;
  top: 10px;
  left: 20px;
  z-index: 400;
}

Visit the Codepen with this shape.

The linear gradient sits on top of the blue but below the grey. It’s also barely opaque, so it only creates a minor effect. This creates the illusion of depth, making our two-dimensional cylinder appear slightly more three-dimensional.

Uneven Border Radius and Slash Notation

This cylinder in both its forms relies on an important property: uneven border-radius and slash notation. If we set two values with a slash, like 100% / 50%, those values individually sets the horizontal border radius separately from the vertical border radius, and in that order. This creates unequal or uneven border curves. You can learn more about uneven border-radius syntax from MDN.

Here’s an example:

body {
  font: 24px Helvetica, sans-serif;
  color: #fcfcfc;
  text-shadow: 1px 1px 0px #000;
}
.shape {
  height: 200px;
  width: 200px;
  background: lightskyblue;
  border: 10px solid green;
  margin: 10px;
  display: block;
  float: left;
}
#even {
  border-radius: 10%;
}
#uneven {
  border-radius: 10% / 20%;
}
#more-uneven {
  border-top-left-radius: 60px 100%; /* no slash on individual radius properties, mix-and-match with pixel and percent measures */
  border-top-right-radius: 10% 40%; /* border will sometimes reflow to fit curve*/
  border-bottom-left-radius: 300px 100px; /* radius measurements larger than the shape itself can be declared */
  border-bottom-right-radius: 10%; /* you can mix and match with regular radius statements */
}
#center {
  width: 80px;
  margin-left: auto;
  margin-right: auto;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

View the Codepen for these shapes here.

For a deep explanation of how border-radius works in a variety of cases, check out Lea Verou’s incredibly deep talk, “The Humble Border-Radius.”

Drawing With Border Widths

Non-curved shapes can be created with careful manipulation of borders. Most of these examples with use an object with 0 height and 0 width, so we’re really drawing with just the borders here.

#shape {
  width: 0; 
  height: 0; 
  border-left: 50px solid transparent;
  border-right: 50px solid transparent; 
  border-bottom: 100px solid lightskyblue;
}

The CSS above will draw and equilateral triangle in light sky blue, my personal favorite of the named CSS colors. It works because of the way borders are divided.

If we were instead to draw a shape like the following, we’d see a more intelligible shape:

#triangle {
  background: black;
  width: 40px; 
  height: 40px; 
  border-top: 50px solid green;
  border-left: 50px solid red;
  border-right: 50px solid yellow; 
  border-bottom: 50px solid lightskyblue;
}

See how the differently colored borders are separated from one another at 45-degree angles? That’s what allows us to create a triangle. We are taking advantage of the spec to draw.

By manipulating which borders are which, we can change the direction of the triangle.

#right-triangle {
  width:0;
  height:0;
  border-left:100px solid transparent;
  border-bottom: 100px solid lightskyblue;
}

We can draw a massive variety of shapes using this trick, including some really complex ones. Take this five-pointed star by Kit MacAllister and found on CSS Tricks.

#star-five {
     margin: 50px 0;
     position: relative;
     display: block;
     color: red;
     width: 0px;
     height: 0px;
     border-right: 100px solid transparent;
     border-bottom: 70px solid lightskyblue;
     border-left: 100px solid transparent;
     transform: rotate(35deg);
 }
 
 #star-five:before {
     border-bottom: 80px solid lightskyblue;
     border-left: 30px solid transparent;
     border-right: 30px solid transparent;
     position: absolute;
     height: 0;
     width: 0;
     top: -45px;
     left: -65px;
     display: block;
     content: '';
     transform: rotate(-35deg);
 }
 
 #star-five:after {
     position: absolute;
     display: block;
     color: red;
     top: 3px;
     left: -105px;
     width: 0px;
     height: 0px;
     border-right: 100px solid transparent;
     border-bottom: 70px solid lightskyblue;
     border-left: 100px solid transparent;
     transform: rotate(-70deg);
     content: '';
 }

Visit the Codepen with this shape.

The big trick with this shape (or combination of shapes) is the use of the transform: rotate property. This works like it sounds, and spins the element around its central point. Positive degree values rotate the shape clockwise. Negative degree values rotate the shape counter-clockwise.

In shapes with zero height and width, the object is rotated about the point that would be the center of the shape if it had dimensions. This means that, with uneven border values, rotations may appear to happen about a position slightly outside the element, which is normal but confusing until you see it.

Conclusion:

Drawing with CSS is an under-utilized use of style sheets. Developers tend to reach immediately for images, SVGs, or even JavaScript. But there’s no reason to bring more than is necessary into your code. Sure, drawing a five-pointed star with CSS isn’t exactly easy or maybe even sensible. But for basic buttons, you can style shapes far more dramatically than you might realize. Try it out on your next project and see what you think.

You might also like the following posts:

Adding Vectors and SVG Images into Website Layouts

What is Z-Index and How Does It Work?

Using CSS Pseudo-elements and Pseudo-classes like ::before and ::after

Filed Under: CSS, Tutorial Tagged With: css, drawing, tutorial

Recent Posts

  • What Factors Determine the Best Digital Marketing Agency?
  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022

Archives

  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022