Change the button's background color gradually when hovered over using CSS

Attempting to create a button that transitions its background color from blue to white and the font color from white to blue. I've reviewed similar posts for guidance.

Here is the fiddle showcasing my work: http://jsfiddle.net/t533wbuy/1/

I used this reference fiddle as inspiration: http://jsfiddle.net/visualdecree/SvjHx/

Encountering the following issues:

  • Prefer the button to be an A element without using ul > li structure.
  • The font color does not smoothly transition to white on hover.

CSS

.menuitem{
width: 200px;
height:30px;
background-color: #005abb;
font-size:16px;font-family: 'gotham_htfbold';
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}

.menuitem a{
width: 200px;
height: 30px;
display: block;
position: relative;
color: #FFF;
text-align: center;
z-index: 9999;

}

.menu-item-span{
display: none;
width: 200px;
height: 30px;
background-color: #fff;
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
color:#005abb;
z-index: -999;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
 }

HTML

    Click here to apply

JQuery

$(function() {
$(".menuitem").find("class")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn();
 }, function() {
$(this).find("span").stop(true, true).fadeOut();
});
});

Answer №1

Have you considered using only CSS for this?

a {
  transition: all .4s ease;
  padding: 12px 28px;
  border-radius: 18px;
}
a {
  background-color: blue;
  color: #fff;
}
a:hover {
  background-color: grey;
  color: #000;
}
<a href="#">BUTTON</a>

Answer №2

In this scenario, following the recommendation of @Aaron and @user1447679, the best approach would involve utilizing pure CSS. However, if you are insistent on using jQuery for this task, you can employ the mouseenter and mouseleave events to dynamically alter the color and background-color properties of an element through the use of jQuery's .css() method:

$(function() {
    $("#lnk-1").on('mouseover',function() { 
        $(this).css('background-color','black').css('color','white'); 
    })
    .on('mouseleave',function(){
        $(this).css('background-color','blue').css('color','black'); 
    });
});

A demonstration showcasing how this can be achieved is available here: http://jsfiddle.net/t533wbuy/4/

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

Alignment dilemmas

I'm experimenting with HTML and CSS and have a query concerning the text-align properties. As I work on constructing my personal website, I aim to align text content to start while having it centered in the middle of the page. Currently, I've ma ...

Generating a multidimensional associative array based on user inputs from a form

What is the best way to transform form input data into a multidimensional associative array? This is how the form appears: <div id="items"> <h4>Engraving Text</h4> <div class="item" data-position="1"> <h4 id="en ...

jquery monitors an element to see if it contains a particular attribute, then takes action accordingly

I'm attempting to conceal a div when an anchor tag (a) has a particular href, and reveal it when the href is different. This is what I've tried: if($('a[href="#intro"]').not(".active")) { $('.navbar-brand').hide();} else ...

What are the steps for implementing this javascript code in an HTML document?

Recently, I've been seeking advice on how to address the issue of wanting a button click to automatically select the search box on my website. Suggestions have pointed towards using Javascript for this functionality, with the following code recommend ...

Ways to organize a table in a horizontal manner

I'm having trouble sorting a table horizontally by clicking the header. I attempted the code below: However, the sorting is not functioning correctly. What is an alternative solution to this issue without using any external plugins? $('th&ap ...

Troubleshooting Display Problems when Switching Bootstrap Themes in ASP.NET MVC

I recently made changes to my MVC project by switching the default Bootstrap theme to Bootswatch - Cosmos. However, I am facing an issue with the Navbar as shown in the image below: View Header Display Issue Image Initially, I was using Bootstrap 3.0 and ...

Is it feasible to overlay a PNG image on top of a button while still allowing the button to be clickable?

Can you place an image (.png) on top of a button and still make the button clickable? https://i.sstatic.net/9Z8nH.jpg ...

Implementing jQuery/JavaScript to efficiently iterate through JSON data

I have implemented a form that allows users to select an item from a multi-select dropdown (A). If the desired item is not listed, users can manually add it using another text input box (B). Once added, an AJAX call saves the item to the database. After su ...

Learn how to apply CSS styles from one component to another in Angular

I am attempting to modify the background color of a wrapper from a different component. I tried using ::ng-deep but it's not functioning correctly. For the mauto component, the background should be a solid block color. For the vgud component, the back ...

Is there a way to locate and delete an image element with a broken hyperlink?

My website is currently hosted on Wordpress, and I've noticed that there is an issue with a broken image link on my site. I'm not sure when this occurred, but it seems to only be affecting the post section of my site. https://i.sstatic.net/iarDU ...

Unable to extract information from empty <td> using python and selenium

Currently, I am facing an issue while trying to fetch values from a <tr> using Python Selenium. Specifically, I need these values to be ordered and also identified based on whether they contain the word "PICK" or not. My goal is to determine the exa ...

The "scrollTop" function seems to be malfunctioning in Firefox but works perfectly fine in Chrome. Is there a way to fix this issue?

Issue with scrollTop in Firefox jQuery(window).scroll(function(){ var NextScroll = jQuery(this).scrollTop(); if (NextScroll >= 800){ jQuery('#logomacchia').addClass("maccancello"); } else { jQuery('#logomacch ...

Troubleshooting a jQuery Selector Issue with a Dynamic Form

I developed a jQuery function to search for all the necessary inputs in a specific section of a website. function check_property_vars() { jQuery(this).parents('.property_group').find('div[id^="property_group_"]:input[required]:visible&a ...

Using JQuery to swap out all URLs on a webpage?

Seeking guidance and examples on replacing all the links in the head of a document using jQuery. I am currently working with an app that requires my site to have SSL. To address this requirement without purchasing an SSL certificate or a static IP, I am a ...

Using PHP to utilize logical OR or AND operators when adding a class to the global navigation's list items

I am currently working on implementing a global navigation bar using PHP on all pages. The PHP code is also being used to add a class and display the current page. The main challenge I am encountering involves selecting the parent navigation item. When on ...

How can I retrieve HTML with JavaScript enabled?

Seeking to extract the HTML code from a webpage using PHP, I attempted the following: $url = 'http://en.wikipedia.org/wiki/New_York_City'; $html = file_get_html($url); However, an issue arose where Wikipedia failed to supply the <script> ...

Looping through PHP code to encapsulate HTML elements

As a newcomer to PHP, I appreciate your patience as I navigate this new language. My goal is to wrap a div around elements like the "h4" tag without disrupting the for loop. Is this achievable? Thank you. <?php $con=mysqli_connect("localhost","root"," ...

The innerHTML property of the iframe element returns null

Yesterday, the code was functioning properly as iframe[0].contentWindow.document.body.innerHTML displayed "success". However, today, both iframe[0].contentWindow.document.body and iframe[0].contentDocument.body are empty. The innerHTML cannot be found. ht ...

jQuery Form: Despite the Ajax response being visible on the page, it does not appear in the source code

Utilizing Jquery Form for an Ajax image upload. This is the relevant HTML code: <div class="input_con imageupload_con"> <form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm"> < ...

Changing a JSON reply to an object using JQuery within Symfony2

I recently delved into the world of AJAX and Json, working on creating a call for a thread of comments to be displayed on my webpage. After the controller finishes loading, it returns an array of objects in Json format: '[{"usr":"gigi","usrpic":"4993 ...