Adjusting the text of a button when hovering over it will also trigger a resizing of the

Currently, I am facing an issue where the bootstrap button's size changes when hovered over. My intention is to have the bootstrap button remain a fixed size while only the text inside it changes using javascript for mouseover and mouseout events. How can I prevent the button from resizing entirely?

Sample Button:

<button type='button' id='Warning' class='btn btn-warning btn-block text-left' data-toggle='modal' data-target='#myModal'>Pending</button>

Javascript:

    $('body').on('mouseover', '#Success', function (e) {
        $(this).removeClass("btn-success");
        $(this).addClass("btn-danger");
        $(this).text('Deactivate?');
    });
    $('body').on('mouseover', '#Warning', function (e) {
        $(this).removeClass("btn-warning");
        $(this).addClass("btn-success");
        $(this).text('Activate?');
    });

Answer №1

In the event that the visible width of the two text labels varies, adjustments need to be made in the button size to accommodate the text.

One solution is to define a fixed size for the button using CSS that can sufficiently display either text label:

#Warning {
    width: 200px;
}

However, without more details about your markup, it's challenging to determine how this will impact other layout elements.

An alternative approach could be giving the button an auto margin horizontally:

#Warning {
    width: 200px;
    margin: 0 auto;
}

Alternatively, you may need to explicitly center the buttons within their container:

.class-name-of-the-buttons-parent-container {
    text-align: center;
}

Answer №2

To optimize the button design, consider setting a fixed width that accommodates both states and eliminating horizontal paddings. Another option is to apply text-align:center

Answer №3

During the mouseover event, freeze the button's width and height and set its padding to 0 in order to keep the text centered:

