DEV Community

Cover image for Explained: Creating a bubble pattern with just CSS
Charanjit Chana
Charanjit Chana

Posted on • Originally published at 1thingaweek.com

1 1

Explained: Creating a bubble pattern with just CSS

Using CSS to create a zigzag pattern was a lot harder to do than bubbles but there are a couple of ways you can approach this problem and there's a bit of fun you can have too.

Starting with the basics, a repeatable bubble pattern can be created using a circular gradient and letting it repeat:

Basic repeating bubble pattern in CSS

background-image: radial-gradient(transparent 25px, black 25px, black 26px, transparent 26px);
background-size: 100px 100px;
Enter fullscreen mode Exit fullscreen mode

It doesn't get much simpler than that, set the size of the pattern and create the appropriate circle using a radial-gradient that goes from transparent to the colour you want for the border.

The nice thing about this solution, it will take on the background colour for the element you've applied it to. Here's the same pattern on top of a teal background:

Teal bubble pattern with just CSS

background-colour: teal;
Enter fullscreen mode Exit fullscreen mode

The other way to achieve this is to replace transparent with the colour of your choice. So here's the same pattern but with turquoise to fill. It's actually on top of the same teal background, you just can't see it.

Turquoise bubble pattern with just CSS

background-color: teal;
background-image: radial-gradient(turquoise 25px, black 25px, black 26px, turquoise 26px);
Enter fullscreen mode Exit fullscreen mode

Now, for a bit of fun. Using the multiple radial-gradients, background-positions and background-sizes you can build up something a bit more complicated:

CSS bubble pattern that kind of looks like bubbles floating

By no means is this a realistic representation of what bubbles look like, but it just shows that you have a bit of freedom to create repeating patterns without having to load in additional assets.

{
    background-image: radial-gradient(rgba(0,0,255,0.05), rgba(0,0,0,0.025) 25px, rgba(0,0,0,0.2) 26px, transparent 26px),
    radial-gradient(transparent 25px, rgba(176,196,222,0.4) 25px, rgba(176,196,222,0.4) 26px, transparent 26px),
    radial-gradient(transparent 50px, rgba(176,196,222,0.25) 50px, rgba(176,196,222,0.25) 51px, transparent 51px),
    radial-gradient(rgba(176,196,222,0.5), rgba(176,196,222,0) 50px),
    radial-gradient(transparent 25px, rgba(135,206,250,0.35) 25px, rgba(135,206,250,0.35) 26px, transparent 26px);
    background-size: 200px 200px;
    background-position: -75px -45px, 0 0, 20px 50px, 25px 55px, 50px 125px;
}
Enter fullscreen mode Exit fullscreen mode

For this example, you need to specify multiple radial-gradients while also adding a specific background-position for each corresponding gradient. You could also add a specific background-size if you wanted to have a parts of the pattern that might not repeat as often.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay