<p><b>Click inside this frame, then press tab to see the focus states.</b></p>

<div class="button-container">
  <button class="custom-button example-1">Example 1</button>
</div>
  
<div class="button-container">
  <button class="custom-button example-2">Example 2</button>
</div>

<div class="button-container">
  <button class="custom-button example-3">Example 3</button>
</div>
  
<div class="button-container">
  <button class="custom-button example-4">Example 4</button>
</div>
/* Remove the dotted outline in Firefox */
.custom-button:not(.default)::-moz-focus-inner {
  border-style: none;
}

.example-1:focus {
  
  /* Adds a white border around the button, along with a blue glow. The white and blue have a color contrast ratio of at least 3:1, which means this will work against any background color. */
  box-shadow: 0 0 0 2px #ffffff, 0 0 3px 5px #3a97f9;
  
  /* NOTE: box-shadow is invisible in Windows high-contrast mode, so we need to add a transparent outline, which actually does show up in high-contrast mode. */
  outline: 2px dotted transparent;
  outline-offset: 2px;
}

.example-2:focus {
  /* Adds a white border along with a black outline. The contrast ratio between the black and white greater than 3:1, which means this will work against any background color. */
  border: 2px solid #FFFFFF;
  outline: 2px solid #000000;
  
  /* We're using `outline`, which doesn't support rounded corners. When we adjust the border-radius to match, the design looks more deliberate. */
  border-radius: 0;
}

.example-3:focus {
  /* Inverts the colors */
  background: #FFFFFF;
  color: #590CE5;
  
  /* Adds an underline so we're not relying on color alone to signify change */
  text-decoration: underline;
  
  /* This is okay to remove, ONLY because of the changes we made above. */
  outline: none;
}

.example-4:focus {
  /* Adds an underline. This is visible, but might be a bit too subtle to recognize. */
  text-decoration: underline;
  
  /* This is okay to remove, ONLY because of the changes we made above. */
  outline: none;
}

External CSS

  1. https://codepen.io/eschafer/pen/bJbaLB
  2. https://codepen.io/eschafer/pen/qvJvZR

External JavaScript

This Pen doesn't use any external JavaScript resources.