In this article, we will learn how to create a floating text effect using HTML, CSS, and JavaScript. This effect is achieved by animating each character of the text individually when hovered over.
We will start with the HTML structure. This code creates a page with a floating title:
Floating Text Effect
FLOAT TEXT
container
div and an h1
title with the ID float-text
.Ensuite, nous allons ajouter le style nécessaire pour obtenir l’effet flottant.
body {
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
background-size: 400% 400%;
animation: gradient 15s ease infinite;
height: 100vh;
}
@keyframes gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#float-text {
color: white;
font-family: 'Times New Roman', Times, serif;
font-size: 100px;
}
#float-text span {
display: inline-block;
transition: transform 0.3s ease-in-out;
}
#float-text span:hover {
animation: float 3s ease-in-out infinite;
cursor: pointer;
}
@keyframes float {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
#float-text span {
display: inline-block;
transition: transform 0.3s ease-in-out;
}
#float-text span
: Selects all
elements that are children of the element with the ID float-text
.display: inline-block
: Allows
elements to behave like blocks while still remaining on the same line. This enables transformations (like movement) to be applied without breaking the line of text.transition: transform 0.3s ease-in-out
: Adds a smooth transition to transformations on these elements. This means that any transformation applied (like a movement) will take 0.3 seconds with an ease-in-out effect. #float-text span:hover {
animation: float 3s ease-in-out infinite;
cursor: pointer;
}
#float-text span:hover
: Targets the
elements when they are hovered over by the mouse.animation: float 3s ease-in-out infinite
: Applies the animation defined by @keyframes float
for 3 seconds, with an ease-in-out effect, and loops infinitely.cursor: pointer
: Changes the cursor to a hand shape to indicate that the element is interactive. @keyframes float {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
@keyframes float
: Defines an animation named float
.0%, 100% { transform: translateY(0); }
: At the 0% (start) and 100% (end) positions of the animation, the elements are in their original position (no vertical movement).50% { transform: translateY(-20px); }
: At 50% of the animation (midpoint), the elements are moved up by 20 pixels.To further customize your website, check out our guide on the LED light effect with JavaScript. This tutorial shows you how to enhance your website with attractive lighting effects.
Finally, we will add JavaScript to split the text into individual characters.
document.addEventListener('DOMContentLoaded', function(){
const text = document.getElementById('float-text');
const textContent = text.textContent;
text.innerHTML = '';
textContent.split('').forEach(char => {
const span = document.createElement('span');
if(char === ' ') {
span.innerHTML = ' ';
} else {
span.textContent = char;
}
text.appendChild(span);
});
});
document.addEventListener('DOMContentLoaded', function(){ ... })
: Adds an event listener that waits for the entire HTML document content to be fully loaded and parsed, without waiting for stylesheets, images, and sub-documents to finish loading. This ensures that the script runs only when the DOM is ready. document.addEventListener('DOMContentLoaded', function(){
const text = document.getElementById('float-text');
const textContent = text.textContent;
text.innerHTML = '';
const textContent = text.textContent;
: Retrieves the text content of the text
element and stores it in the textContent
variable.text.innerHTML = '';
: Empties the HTML content of the text
element, preparing it to receive new
elements. textContent.split('').forEach(char => {
const span = document.createElement('span');
if (char === ' ') {
span.innerHTML = ' ';
} else {
span.textContent = char;
}
text.appendChild(span);
});
textContent.split('').forEach(char => { ... });
: Splits the text into individual characters and iterates over each one.const span = document.createElement('span');
: Creates a new
element for each character.if (char === ' ') { span.innerHTML = ' '; }
: If the character is a space, it inserts a non-breaking space (
) into the
to preserve spaces.else { span.textContent = char; }
: Otherwise, place the character in the
.text.appendChild(span);
: Adds the
to the text
element.DOMContentLoaded
: Ensures that the script runs once the DOM is fully loaded.float-text
: Retrieves the element containing the text to be animated.float-text
element is cleared.
elements: Each character (including non-breaking spaces) is wrapped in a
element and added to float-text
.By following these steps, you can create an interactive and aesthetic floating text effect. Feel free to experiment with colors, fonts, and animations to customize the effect to your liking.
Share it
MonDesign Web, your digital marketing agency, dedicated to guiding you to the pinnacle of your online success.
We use cookies to improve your experience on our site. By using our site, you consent to cookies.
Websites store cookies to enhance functionality and personalise your experience. You can manage your preferences, but blocking some cookies may impact site performance and services.
Essential cookies enable basic functions and are necessary for the proper function of the website.
These cookies are needed for adding comments on this website.
Statistics cookies collect information anonymously. This information helps us understand how visitors use our website.
Google Analytics is a powerful tool that tracks and analyzes website traffic for informed marketing decisions.
Service URL: policies.google.com
Marketing cookies are used to follow visitors to websites. The intention is to show ads that are relevant and engaging to the individual user.
Facebook Pixel is a web analytics service that tracks and reports website traffic.
Service URL: www.facebook.com