htmlbuttonhyperlinkhreftel

Is it possible to have two href links (phone number and website links) based on screen size? html/css


<a href="contact.html" href="tel:5555555555">Contact</a>

I am trying to create a contact button that will take the user to a website link if the user is not on a mobile screen. However, if the user is on a screen smaller than say 1280 pixels ie. @media screen and (max-width: 1280px) {...then I want the contact button to call the number in the link.

Either href that I have there works fine but I'm trying to get both based on screen size. Is there a way to make this possible?

EDIT:

.button {
    -moz-appearance: none;
    -webkit-appearance: none;
    -ms-appearance: none;
    appearance: none;
    -moz-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    -webkit-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    -ms-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    background-image: -moz-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-image: -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-image: -ms-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-image: linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-color: darkgreen;
    border-radius: 5px;
    border: 0;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    padding: 0 1.5em;
    line-height: 2.75em;
    min-width: 9em;
    text-align: center;
    text-decoration: none;
    font-weight: 600;
    letter-spacing: -0.025em;
}

.button:hover {
        background-color: green;
        color: #fff !important;
    }
.button:active {
        background-color: green;
        color: #fff;
    }

#banner header .button {
            vertical-align: middle;
            margin-left: 1em;

        }

Solution

  • This can be done via CSS Media Queries (https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries)

    So basically, you'll have two links:

    .visible-on-mobile {
      display: none;
    }
    
    .visible-on-desktop {
      display: inline;
    }
    
    @media only screen and (max-width: 640px) {
      .visible-on-mobile {
        display: inline;
      }
      .visible-on-desktop {
        display: none;
      }
    }
    <div class="visible-on-mobile">
        <a href="tel:0000000000" class="button">Phone</a>
    </div>
    
    <div class="visible-on-desktop">
        <a href="mailto:hi@example.com" class="button">Email</a>
    </div>

    This will show the Phone link when the device width is less than or equal to 640px otherwise, the email link will be shown.

    Hope it helps!