When I click the button, it shifts. What steps should I take to correct this issue?

I have a problem with the pause/play button moving when clicked. How can I fix this?

const btn = document.querySelector(".button");
if (btn === null) {
  throw new Error('could not find button');
}
btn.addEventListener("click", function() {
  btn.classList.toggle("paused");
  return false;
});
:root {
  --pauseButtonhight: 16px;
  --pauseButtonwidth: 13px;
  --pauseButtonborder: 8px;
}

.button {
  border: 0;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  padding: 0;
  height: var(--pauseButtonhight);
  border-color: transparent transparent transparent #7083d8;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
}

.button.paused {
  padding-top: 8px;
  border-style: double;
  border-width: 0px 0 0px var(--pauseButtonwidth);
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}
<div>
  <label for="playspersecond">Updates per second: </label>
  <input type="number" id="playspersecond" value="5" min="0" max="10" />
  <button class="button" id="play"></button>
</div>

The issue is that the button moves up and down when pressed, affecting the surrounding text as well. I suspect there's an error in how I'm styling the button in different states, but being new to CSS/HTML, I'm unsure of what to do next. This design is based on this pause button

If you know of a more efficient way to create a pause/play button, please let me know.

Answer №1

Adjust the button's vertical alignment to middle (or top):

const btn = document.querySelector(".button");
if (btn === null) {
  throw new Error('Button not found');
}
btn.addEventListener("click", function() {
  btn.classList.toggle("paused");
  return false;
});
:root {
  --pauseButtonhight: 16px;
  --pauseButtonwidth: 13px;
  --pauseButtonborder: 8px;
}

.button {
  border: 0;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  padding: 0;
  height: var(--pauseButtonhight);
  border-color: transparent transparent transparent #7083d8;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
  vertical-align: middle;
}

.button.paused {
  padding-top: 8px;
  border-style: double;
  border-width: 0px 0 0px var(--pauseButtonwidth);
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}
<div>
  <label for="playspersecond">Updates per second: </label>
  <input type="number" id="playspersecond" value="5" min="0" max="10" />
  <button class="button" id="play"></button>
</div>

Answer №2

Incorporate the style vertical-align: middle; into the .button class.

Here's an example:

const btn = document.querySelector(".button");
if (btn === null) {
  throw new Error('could not find button');
}
btn.addEventListener("click", function() {
  btn.classList.toggle("paused");
  return false;
});
:root {
  --pauseButtonhight: 16px;
  --pauseButtonwidth: 13px;
  --pauseButtonborder: 8px;
}

.button {
  border: 0;
  vertical-align: middle;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  padding: 0;
  height: var(--pauseButtonhight);
  border-color: transparent transparent transparent #7083d8;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: var(--pauseButtonborder) 0 var(--pauseButtonborder) var(--pauseButtonwidth);
}

.button.paused {
  padding-top: 8px;
  border-style: double;
  border-width: 0px 0 0px var(--pauseButtonwidth);
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}
<div>
  <label for="playspersecond">Updates per second: </label>
  <input type="number" id="playspersecond" value="5" min="0" max="10" />
  <button class="button" id="play"></button>
</div>

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

The reference to the Material UI component is not functioning

I am currently working on a chat application and have implemented Material UI TextField for user message input. However, I am facing an issue with referencing it. After researching, I found out that Material UI does not support refs. Despite this limitatio ...

Is it possible to toggle CSS classes worldwide?

One of my CSS techniques involves outlining every div in red, which is helpful for debugging alignment of elements. In my app, I have a button that currently toggles various debugging features. I'm interested in incorporating the toggling of these re ...

Having trouble understanding the response message after a post request in Sweet Alert using ajax

I'm looking to implement sweet alert JS for displaying alerts on an ajax form submission. After some investigation, I've managed to set up the ajax post functionality, but I'm struggling to integrate it smoothly with sweet alert for displayi ...

Switch the background color alternately from red to green every second

Need help with a webpage that changes the background color every second using JavaScript. The issue lies in figuring out how to correctly change the variable within the function. Here's an example of the code: <!DOCTYPE html> <html> ...

