How to make bubble background using HTML5 and CSS3?
Last Updated :
31 Jul, 2024
Water bubble background animation can easily be generated by using HTML, CSS JavaScript. By using HTML5 we will design the basic body part of the page and by CSS3 we will make the bubbles in the background, And with the help of JavaScript, it will move over the whole page from bottom to up.
Approach:
The basic idea is to create a section using <lspan> element, give it a round shape then by using the CSS animation property translateY the bubble can be moved bottom to top along the Y-axis. The following steps can be followed to obtain the desired result. Create the body by using simple HTML tags.
Make a section in the body tag and then write something which will show in the body of the page.
- HTML code: In this section, we will design a simple body structure:
html
<!DOCTYPE html>
<html>
<body>
<section>
<h2>GeeksforGeeks</h2>
</section>
</body>
</html>
CSS Part: By using CSS we can make the bubbles in the background. First of all, we fixed the body part with margin, padding, and font style. Then we will try to fix the background and use some attributes like position, height, width, background color, text position, etc. Then we will try to make the bubbles in the background. As it's a kinda hover effect so we will use span and span:before to fix the position of the bubble, then we will fix the background as transparent and give some border as it show like water bubble, then we have to add some animation like "animate 4s linear-gradient" to animate it. And the last of the CSS part we will use @Keyframes animation to move the bubbles along y-axis.
css
* {
margin: 0;
padding: 0;
font-family: consolas;
}
section {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
background: #111;
display: flex;
justify-content: center;
align-items: center;
}
section h2 {
font-size: 10em;
color: #333;
}
section span {
position: absolute;
bottom: -50px;
background: transparent;
border-radius: 50%;
pointer-events: none;
box-shadow: inset 0 0 10px rgba(225, 225, 225, 0.5);
animation: animate 4s linear infinite;
}
sectionspan: before {
content: "";
position: absolute;
width: 100%;
height: 100%;
transform: scale(0.25) translate(-70%, -70%);
background: radial-gradient(#ffffff, transparent);
border-radius: 50%;
}
@keyframes animation {
0% {
transform: translateY(0%);
opacity: 1;
}
99% {
opacity: 1;
}
100% {
transform: translateY(-1200%);
opacity: ;
}
}
JavaScript:
At the last we use JavaScript to move the bubbles from bottom to top off the page. And it will give a charming and look like alive. We will take create a function named to create a bubble and write some code. we will give it animation and set the timing and at the last, we will add setInterval(createBubble, 100) as the bubbles can move from bottom to top. If we don't add this the bubble will generate at the bottom part of the page but don't move to the top but to be continued to generate.
JavaScript Code: here we will see how to move the bubbles:
javascript
<script type="text/javascript">
function createBubble() {
const section = document.querySelector("Section");
const createElement = document.createElement("span");
let size = Math.random() * 60;
createElement.style.animation =
"animation 6s linear infinite";
createElement.style.width = 180 + size + "px";
createElement.style.height = 180 + size + "px";
createElement.style.left =
Math.random() * innerWidth + "px";
section.appendChild(createElement);
setTimeout(() => {
createElement.remove();
}, 4000);
}
setInterval(createBubble, 100);
</script>
Complete Code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
* {
margin: 0;
padding: 0;
font-family: consolas;
}
section {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
background: #111;
display: flex;
justify-content: center;
align-items: center;
}
section h2 {
font-size: 10em;
color: #333;
}
section span {
position: absolute;
bottom: -50px;
background: transparent;
border-radius: 50%;
pointer-events: none;
box-shadow: inset 0 0 10px rgba(225, 225, 225, 0.5);
animation: animate 4s linear infinite;
}
section span:before {
content: "";
position: absolute;
width: 100%;
height: 100%;
transform: scale(0.25) translate(-70%, -70%);
background: radial-gradient(#ffffff, transparent);
border-radius: 50%;
}
@keyframes animation {
0% {
transform: translateY(0%);
opacity: 1;
}
99% {
opacity: 1;
}
100% {
transform: translateY(-1200%);
opacity: 0;
}
}
</style>
</head>
<body>
<section>
<h2>GeeksforGeeks</h2>
</section>
<script type="text/javascript">
function createBubble() {
const section = document.querySelector("section");
const createElement = document.createElement("span");
var size = Math.random() * 60;
createElement.style.animation = "animation 6s linear infinite";
createElement.style.width = 180 + size + "px";
createElement.style.height = 180 + size + "px";
createElement.style.left = Math.random() * innerWidth + "px";
section.appendChild(createElement);
setTimeout(() => {
createElement.remove();
}, 4000);
}
setInterval(createBubble, 100);
</script>
</body>
</html>
Output:
Similar Reads
How to blur background image using CSS ? CSS (Cascading Style Sheets) is a language used to describe the appearance and formatting of a document written in HTML. In this article, we will learn how to blur background images using CSS, along with basic implementation examples. Blurring Background Images with CSSTo blur a background image usi
3 min read
How to Add Image in Text Background using HTML and CSS ? In this article, we will use HTML and CSS to set the image in the text background. To set the image in the text background, some CSS property is used. To add an image in the text background using HTML and CSS, create a container element (e.g., a `<div>`), set its background to the desired imag
2 min read
How to Create a Wave Image for a Background using HTML and CSS ? This type of background creates uniqueness on your webpage by avoiding regular rectangular sized background or header. The following header design will show your creativity. This design can be achieved in two ways: Using ::before and ::after selector on a div element in CSS.Using SVG in HTML. Exampl
3 min read
How to Create Animated Background using CSS3 ? Pre-requisite:Basic html Learn HTMLcss Learn cssYou will need to have a basic knowledge of Keyframes with css Learn Keyframes In this article, we are creating the background animation using CSS. The login form is used as a demo and the main intention to design background animation. HTML code: In thi
3 min read
How to Add Border to an Image Using HTML and CSS? Adding a border to an image means putting a line around the image to make it look better and more noticeable. To add a border to an image using HTML and CSS, we can use the <img> tag and apply CSS styles like border, border-width, and border color to customize the border's appearance.Syntax.im
1 min read
How to Create Badges using HTML and CSS ? This article will show you how to create a badge using HTML and CSS. Badges are simple and basic components that are used to display an indicator or count a number. This is quite useful for mail count and alerting purposes, among other things. Badges are identical to labels, with the exception that
2 min read
How to create texture background using CSS ? Introduction: We can use CSS property to texture the background of the web page by using an image editor to cut out a portion of the background. Apply CSS background-repeat property to make the small image fill the area where it is used. CSS provides many styling properties of the background includi
3 min read
How to create a Hero Image using HTML and CSS ? A Hero Image is a large image with text, often placed at the top of a webpage. Hero images can be designed using HTML and CSS. This article contains two sections. The first section attaches the image and designs the basic structure. The second section designs the images and texts on the images. The
2 min read
How to Add Background Image in HTML? Adding a background image to an HTML page can enhance the visual appeal and provide a more immersive experience for your website visitors. The <body> background attribute is used to add a background image in HTML, but this attribute is deprecated in HTML5 and is not in use. In place of the bac
3 min read
How to create and style border using CSS ? The border property is used to create a border around an element and defines how it should look. There are three properties of the border. border-colorborder-widthborder-style border-style property: This defines how the border should look like. It can be solid, dashed, offset, etc. The following val
4 min read