Place a loading spinner in the middle of a button

Is there a way to add a spinner in the center of a button using CSS that adjusts its position every time the button width changes? Setting margins does not seem to work as the spinner position remains fixed and does not align with the center of the button. Here is my current output And here is the expected output

.ic2-fa-spin-blue {
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
    border: 3px solid #008ad6;
    border-radius: 50%;
    border-top: 3px solid #f3f3f3;
    width: 20px;
    height: 20px;
    -webkit-animation: ic2-spin 2s linear infinite; /* Safari */
    animation: ic2-spin 2s linear infinite;
    -moz-animation: ic2-spin 2s infinite linear;
    -o-animation: ic2-spin 2s infinite linear;
    display: block;
    position: absolute;
}
@-webkit-keyframes ic2-spin {
    0% {
        -webkit-transform: rotate(0deg);
    }

    100% {
        -webkit-transform: rotate(360deg);
    }
}

@keyframes ic2-spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}



.ic2-outlined-spin-blue-btn {
    color: #a3deff;
    border: 1px solid #008ad6;
   
}
.ic2-outlined-btn {
    border-radius: 3px;
    background-color: var(--white-color);
    box-shadow: 0 2px 2px 0 rgba(0,0,0,0.05);
    font-family: Roboto;
    font-size: 17px;
    font-weight: 500;
    text-align: var(--button-alignment);
    padding: 0px 16px;
    border-radius: 3px;    
    width: auto;
    word-spacing: 8px;
    height:32px;
}
<button value="Loading" class="ic2-outlined-btn ic2-outlined-spin-blue-btn ">
  <span class=" ic3-faspinblue"></span>
  Loading
</button>

I am looking for a solution to ensure that the spinner always loads at the center.

Answer №1

To align the spinner, use the following CSS properties:

top: calc(50% - 13px); left: calc(50% - 13px);
. The values are based on the parent element, with 13px representing half of the total width/height (20px) plus border (6px).

Make sure to add position: relative to the button .ic2-outlined-spin-blue-btn to establish it as the positioning context.

.ic2-fa-spin-blue {
  border: 3px solid #008ad6;
  border-radius: 50%;
  border-top: 3px solid #f3f3f3;
  width: 20px;
  height: 20px;
  display: block;
  position: absolute;
  top: calc(50% - 13px);
  left: calc(50% - 13px);
  animation: ic2-spin 2s linear infinite;
}

@keyframes ic2-spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.ic2-outlined-spin-blue-btn {
  position: relative;
  color: #a3deff;
  border: 1px solid #008ad6;
}

.ic2-outlined-btn {
  border-radius: 3px;
  background-color: var(--white-color);
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.05);
  font-family: Roboto;
  font-size: 17px;
  font-weight: 500;
  /*line-height: 32px;*/
  text-align: var(--button-alignment);
  padding: 0px 16px;
  border-radius: 3px;
  width: auto;
  word-spacing: 8px;
  height: 32px;
}
<button value="Loading" class="ic2-outlined-btn ic2-outlined-spin-blue-btn ">
  <span class=" ic2-fa-spin-blue"></span>
  Loading
</button>

Answer №2

  1. Include position:relative in the style for .ic2-outlined-btn class.
  2. Apply the following CSS to the .ic2-fa-spin-blue class.
right: calc(50% - 15px);
top: 2px;

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

Problem with Button Functionality in Outlook Email

I am having trouble making a specific button for Outlook within my email. Due to the different sections in my EMAIL, the button got wrapped in v:rect and now it is shifting to the top of the email. Can someone assist me with this issue? Actual Image - htt ...

Encountering an issue while attempting to install an SSL certificate on my server. The error message displayed is: "ERROR:

Unique HTML Code: <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="farmapp.css"> </head> <body onload=&qu ...

Adjusting the duration of the carousel in Bootstrap 4.2

Is there a way to alter the transition or fade duration of a carousel in BS4.2 using scripts? According to the BS documentation: Adjusting transition duration To modify the transition duration of .carousel-item, you can utilize the $carousel-transition Sa ...

Ways to interact with a button that toggles between states

