skip to Main Content

The pause/play button moves when I click it. How do I fix that?

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 button moves up and down when you press it, and moves the text next to it as well.
I think this has something to do with how I am drawing the button in the different states, but I am new to CSS/HTML so I don’t know what to do.
This is an edited version of this pause button

If you have a better/correct way of doing a pause/play button, that would work as well.

2

Answers


  1. Set the vertical alignment of the button to middle (or top):

    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);
      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>
    Login or Signup to reply.
  2. Add vertical-align: middle; to .button class.

    Like 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;
      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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search