Animation unveiling of text slides

I have been working on designing a unique button for my personal diet app project that involves a sliding text effect when the mouse hovers over it. I'm almost there, but I've encountered a major issue that seems impossible to solve. I want the sliding text to appear within a black box with yellow borders rather than just floating around. However, due to the 0 width of the text needed to trigger the animation inside the button, the black box ends up being just a vertical line in the center of the button. Additionally, the black box remains visible at all times instead of being transparent like the text before hover. I've tried various methods, such as adjusting the width, nesting the text in containers, and styling them differently, but nothing seems to work. Any suggestions or help would be greatly appreciated!

#create-meal-button {
    background-color: #ffc107;
    border-radius: 100%;
    transition: 0.3s;
    display:inline-block; 
}
#create-meal-button:hover {
    transform: scale(1.2);
    background-color: black;
    border: solid 0.1vw #ffc107;
}
#create-meal-button > svg { width: 50px; }
#create-meal-button > svg:hover { fill:#ffc107; }

#create-meal {
    text-align: center;
}

#create-meal > div {
    display: inline-block; 
}
span {
    display: inline-block; /* inline-block so width can be set */
    width: 0;
    white-space: nowrap; /* keep text in one line */
    direction: ltr; /* overflow direction */
    color: rgba(255,255,255,0); /* text transparency before hover */
    transition: .5s;
    transform: translateX(-20px); /* distance the text will move on hover */
    pointer-events: none;
}

#create-meal > div:hover span {
    color: #ffc107;
    border: solid 0.1vw #ffc107;
    transform: translateX(0);
}
<head>
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2f2222393e393f2c3d0d78637e637f">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c4c9c9d2d5d2d4c7d6e69388958894">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</head>

<div id="create-meal">
    <div>
        <button id="create-meal-button" class="btn">
            <svg xmlns="http://www.w3.org/2000/svg" height="50" viewBox="0 -960 960 960" width="50">
                <path d="M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z"/>
            </svg>
        </button>
        <span id="create-meal-tooltip" class="bg-black p-1 rounded">Create meal</span>
    </div>
</div>

Answer №1

Here are some adjustments you can make to your code to achieve the desired outcome.

#create-meal-button {
    background-color: #ffc107;
    border-radius: 100%;
    transition: 0.3s;
    display:inline-block; 
    position: relative; /* Include this line */
    
}
#create-meal-button:hover {
    transform: scale(1.2);
    background-color: black;
    border: solid 0.1vw #ffc107;
}
#create-meal-button > svg { width: 50px; }
#create-meal-button > svg:hover { fill:#ffc107; }

#create-meal {
    text-align: center;
}

#create-meal > div {
    display: inline-block; 
}

#create-meal-tooltip {
overflow: hidden; /* Include this line */
    display: inline-block;
    white-space: nowrap;
    direction: ltr;
    color: rgba(255,255,255,0);
    transition: .5s;
    transform: translateX(-20px);
    position: absolute; /* Include this line */
    top: 50%;
    left: 50%;
     transform: translate(-50%, -50%) translateX(0%); /* Modify this line */
opacity:0;
}


#create-meal-button:hover #create-meal-tooltip {
    color: #ffc107;
opacity: 1;
    background-color: black;
    border: solid 0.1vw #ffc107;
    transform: translate(-50%, -50%) translateX(100%); /* Modify this line */
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee8c81819a9d9a9c8f9eaedbc0ddc0dc">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23414c4c57505751425363160d100d0b1e19181704131312572a7a4976">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div id="create-meal">
    <button id="create-meal-button" class="btn">
        <svg xmlns="http://www.w3.org/2000/svg" height="50" viewBox="0 -960 960 960" width="50">
            <path d="M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z"/>
        </svg>
        <span id="create-meal-tooltip" class="bg-black p-1 rounded">Create meal</span>
    </button>
</div>

Answer №2

Implement the following code to display text around a button without modifying the HTML structure.

#create-meal-button {
    background-color: #ffc107;
    border-radius: 100%;
    transition: 0.3s;
    display:inline-block; 
}
#create-meal-button:hover {
    transform: scale(1.2);
    background-color: black;
    border: solid 0.1vw #ffc107;
}
#create-meal-button > svg { width: 50px; }
#create-meal-button > svg:hover { fill:#ffc107; }

#create-meal {
    text-align: center;
}
#create-meal-tooltip {
    opacity:0;
    position:relative;
    transform: translateX(-20px);
    transition: opacity .5s, transform .5s;
    pointer-events: none;
    display:inline-block;
}
#create-meal:hover #create-meal-tooltip {
    opacity:1;
    transform: translateX(5px); /*add some space*/
    color: #ffc107;
    border: solid 0.1vw #ffc107;
}
<head>
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04b9b4b4afb8bbbbbdaebfb8a9d99cc7dad7cddb">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="660916161d0a0d0b18191407067c29203d202c">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</head>

<div id="create-meal">
    <div>
        <button id="create-meal-button" class="btn">
            <svg xmlns="http://www.w3.org/2000/svg" height="50" viewBox="0 -960 960 960" width="50">
                <path d="M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z"/>
            </svg>
        </button>
        <span id="create-meal-tooltip" class="bg-black p-1 rounded">Create meal</span>
    </div>
