Text displayed when hovering over a button with a fading effect

UPDATE: I am currently looking for a way to display text below the button, so that when the button fades away upon hovering, the text is revealed in its place.

Is there a method to position text below (in the z axis) a button created with an anchor element, such that when the button is hovered over, it fades and displays the text below?

I attempted to use positioning and position:absolute to ensure both texts could occupy the same space, but unfortunately, I couldn't get it to work correctly.

Here is a link to my button: http://jsfiddle.net/27bCK/

<div id="pricebar">
 <a class="see" href="#">PRICING</a>
 </div>


#pricebar {
    width:100%;
    height:175px;
    background-color:#EAE5E5;
    text-align:center;
}
#pricebar .see {
    color:#fff;
    font-size:50px;
    line-height:175px;
    background-color:#2ecc71;
    border:5px solid #2ecc71;
    border-radius:5px;
    border-left:17px solid #2ecc71;
    border-right:17px solid #2ecc71;
    transition-duration: .3s;
    transition-property: background-color, border, border-left, border-right, opacity;
}
#pricebar .see:hover {
    opacity:0;
    background-color:#27ae60;
    border:5px solid #27ae60;
    border-left:17px solid #27ae60;
    border-right:17px solid #27ae60;
}
#pricebar .show {
    color:#fff;
    font-size:50px;
    line-height:175px;
    background-color:#000;
    margin-right:20px;
}

Answer №1

Give this a shot:

<div id="pricebar">
 <a class="view" href="#">VIEW PRICING</a>
 <div id="hidden">secret pricing details</div>
</div>

CSS styling:

#hidden{
text-align:center;
position:relative;
bottom:100px;
z-index:0;
border:0px solid red;
}


#pricebar {
width:100%;
height:175px;
background-color:#EAE5E5;
text-align:center;
    z-index:1;

}

#pricebar .view {
color:#fff;
font-size:50px;
line-height:175px;
background-color:#2ecc71;
border:5px solid #2ecc71;
border-radius:5px;
border-left:17px solid #2ecc71;
border-right:17px solid #2ecc71;
transition-duration: .3s;
    transition-property: background-color, border, border-left, border-right, opacity;
    position:relative;
    z-index:3;

}

#pricebar .view:hover {
opacity:0;
background-color:#27ae60;
border:5px solid #27ae60;
border-left:17px solid #27ae60;
border-right:17px solid #27ae60;

}
#pricebar .show {
color:#fff;
font-size:50px;
line-height:175px;
background-color:#000;
margin-right:20px;

}

Answer №2

One way to achieve this is by utilizing jQuery. While there may be simpler methods, my current focus lies on jQuery. With the help of a specific class, you can easily add or remove elements. Let's delve into the jQuery code for better understanding.

Here is the jQuery code snippet:

$('a').hover(
    function() {
        var $this = $(this); // storing $(this) in a variable
        $this.data('initialText', $this.text());
        $this.text("I'm replaced!");
    },
    function() {
        var $this = $(this); // storing $(this) in a variable
        $this.text($this.data('initialText'));
    }
);

UPDATE: I have modified the process of adding and removing classes by caching the original data into a variable. As you hover over the element, it displays the new text specified within the jQuery function, reverting back when hovered out. Credit for this technique goes to a helpful answer on Stack Overflow; consider upvoting that response. This method should align with your requirements.

View the JSFIDDLE example

Answer №3

To achieve this using only CSS, a few modifications need to be made. Remove the line height, make sure both .see and .show have absolute positioning, set the pricebar position to relative, assign height and width to see and show elements, place the content for .show above the .see element in your HTML, and adjust with negative margin to align them.

#pricebar {
width:100%;
height:175px;
background-color:#EAE5E5;
text-align:center;
position:relative;
}
#pricebar .see {
color:#fff;
font-size:50px;

margin-left:-15%;    
background-color:#2ecc71;
border:5px solid #2ecc71;
border-radius:5px;
border-left:17px solid #2ecc71;
border-right:17px solid #2ecc71;
transition-duration: .3s;
transition-property: background-color, border, border-left, border-right, opacity;
position:absolute;
height:60px;
width:234px;
top:60px;
}
#pricebar .see:hover {
opacity:0;
background-color:#27ae60;
border:5px solid #27ae60;
border-left:17px solid #27ae60;
border-right:17px solid #27ae60;
}
#pricebar .show {
color:#fff;
font-size:50px;
background-color:#000;
height:60px;
top:60px;
margin-left:-40px;
position:absolute;
}

You can view the implementation in this fiddle

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 are some techniques for styling a label element with an empty for attribute using CSS?

Here is what I currently have: <label class="filter-label" for="pv[]181"> ::before white <span class="count">5</span> ::after </label> My goal is to modify the ::after element within that label, but so far my attempts ...

