How to create an HTML button that acts like a link?

后端 未结 30 3557
长发绾君心
长发绾君心 2020-11-21 04:18

I would like to create an HTML button that acts like a link. So, when you click the button, it redirects to a page. I would like it to be as accessible as possible.

相关标签:
30条回答
  • 2020-11-21 05:19

    People who have answered using <a></a> attributes on a <button></button> was helpful.

    BUT then recently, I encountered a problem when I used a link inside a <form></form>.

    The button is now regarded like/as a submit button (HTML5). I've tried working a way around, and have found this method.

    Create a CSS style button like the one below:

    .btn-style{
        border : solid 1px #0088cc;
        border-radius : 6px;
        moz-border-radius : 6px;
        -webkit-box-shadow : 0px 0px 2px rgba(0,0,0,1.0);
        -moz-box-shadow : 0px 0px 2px rgba(0,0,0,1.0);
        box-shadow : 0px 0px 2px rgba(0,0,0,1.0);
        font-size : 18px;
        color : #696869;
        padding : 1px 17px;
        background : #eeeeee;
        background : -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eeeeee), color-stop(49%,#eeeeee), color-stop(72%,#cccccc), color-stop(100%,#eeeeee));
        background : -moz-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
        background : -webkit-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
        background : -o-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
        background : -ms-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
        background : linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
        filter : progid:DXImageTransform.Microsoft.gradient( startColorstr='#eeeeee', endColorstr='#eeeeee',GradientType=0 );
    
    }
    

    Or create a new one here : CSS Button Generator

    And then create your link with a class tag named after the CSS style you have made:

    <a href='link.php' class='btn-style'>Link</a>
    

    Here's a fiddle:

    JS Fiddle

    0 讨论(0)
  • 2020-11-21 05:20

    If you are using an inside form, add the attribute type="reset" along with the button element. It will prevent the form action.

    <button type="reset" onclick="location.href='http://www.example.com'">
        www.example.com
    </button>
    
    0 讨论(0)
  • 2020-11-21 05:20

    @Nicolas,following worked for me as yours didn't have type="button" due to which it started behaving as submit type..since i already have one submit type.it didn't worked for me ....and now you can either add class to button or to <a> to get required layout:

    <a href="http://www.google.com/">
        <button type="button">Click here</button>
    </a>
    
    0 讨论(0)
  • 2020-11-21 05:21

    If you want to avoid having to use a form or an input and you're looking for a button-looking link, you can create good-looking button links with a div wrapper, an anchor and an h1 tag. You'd potentially want this so you can freely place the link-button around your page. This is especially useful for horizontally centering buttons and having vertically-centered text inside of them. Here's how:

    Your button will be comprised of three nested pieces: a div wrapper, an anchor, and an h1, like so:

    .link-button-wrapper {
        width: 200px;
        height: 40px;
        box-shadow: inset 0px 1px 0px 0px #ffffff;
        border-radius: 4px;
        background-color: #097BC0;
        box-shadow: 0px 2px 4px gray;
        display: block;
        border:1px solid #094BC0;
    }
    .link-button-wrapper > a {
        display: inline-table;
        cursor: pointer;
        text-decoration: none;
        height: 100%;
        width:100%;
    }
    .link-button-wrapper > a > h1 {
        margin: 0 auto;
        display: table-cell;
        vertical-align: middle;
        color: #f7f8f8;
        font-size: 18px;
        font-family: cabinregular;
        text-align: center;
    }
    <div class="link-button-wrapper">
        <a href="your/link/here">
            <h1>Button!</h1>
        </a>
    </div>

    Here's a jsFiddle to check it out and play around with it.

    Benefits of this setup: 1. Making the div wrapper display: block makes it easy to center (using margin: 0 auto) and position (while an <a> is inline and harder to positionand not possible to center).

    1. You could just make the <a> display:block, move it around, and style it as a button, but then vertically aligning text inside of it becomes hard.

    2. This allows you to make the <a> display: inline-table and the <h1> display: table-cell, which allows you to use vertical-align: middle on the <h1> and center it vertically (which is always nice on a button). Yes, you could use padding, but if you want your button to dynamically resize, that won't be as clean.

    3. Sometimes when you embed an <a> within a div, only the text is clickable, this setup makes the whole button clickable.

    4. You don't have to deal with forms if you're just trying to move to another page. Forms are meant for inputting information, and they should be reserved for that.

    5. Allows you to cleanly separte the button styling and text styling from each other (stretch advantage? Sure, but CSS can get nasty-looking so it's nice to decompose it).

    It definitely made my life easier styling a mobile website for variable-sized screens.

    0 讨论(0)
  • 2020-11-21 05:22

    In JavaScript

    setLocation(base: string) {
      window.location.href = base;
    }
    

    In HTML

    <button onclick="setLocation('/<whatever>')>GO</button>"
    
    0 讨论(0)
  • 2020-11-21 05:23

    HTML

    The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute.

    <form action="https://google.com">
        <input type="submit" value="Go to Google" />
    </form>
    

    If necessary, set CSS display: inline; on the form to keep it in the flow with the surrounding text. Instead of <input type="submit"> in above example, you can also use <button type="submit">. The only difference is that the <button> element allows children.

    You'd intuitively expect to be able to use <button href="https://google.com"> analogous with the <a> element, but unfortunately no, this attribute does not exist according to HTML specification.

    CSS

    If CSS is allowed, simply use an <a> which you style to look like a button using among others the appearance property (it's only not supported in Internet Explorer).

    <a href="https://google.com" class="button">Go to Google</a>
    
    a.button {
        -webkit-appearance: button;
        -moz-appearance: button;
        appearance: button;
    
        text-decoration: none;
        color: initial;
    }
    

    Or pick one of those many CSS libraries like Bootstrap.

    <a href="https://google.com" class="btn btn-primary">Go to Google</a>
    

    JavaScript

    If JavaScript is allowed, set the window.location.href.

    <input type="button" onclick="location.href='https://google.com';" value="Go to Google" />
    

    Instead of <input type="button"> in above example, you can also use <button>. The only difference is that the <button> element allows children.

    0 讨论(0)
提交回复
热议问题