Introduction
Just about every other website I visit has wave patterns. I have no doubt you've seen them, too, used as dividers between content sections or a subtle background texture. The idea is they add a fluid, non-linear flair to a web design rather than relying on crisp, solid lines between elements.
Gallery of examples โฌ
- markitup.in
- uplabs.com/posts/portfolio-8efd741d-109f-47..
- csshint.com/css-wave-animation
- digitalocean.com
- theeventscalendar.com
- basecamp.com
- helpscout.com
- fauna.com
Waves are a neat pattern! And creating them is pretty darn easy, thanks to the gazillion online generators you can use to spit out SVG you to add directly to your HTML. Here are just a few of them:
Those are great, so use them! At the same time, I think it's a good idea to know how to make something like waves from scratch because it helps us understand the code we're using.
And there's more than one way to make waves! ๐
Using border-radius
in CSS
The idea here is to make a very large circular shape and position it off the page so only part of it is revealed:
codepen.io/geoffgraham/pen/qBoGedg
Repeat the element for more waves:
codepen.io/geoffgraham/pen/qBoGedg
This certainly works, but you may already see its limitations. For example, we need to know how many ways there are and then use some magic numbers to position everything.
Maybe we could do something with CSS Grid's auto-fit
powers so the number of items is less important:
codepen.io/geoffgraham/pen/xxWNvXe
Eh, still not the best. What makes this approach so tough is that putting two circles together doesn't really create a wave pattern. So, I'd say border-radius
is great when we're working with a single shape.
Using CSS gradients
Yeah! We can totally chain some gradients together with various opacities to make wavey patterns. In fact, Lea Verou's classic "CSS3 Patterns Gallery" offers a perfect example using a combination of linear and radial gradients:
codepen.io/geoffgraham/pen/JjLQPgy
Using CSS clip-path
and mask
Using clip-path
and mask
you can create creative waves and how to look into them.
This example shows you what exactly you can create with clip-path.
This is a way to create waves using a clip path and below is an example of adding creativity with CSS animations.
clip-path is not a flexible way to create waves you have to put every pixel or dot but you can create and do amazing work using it. Now there's another way to create waves which are super amazing and easy. Let's look into it.
Inline SVG
We also have SVG! What I like about this approach is that the SVG can form the shape:
codepen.io/geoffgraham/pen/WNzBqwN
Then we have all the flexibility in the world to fill the waves in with whatever color we want in CSS:
codepen.io/geoffgraham/pen/bGvyPpX
Or we can drop in a bunch of SVGs and use absolute positioning on them to get a real splashy:
codepen.io/ganesh1010/pen/xxWmLvY
Using SVG in CSS
We can also use SVG as a background image:
codepen.io/geoffgraham/pen/NWYVZRW
All we have to do to get multiple waves with the background image approach is to repeat the element a few times in the HTML, then reach for background-position-x
to space them apart and opacity
to show the layers:
codepen.io/geoffgraham/pen/Yzabomo
And, of course, we can animate those as well. We tweak the animation-delay
and animation-duration
of each wave to offset things a bit:
codepen.io/geoffgraham/pen/jOzojRB
Conclusion
There you go, a bunch of ways to make waves! We looked at a bunch of different wave patterns and several approaches for coding them in CSS and SVG. Which approach is best? It really depends on the design and use case, so choose what fits best with your requirements.