DEV Community 👩‍💻👨‍💻

Cover image for A Complete Guide to Waves
Ganesh Patil
Ganesh Patil

Posted on

A Complete Guide to Waves

Makin' Waves

Just about every other website I visit has waves 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

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 make a very large circular shape and position it off the page so only part of it is revealed:

Repeat the element for more waves:

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:

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:

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.

See the Pen
css clip path - wave
by nicholasnadel (@nicholasnadel)
on CodePen.

This is a way to create waves using clip-path and below is an example of adding creativity with CSS animations.

See the Pen
Water Wave Effect Using CSS Clip-path
by Muhammad Mabrouk (@muhammad_mabrouk)
on CodePen.

clip-path is not the 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 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:

Then we have all the flexibility in the world to fill the waves in with whatever color we want in CSS:

Or we can drop in a bunch of SVGs and use absolute positioning on them to get a real splashy:

Using SVG in CSS

We can also use SVG as a background image:

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:

https://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:

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.

Top comments (0)

🌚 Turn on dark mode

🔠 Change your default font

📚 Adjust your experience level to see more relevant content

 

Head to your account's Settings to do all this and more.