</div>

To enhance the visual effect, I utilized opacity:0; to create smoother transitions. Additionally, I applied transform: translateX(-20px); for an "outward slide from the right" effect.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to disregard all pathNames and display my index.php file directly from the root of my website?

After a user clicks on a navigation link on my website, the page is loaded into a content window within the same page using JavaScript. I then utilize HTML 5 to modify the URL and create a history entry, resulting in a URL resembling the one mentioned (). ...

Converting data from Random.org into an integer using JavaScript

I have been working on a small web application to experiment with CSS animations. Although it's functioning, I'm seeking more genuine randomness. To achieve this, I am exploring the use of Random.org. How can I import the output from Random.org i ...

Displaying vertical content with a horizontal scroll using Bootstrap

I have created a div container and I would like to implement a horizontal scroll bar when there are more than four items (each item consists of a picture with a title). Desired outcome: https://i.sstatic.net/wWucQ.png The issue I am facing is that by usi ...

Expansive full screen image proportions

Is there a way to showcase an image on the entire screen without distorting it, while maintaining its aspect ratio in either HTML or Javascript? The desired outcome is for the image to be centered and: - Have a height of 100% and a width of x% (where "x" ...

Divide a page into two equal halves

I have been working on the code below. The circular icons function as navigation buttons. The collapsible list is located in the second div. My goal is to align the list to the right side of the navigation buttons. Despite trying to wrap the divs and us ...

The issue of HTML Service Submit Form not triggering the google.script.run function

Everything was going smoothly with my script until it suddenly stopped functioning properly. A user selects an option from a custom menu, triggering a dialog box (HTML Service form) to collect two parameters as intended. Upon submitting the form, the fol ...

What is the best way to add a color swatch image using Javascript?

I'm facing a challenge in Shopify where I need to assign corresponding background images to color swatches. Currently, my icons are set up with the correct links for each color, but they are missing their respective images, similar to this example. I ...

Enhancing the synchronization of data between the model and the view in

Can AngularJS support "double data-binding" functionality? I am trying to extract mat.Discipline[0].nom_FR from an array containing nom_FR, nom_DE, nom_EN, and nom_IT. The value of mat.Langue[0].langue is either "FR", "DE", "EN", or "IT" based on the sel ...

placing an image within a frame

I am having trouble aligning two divs side by side. I want to create a white background box with the date displayed in the top right corner, and I want the image to be at the same height as the date. Below is the code I have written: #boxx { background-c ...

Access remote web page within bootstrap modal

Dealing with a recurring issue, I am trying to create a modal that opens content from a remote page populated by a MySql database. Custom styling is also required for the modal. Progress has been made, but now I'm stuck. Below is the current code: Ou ...

Developing web applications using Express.js and Jade involves connecting CSS stylesheets

I'm currently in the process of developing a web application using Express.js along with some Bootstrap templates. I utilized jade2html for conversion, and while the page loads successfully, it lacks any styling. The following code snippet is what I ...

Can you explain the HTML semantic layout for having multiple titles matched with their respective lists?

In my directional segment, I have included details such as the address, phone number, email, and service date/time. I envision it looking like the example below: https://i.sstatic.net/5h4SM.jpg Currently, my code appears as follows: <div class="co ...

Automatic padding within Bootstrap5

I'm struggling to understand why I am experiencing a consistent padding at the bottom of my container in Bootstrap5. I attempted to use align-items-center within the row to center the content, but it had no effect. <!DOCTYPE html> <html la ...

Updating choices within a div in real-time by changing the select box dynamically

I need to swap out the select box that is nested inside this div <div class="models"> <select disabled="disable"> <option>Model Name</option> </select> </div> I am attempting to target the div and manipul ...

Some PDF files appear as blank when shown using a Base64 encoded string

I am attempting to display a PDF file on a web page using its base64 encoding. Below is the ReactJS code I am using: <object style={{ width: '100%', height:'842pt' }} type="application/pdf" data={`data:application/pdf;base ...

How can I assign a class to my Typography component in Material UI?

When using my material-ui component, I wanted to add an ellipsis to my Typography element when it overflows. To achieve this, I defined the following styles: const customStyles = (theme) => ({ root: { textAlign: "left", margin: theme.spacing( ...

"Troubleshooting Bootstrap nav-pills' failure to update displayed content

I'm currently working on creating a dynamic navbar that updates the content based on which navigation pill is selected. For some reason, the content in my div.tab-content isn't changing as expected... Here is an example of the code I am using: ...

Assign the callback function to execute when the select element loses focus

Is there a way to trigger a function when the user clicks out of a select menu without selecting an option, even though I know about the onChange and onFocus event listeners associated with the select HTML element? ...

Learning the basics of JavaScript: Displaying the last number in an array and restarting a function

Hey there, I'm diving into the world of JavaScript and have set myself a challenge to create a simple bingo number machine. Check out my CodePen project here. I've successfully generated an array of 20 numbers, shuffled them, and added a marker t ...

Having trouble pinpointing the issue with this particular if statement?

I am currently working on creating a form that compiles all entered data when the user fills out all fields. The form is connected to a PHP file and functions properly, but I encountered issues when implementing validation logic. The "Validation" section ...