What is the best way to capture the dynamic text "3204255" from the html content using Selenium?

My goal is to extract a number from a specific div element during the execution of my test cases. I am able to locate the element, but I am struggling to retrieve the number and print it to the console. This is what I have tried: WebElement webelement = ...

Methods for dynamically altering the background color of a parent component from a child component

Currently, I am working on a T-shirt design application using fabric.js within reactjs. I have encountered an issue when attempting to change the color of the t-shirt. My goal is to allow the color of the T-shirt to be changed from the child component but ...

Troubleshooting a Selected Menu Problem in jQuery CSS

Hello, I have implemented a jQuery script to change the color of a menu item when it is selected: .selected{ background-color: red; } $("#nav-container>li").click(function(){ $(this).addClass('selected') .siblings() ...

Expanding the navbar with Bootstrap 3.0 by using the navbar-brand class

I'm facing an issue with the navbar-brand while adding my logo. I am using the latest version of bootstrap CSS from getbootstrap.com, and the problem is also evident there: The navbar becomes slightly oversized when the logo is included. However, if I ...

What could be causing my React Router to display empty pages?

I've been working with the latest version of create-react-app and have been struggling to display the pages within my app. I've tried both functional components and class-based components, but nothing seems to work. I've wrapped the content ...

Dynamic content is loaded on the page using AJAX before refreshing to retrieve new

I am using the FullPage.js plugin and attempting to enable navigation between pages using AJAX. While using .load();, I am able to retrieve the desired page. However, it seems that I need to reload fullpage.js since the loaded page is not active and the s ...

What is the best way to add a directional hover effect to a filter?

I am attempting to implement a sliding hover filter effect on an image. The original filter I have is set to grayscale(1). What I want to achieve is for the filter to transition directionally instead of having a slider overlay effect, in order to change i ...

How can CSS be used to format an unordered list around images?

Can anyone suggest ways to improve the formatting of this HTML list when wrapping around a left-floated image? I often encounter this small problem while working on client websites: The image has a right-margin, but it doesn't seem to affect the ... ...

Issue with MUI Data Grid sorting: Data Grid sortComparator function returning undefined

I'm attempting to organize data with nested values in a data grid, but I keep receiving 'undefined' on the sortComparator of Data Grid Columns. Code: Column Data Setup: { headerName: 'Title', field: `${this.props.type} ...

Implement a responsive CSS division that simulates a two-column table

In my layout, I have three row divs: header, content, and footer. My goal is to create a table-like format within the content div to display information. Here's an example: .itemwrapper{width:600px;} .picture{width:150px;float:left;} .item{width:45 ...

The edge of the 'parent' element is obscured by the :after element

I am trying to create a transparent outlined button with a shadow in the shape of the button. However, I am facing an issue where the border of the button appears behind the :after element. You can see the problem below. Adding overflow: hidden to the tog ...

The background image is trapping my text and preventing it from moving forward

As a beginner developer embarking on my first web page project, I've encountered a challenge. The text I inserted into a flexbox refuses to align beneath the image. .title { text-align: center; width: 500px; margin-left: auto; margin-right: a ...

Troubleshooting JavaScript If-Else Statements

Having a bit of trouble with my if else structure. When I enter the correct star name like "Vega", it incorrectly shows me "Error" instead of the expected result "Lyra". Here's my code snippet: var stars = ["Polaris", "Aldebaran", "Deneb", ...

Steps for creating a sleek vertical slider with transitional effects using JavaScript and HTML

Take a look at my code snippet: <table> <tr> <td onclick="shownav()">Equations of Circle <div id="myDownnav" class="sidenav"> <a>General Info</a> <a>In Pola ...

CSS failing to acknowledge specified div background

After successfully configuring the featured-top and footer elements in my CSS, I encountered an issue with a persistent white line appearing between them. Despite inspecting the elements in Google Chrome, I couldn't identify the source of this problem ...

Tips for separating these two words onto two separate lines

I created this box using Angular Material, but I am having trouble breaking the words "1349" and "New Feedback" into two lines. Here's an image included in my design. Any tips on accomplishing this in Angular Material? Thank you! <style> . ...