$('body')
  .on('mouseover', '#Warning', function (e) {
    $(this).css({
      width: $(this).outerWidth(),
      height: $(this).outerHeight(),
      padding: 0
    });
    $(this).text('Activate?');
  })
  .on('mouseout', '#Warning', function (e) {
    $(this).text('Pending');
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='Warning'>Pending</button>

Answer №4

To customize the button in Bootstrap, consider creating a new CSS class that modifies the default styles. For example...

.custom_button {
    width: 200px;
}

Sometimes you may need to use the !important declaration to ensure your styles take precedence over Bootstrap's existing rules, especially when dealing with its heavy use of !important statements. For instance...

.custom_button {
    width: 200px !important;
}

However, it's best to avoid relying on !important as much as possible to maintain code readability and flexibility.

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

Guide on inserting a dropdown menu following a specific count of <li> elements using Javascript or Jquery

I am currently working on an HTML project <div class="ktmsg"> <ul> <li class="a1"> <a title="Link Tools" href="#"> … </a> </li> <li class="a2"> <a title="Link Tools" href="#"> ...

Refresh the content of a webpage in AngularJS without the need to fully reload the entire page

Within my controller and view files, I have content that is sourced from various places, including API calls. For instance, I have information retrieved from the database where users can update certain details like their last name. After submitting the up ...

Unable to designate the drop-down option as the default selection

Can anyone help me with setting a default drop-down value using JavaScript? I have been struggling to achieve this. You can find my code on jsFiddle <div ng-controller="csrClrt"> <div ng:repeat="(key, item) in items track by $index"> ...

Error message: Unable to locate Bootstrap call in standalone Angular project after executing 'ng add @angular/pwa' command

Having an issue while trying to integrate @angular/pwa, it keeps showing me an error saying "Bootstrap call not found". It's worth mentioning that I have removed app.module.ts and am using standalone components in various places without any module. Cu ...

declaration of function interface and property that cannot be modified

After reviewing some new TypeScript code, I encountered a part that left me puzzled. interface test { (a: number): number; readonly b: number; } While I understand that (a:number): number signifies a function where the argument is a:number and the ret ...

Encountered difficulty accessing the controller ActionResult from JavaScript代码

Resolution: After thorough investigation, I successfully identified and resolved the issue. Surprisingly, it was not related to the Javascript or Controller code as initially anticipated. The root cause stemmed from a .dll file that was causing discrepanci ...

Exploring MySQL: Retrieving Data Efficiently while Monitoring Server Performance

Currently, I am in the process of developing a web application that communicates with a MySQL database to send and receive data. My goal is to display any changes made in the database on the web page as quickly as possible. My main concern now is determin ...

Using jQuery to send an image to web service

I'm currently working on a project for a Blackberry OS 5 + application and I have a requirement to send data along with an image. For the development of this application, I am utilizing HTML5, jQuery, and Webworks SDK. The form structure looks like ...

How can Selenium interact with various shadow DOM elements within a dropdown menu?

I am attempting to interact with a dropdown menu in order to track activity on the page when clicking on one of its items. Here is an overview of my HTML structure: <slot> #shadowroot <myoption-cmp> #shadowroot <some anchor te ...

Why is my JSON object showing up as undefined in my Node.js POST request?

I have a CentOS server that is currently running a node.js HTTP server (code provided below). My goal is to pass JSON data from the command line by using the following CURL command: curl -X POST -H "application/json" -d '{"val1":"hello","val2":"my"," ...

Marked checkboxes and Node.js

I'm having trouble grasping the concept of using HTML checkboxes with Node.js and Express. I have a basic form in EJS and before diving deeper into the backend logic, I want to ensure that the correct values are being retrieved. Despite my efforts to ...

WebDriver encounters difficulty clicking on a certificate error popup window

Currently, I am using webdriver 2.40.0 in C# to interact with my company's website. The issue arises when I encounter a certificate error page while trying to access certain elements. Specifically, after clicking the override link and entering some in ...

Ways to identify whether the ajax error is due to Access-Control-Allow-Origin restriction or if the file is genuinely absent

My question pertains to customizing error messages for Access-Control-Allow-Origin issues and outdated URLs. I want to display specific messages to the user based on different types of errors that may occur. Currently, my code looks like this: $.ajax( { ...

Transfer a term to a different division - JavaScript Object Model

I am trying to achieve a specific task where I need to move one term under another in SharePoint. However, despite my efforts using JSOM and SharePoint 2013, I have not been able to find a direct method for moving terms. The code snippet I have used below ...

What is the process for connecting personalized toggle controls to a checkbox input field?

I have created a custom slide toggle control to enhance the functionality of my checkboxes in the app. You can check out the controls by following this link: http://codepen.io/spstratis/pen/fJvoH Currently, I am looking for a way to connect these switche ...

Understanding how objects are created using the reduce method

I'm curious about how the reduce function can transform an array into an object as shown in this example, particularly with the line: p[c[0]] = c[1]; Is this an unconventional syntax for creating key-value pairs that I haven't encountered before ...

Converting Moment JS to Carbon for Time Calculations

How can I synchronize the timestamps between MomentJS and Carbon instances in the GMT timezone? $(document).on('click', '#confirm-reservation .dropdown-menu a', function () { $('#insert_text').text($(this).text()); va ...

Exploring the features of the AJAX event object within a callback setting

I have successfully implemented the Aquantum jQuery plugin for file uploads on my LAMP site. While it works well, I am now looking to add a success callback function that can insert both the uploaded filename and file URL into a form field. You can find ...

The functions (window).load and (document).ready are creating a clash in execution

I am struggling to make both jQuery scripts work together smoothly! Below is the code I'm using: (function($){ //The first parameter of this function is called $ $(document).ready(function(){ // tabbed boxes functionality $(&apos ...

How to incorporate an image as a background in a Bootstrap 4 card

I'm working on achieving a specific design, as shown in the image https://i.sstatic.net/GRjwo.jpg In Bootstrap 4, it's simply a group of cards with the "card-img-overlay" class. The issue arises when the height of the card is dependent on the im ...