tags:

views:

109

answers:

3

Basically, I like the way that <input type="submit"> is styled, with the clickable button when you add a little CSS. However, regular buttons are not styled as such, they have no such clickability without some major CSS or JS, and you have to use images.

I made submit buttons into links, by using the form action, but this requires me to make a new form for each button. How can I find a happy medium? Using multiple forms is causing problems in my styling that I can't seem to fix unless I find another way to make buttons that are links in HTML, that have a default style that makes them have a pressed state (and I don't mean browser default settings).

Any ideas?

+1  A: 

You have three options:

  • Style links to look like buttons using CSS.

    Just look at the light blue "tags" under your question.

    It is possible, even to give them a depressed appearance when clicked (using pseudo-classes like :active), without any scripting. Lots of major sites, such as Google, are starting to make buttons out of CSS styles these days anyway, scripting or not.

  • Put a separate <form> element around each one.

    As you mentioned in the question. Easy and will definitely work without Javascript (or even CSS). But it adds a little extra code which may look untidy.

  • Rely on Javascript.

    Which is what you said you didn't want to do.

thomasrutter
Does making separate <form> elements cause any sort of spacing? I've tried to make my two buttons appear side-by-side but the stylesheets I'm using (I didn't write them) are very numerous and must have some sort of margins or padding somewhere that causes them to be on top of each other. Any way I can override this or find out where the styling is held for this particular element?
Andrei Korchagin
You'd use CSS to remove the spacing. Install Firebug and use "Inspect Element" to find out where the existing spacing is coming from.
thomasrutter
+1  A: 
<a href="#"><button>Link Text</button></a>

You asked for a link that looks like a button, so use a link and a button :-) This will preserve default browser button styling. The button by itself does nothing, but clicking it activates its parent link.

edeverett
A: 

The 3 easiest ways IMHO are

1: you create an image of a button and put a href around it. (Not a good way, you lose flexibility and will provide a lot of difficulties and problems.)

2 (The easiest one) -> JQuery

<input type="submit" someattribute="http://yoururl/index.php"&gt;

  $('button[type=submit] .default').click(function(){
     window.location = $(this).attr("someattribute");
     return false; //otherwise it will send a button submit to the server

   });  

3 (also easy but I prefer previous one):

<INPUT TYPE=BUTTON OnClick="somefunction("http://yoururl");return false" VALUE="somevalue">

$fn.somefunction= function(url) {
    window.location = url;
};
Nealv
The image option will not have the behaviours of a normal browser button (expanding according to text content, depressing, etc.)The Javascript option is overkill and leaves a useless button in the page if Javascript fails to load or is turned off.
edeverett
if you know some css and cutting the image button WILL stretch and WILL have the behaviour of normak browser buttons.
Nealv
For my sins I've done this plenty in the past, but coping with things like text scaling and the different browser's default button styles make styling a link as a fake button a time consuming compromise. It can work in limited circumstances but is not something I'd recommend.
edeverett
I wouldn't recommend it either, it was only an alternative :) but ok I'll leave it by that
Nealv