-3

This might seem trivial, but I went through many proposed solutions and found nothing. The goal is to create an anchor <a> button with fixed height and width while keeping the text centered (vertically and horizontally) regardless of the text's width.

Example:

enter image description here

Mr Lister
  • 44,061
  • 15
  • 107
  • 146
Muaaz
  • 85
  • 9

1 Answers1

3

The best solution I reached was the following:

  1. Define a vertical center class:

    .text-vertical-center {
        display: table-cell;
        text-align: center;
        vertical-align: middle;}
    

    This served as the inner container of the button to center the text vertically and horizontally.

  2. Define a btn class:

    .btn {
        width: 300px;
        height: 75px;
        background-color: transparent;
        color: #000;
        border-radius: 0;
        border: #000 solid 1px;
        cursor: pointer;
        text-align: center;}
    

    The class btn servers for styling the button.

  3. I finally wrapped the inner div with an anchor tag <a> to link the button:

<a href="#">
  <div class="btn text-vertical-center">
      Button text
  </div>
</a>

This seems to work fine and is compatible with most browsers as far as I know. Please let me know if there are any better solutions.

JSFiddle Example: https://jsfiddle.net/MuaazO/ga4s540v/

Mr Lister
  • 44,061
  • 15
  • 107
  • 146
Muaaz
  • 85
  • 9