Disguise a button as a hyperlink

Source: Internet
Author: User

Original http://natbat.net/2009/Jun/10/styling-buttons-as-links/

A common mistake that your developers make is to use a link to trigger an action on the server, for example deleting an item from a shopping basket or adding something to your favorites. both of these examples are actions that modify state on the server and shoshould therefore be wrongly med using'post'.

However, sometimes even developers who know that is wrong to use a link where they shoshould be using a form, get sucked in to doing so when the design requires a button to look like a link.

Please note that I am definitely not encouraging the redesign of Button elements to look like links. I believe that we shouldn't mess too much with browser defaults for functional things like Form Controls, scroll bars and the like. that said, sometimes you just have to build what your designer tells you.

It actually isn' t hard to make a submit button look like a link using CSS so you shoshould never find yourself in a position where you have to sacriice forms for links purely for the sake of design.

Firsly the markup: although you can use an inputtype="submit"As a submit element and this example wocould work in the same way, the button element is, in my opinion, a much better option. it is really flexible and can have a variety of different elements nested inside if you so choose, from simple text with an image right through to headings and paragraphs. this article on buttons by Aaron Gustafson back in 2006 is still fairly relevant today and explains some of the uses the humble button element can be put.

Another useful article also from a while ago explains the techniques that Wufoo use to style links to look like buttons. the really important thing to take away from this article is that puttingoverflow: visibleOn a button fixes the crazy width issue that IE likes to deal us.

I have posted a simple demo for you to follow along in your browser. For the purposes of this example the markup will be:

<Form action = "#" method = "Post"> <br/> <p> <button type = "Submit" class = "Link"> <span> hello there I am A button </span> </button> </P> <br/> </form> </P> <p> <a href = "#" mce_href = "#" That's nice, I am a link </a> </P> <br/>

 

The basic page in this demo has the following styles applied to the links and body:

Body {<br/> font-family: "verdana" Sans-serif; <br/>}< br/> A: Link, <br/>: visited {<br/> color: Blue; <br/>}< br/> A: hover, <br/> A: focus, <br/>: active {<br/> color: black; <br/>}

 

 

Next I add the magical incantation to kick the width in IE for all buttons:

Button {<br/> overflow: visible; <br/> width: auto; <br/>

 

I have added a class of link to the button that I wish to style as a link element and the basic styles that I apply to this are the color and font family (the button seems to inherit system font settings ), as well as over-riding the button defaults with regards to border, margin, padding and background.

The default cursor for button elements is a regular arrow. I normally setcursor: pointerFor all button elements to ensure the user knows they are clickable. This makes even more sense for buttons that are pretending to be links.

Button. link {<br/> font-family: "verdana" Sans-serif; <br/> font-size: 1em; <br/> text-align: left; <br/> color: Blue; <br/> Background: none; <br/> margin: 0; <br/> padding: 0; <br/> border: none; <br/> cursor: pointer; <br/>}

 

 

Interestingly, Mozilla won't let you select the text of the button element like other browsers will, so to override this and enforce that the user can select the text of our psuedo-link, you can apply the following as well:

-Moz-user-select: text;

You can also choose to override all your other button styles if there are any.

Now you almost have a link. those of you paying close attention earlier on will have noticed an as yet unexplained inner span to our button. this is because it does not appear to be possible to set text-decoration on a button directly and depending on how you have your link styles set this is something you are likely to want to do.

The text-Decoration: underline rule is actually applied to the span on hover or focus of the button. this way is more flexible if you choose at some point to add or remove an underline on hover.

Button. link span {<br/> text-Decoration: underline; <br/>}< br/> button. link: hover span, <br/> button. link: Focus span {<br/> color: black; <br/>}

 

 

Naturally everybody's'Favorite'Browser, Internet Explorer six will not display the hover effect on your link because it Doesn 't'do 'hover on arbitrary elements, only on actual links. you can fake this effect with JavaScript if you really wish, adding a class on hover and removing it on mousout. other limitations of this technique are that selection of text looks a bit dodgy in all versions of IE.

My example above is very simple. if you wath a button that appears in flow with text as a link, for example the delete link on a shopping basket item in this example, you will also need to apply'display:inline'To both the form and any block level elements inside.

So there you have it, no excuses now-go forth and use the correct HTTP method!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.