Draw a curve with css
Categories:
Drawing Curves with CSS: A Comprehensive Guide

Explore various CSS techniques to create smooth, dynamic, and visually appealing curves for web design, from basic arcs to complex Bézier paths.
Creating curves in CSS can significantly enhance the visual appeal and user experience of a website. While CSS doesn't offer a direct 'curve' property, there are several clever techniques using existing properties like border-radius
, transform
, clip-path
, and SVG to achieve various curved shapes. This article will guide you through the most common and effective methods, helping you choose the right approach for your design needs.
Basic Curves with border-radius
The simplest way to create a curved edge or a circular/oval shape is by using the border-radius
property. This property allows you to round the corners of an element. By applying a large enough border-radius
to a square element, you can create a perfect circle. For rectangular elements, you can achieve oval shapes or rounded corners with varying degrees of curvature.
.circle {
width: 100px;
height: 100px;
background-color: #3498db;
border-radius: 50%; /* Creates a perfect circle */
}
.rounded-rectangle {
width: 200px;
height: 100px;
background-color: #2ecc71;
border-radius: 25px; /* Rounds all corners */
}
.half-circle {
width: 200px;
height: 100px;
background-color: #e74c3c;
border-bottom-left-radius: 100px;
border-bottom-right-radius: 100px;
/* Or for a top half: border-top-left-radius: 100px; border-top-right-radius: 100px; */
}
Examples of border-radius
for circles, rounded rectangles, and half-circles.
border-radius: 50%;
on a square element, it will always result in a perfect circle, regardless of the element's size. For rectangles, border-radius: 50%;
will create an oval.Advanced Curves with clip-path
For more complex and custom curve shapes, the clip-path
property is incredibly powerful. It allows you to define a clipping region, effectively hiding parts of an element outside that region. You can use various functions like polygon()
, circle()
, ellipse()
, and most notably, path()
to draw intricate curves. The path()
function accepts SVG path data, giving you maximum flexibility.
.curved-edge {
width: 300px;
height: 150px;
background-color: #f39c12;
clip-path: polygon(0 0, 100% 0, 100% 80%, 50% 100%, 0 80%);
}
.wave-shape {
width: 400px;
height: 200px;
background-color: #9b59b6;
clip-path: path('M0,100 Q150,0 300,100 T600,100 V200 H0 Z');
/* This path creates a wave-like bottom edge */
}
Using clip-path
with polygon()
and path()
for custom shapes.
flowchart TD A[Start] --> B{"Need a simple curve?"} B -->|Yes| C[Use `border-radius`] B -->|No| D{"Need complex/custom curve?"} D -->|Yes| E[Use `clip-path` with `polygon()` or `path()`] D -->|No| F{"Need animated/interactive curve?"} F -->|Yes| G[Consider SVG or Canvas] F -->|No| H[Review requirements: Is a curve truly needed?] C --> I[End] E --> I G --> I H --> I
Decision flow for choosing CSS curve techniques.
Animating Curves with SVG and CSS
For truly dynamic and complex curves, especially those that need to be animated or interact with user input, SVG (Scalable Vector Graphics) is often the best solution. You can define curves using SVG <path>
elements and then manipulate them with CSS animations or JavaScript. CSS can animate SVG properties like stroke-dasharray
and stroke-dashoffset
to create drawing effects, or transform
for movement and scaling.
<svg width="200" height="200" viewBox="0 0 200 200">
<path id="myCurve" d="M10 100 Q 50 10 100 100 T 190 100" stroke="#e67e22" stroke-width="4" fill="none"/>
</svg>
<style>
#myCurve {
stroke-dasharray: 400;
stroke-dashoffset: 400;
animation: drawCurve 3s forwards;
}
@keyframes drawCurve {
to {
stroke-dashoffset: 0;
}
}
</style>
Animating an SVG path to draw a curve using CSS.
d
attribute contains the path data. M
stands for 'moveto', Q
for 'quadratic Bézier curve', and T
for 'smooth quadratic Bézier curveto'. Understanding these commands is key to drawing custom shapes.1. Identify Your Curve Needs
Determine the complexity and dynamism required for your curve. Is it a static rounded corner, a custom shape, or an animated path?
2. Choose the Right Tool
For simple curves, use border-radius
. For custom static shapes, clip-path
is excellent. For complex, animated, or interactive curves, leverage SVG.
3. Implement and Test
Write your CSS or SVG code. Test across different browsers and screen sizes to ensure your curves render as expected and maintain responsiveness.
4. Refine and Optimize
Adjust values, experiment with different path data, and optimize your code for performance and accessibility.