I am working with a button that has both an enabled and disabled state. Enabled State: <button type="button" class="btt-download-csv site-toolbar-menu-button icon-download no-icon-margins ng-isolate-scope" title="Download CSV" rc-download-csv="" curren ...

What is the best way to add a gradient color to the bottom of your content?

Seeking assistance in replicating the unique UI design featured in the image below, taken from a course description section on udemy.com. The goal is to create a gradient effect on specific parts of the content, similar to what is shown in the image. http ...

When using font size in rem units, it will remain consistent across different screen sizes and not be subject to scaling

When setting padding, margin, and font size using rem units, I noticed that on larger screens the output looks good. However, on smaller screen devices, the sizes do not reduce proportionally - instead, the measurements from the larger screen persist. Wh ...

The elimination of margin-right is not taking place

On mobile devices, I am experiencing an unwanted margin-right on my page. Despite trying various solutions found in this discussion thread, including using body, html {margin:0;} and margin-right:-8px; with !important, the default margin persists. https:/ ...

Is there a way to fix the issue of ContextMenu not appearing at the right position within a scrollable

I have a div within an iframe that needs to display a context menu when right-clicked. The div's length may exceed the visible area, making it scrollable. However, the issue I am facing is that the context menu appears in a different position than whe ...

Mobile devices may experience a glitch with the input group append feature on screens with lower resolution

I have a form in a navigation bar where I am trying to display the submit button beside my text box. Everything looks good on larger screens, but when I switch to mobile view, there is an awkward gap between the text box and the button. This issue becomes ...

Parsing CSS with PHP

Having an issue with my CSS link for images and need some assistance: background-image:url(images/background.gif); I'm looking to adjust the directory by adding ../. The code I aim to modify is as follows: background-image:url(../images/background. ...

Clicking on the mail icon will open the mail with the associated mail id

https://i.sstatic.net/6TLpN.pngWhen the mail icon is clicked, the mail will open with this email address. I am currently working on a project where clicking the mail icon will redirect to the mail signup page with the corresponding email address. In the ...

Having trouble with the border in Tailwind CSS after integrating Flowbite?

Inside this div is where my component is wrapped: <div className="bg-gray-800 border border-gray-700 rounded-lg"> .... </div> When I installed Flowbite, the border color didn't show up as expected. Check out the borde ...

Javascript's second element does not trigger a click event with similar behavior

I'm currently facing an issue with displaying and hiding notification elements based on user interaction. My goal is to have multiple popup elements appear when the page loads. Then, when a user clicks the ".alert-close" element within one of the popu ...

Clicking the button will make it jump

I recently created a landing page for my BTEC Media coursework, but I encountered an issue with a button. Whenever I click on my call-to-action button, it inexplicably increases in length. I've attempted adjusting the max-width to auto and tweaking m ...

New ways to style Links in NextJs 13

I am relatively new to Next.js and I am attempting to create a menu with a hover effect using the following code: import Link from 'next/link'; const MenuItem = ({ href, label }) => ( <Link href={href} className="menu-item"> ...

Avoiding the selection of HTML canvas objects

I am currently working on customizing my homepage with an interactive animation. However, I am facing some challenges in integrating it seamlessly into the page. You can view the progress at . My main issue is preventing the canvas object from being select ...

Text situated under a stylish Css bar

I want to create a bar chart to display the number of surveys that are completed, not started, and not available. The bars should also show the actual numbers below them, similar to this example https://i.sstatic.net/8miIs.png Currently, I have the fol ...

Safari and Chrome render flexbox layouts differently

I am encountering a frustrating issue with my font "History Maps" centering within its title box on Chrome using flexbox, but failing to do so in the Safari browser. For reference: Below is the HTML and CSS code in question: /* Parent elements */ ...

Switching from Bootstrap's traditional "loading" style progress bar to a more minimal dot style progress bar

Currently, I am implementing a survey progress bar using Bootstrap's "loading" style. Here is the code for the progress bar: <div class="progress" style="height: 20px;"> <div class="progress-bar" role="pro ...

Tips for maintaining the particles.js interaction while ensuring it stays under other elements

I have incorporated particle.js as a background element. By adjusting the z-index, I successfully positioned it in the background. While exploring solutions on the Github issues page, I came across a suggestion involving the z-index and this code snippet: ...