Two sections that are supposed to have the same height, but one seems to be slightly taller than the other

Upon closer inspection: At first glance, they may appear identical at certain zoom levels. However, a more detailed examination through zooming in and out reveals a noticeable discrepancy. Firefox's default zoom may make them seem incorrect, while Chr ...

Strange FontAwesome input placeholder issue

I'm trying to use an icon from FontAwesome in the placeholder of an input field. Here is my code snippet: <input type="password" name="password" placeholder="&#xf023; &nbsp; Password"> However, when I view it on the screen, it appear ...

Highlight all the written content within the text box

I'm struggling with a piece of code that is supposed to select all the text inside an input field: <input id="userName" class="form-control" type="text" name="enteredUserName" data-ng-show="vm.userNameDisplayed()" data-ng-model="vm.enteredUs ...

Top method for generating intricate HTML elements sent by the server in real-time

When a Mouse click event occurs, I want to populate a <div> with a form. I have set up the jQuery to send a request to the server and receive the form as plain text. My question is: if I use the innerHTML method to add the form to the <div>, w ...

Python script utilizing Selenium is returning an empty string when trying to extract data from

When I try to retrieve the value from a dynamically loading table by clicking on the TD element, it works fine. However, when I attempt to fetch the text from the same TD element, it returns an empty string despite trying XPATH and CSS Selector methods. H ...

Interactive Navigation Path URL

I came across this code online that does almost exactly what I need it to do, but there are a few issues that I've run into. One problem is that when I click on the home tab, it adds "https" in front of the domain which causes the website to show as ...

Bizarre idiosyncrasy in Internet Explorer 7/8 regarding Javascript

Whenever I access my page in IE, the sliding carousel feature malfunctions. Instead of displaying images in a carousel format, they are shown in a list format. It seems like IE is not reading the javascript for the selector and applying the effect as it sh ...

Combination of AngularJS and jQuery

I have a page in which additional input fields are dynamically added based on the selection made in a drop-down menu. This page is powered by angularjs, but I had to use jQuery specifically for adding these extra input fields. Here is the HTML code snippe ...

Seeking assistance with CSS for hover element - struggling to stretch it across the entire width of the page

Looking for some expert advice in CSS. Visit the link below: When you hover over the years (2008, 2009, 2010, etc.) at the top middle of the page, you'll see an orange bar extending from the top to the bottom. I'm trying to achieve the same effe ...

What is the best way to print HTML code without having to write it multiple times?

I am working at a web application and i am doing some refactoring. Doing so, anyway, i was caught in a dilemma: i have some similar nor identical parts in my pages that i want to compress in just one in order to make some edits just once, since edits are ...

Tips for creating a full-width background image in a Bootstrap 4 carousel

I am struggling with making the images in my 2 slide carousel full width. I have built the carousel using Bootstrap 4, and each slide has an image as its background. Typically, I would use CSS properties like background-size: cover, background-attachment: ...

Conceal YouTube upon opening any model or light box

I have a YouTube video in the center of my page, and when I click on any link from the side navigation, a lightbox appears. However, in IE, the lightbox is behind the YouTube video. I have tried setting the Z-index, but it doesn't seem to work. Is the ...

Creating a Pop-Up on Website Load with HTML, CSS, and Jquery

<script> // utilizing only jquery $(document).ready(function(){ $('#overlay-back').fadeIn(500,function(){ $('#popup').show(); }); $(".close-image").on('click', function() { $('#po ...

The bottom section of the main page

I've successfully implemented a footer that sticks to the bottom of each page as intended. However, I'm facing an issue when there's a large amount of text on a particular page that requires scrolling. The scroll continues beyond the footer, ...

Shift the card and all associated elements downwards

My challenge lies in adjusting the placement of an image within a card, without it overlapping text or other images. How can I achieve a layout where the top half displays the bottom part of the image and the bottom half contains additional elements like t ...

Reduce the height of the tabs bar in the Gnome terminal

After recently installing Lubuntu 16.04 with gnome-terminal, I noticed that the terminal takes up more space on my monitor due to two unnecessary buttons in the top right corner. For avid terminal users like myself, these buttons only add bulk to the tabs ...

Align the radio button in the center of the input field

I have created custom radio buttons, and the last option includes a radio button with an input field. However, in the Codepen example provided, the alignment of the radio button and input field is not centered vertically. I've attempted various soluti ...

Navigating tag blocks in VIM can be done easily by using the open

Is there a way to wrap a block inside tags in Vim, similar to how it's done in Sublime as shown in the image? ...

Unable to align span vertically using font-style "Luckiest Guy" in CSS

I have encountered an issue with vertically centering a span using the font-style "Luckiest Guy". https://i.sstatic.net/Lz8o3.png I attempted to use display: flex;align-items: center; on the span, but it did not work. App.vue <template> <div ...