Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting <a> inside <button> doesn't work in Firefox

Tags:

html

firefox

I have a <button> with a hyperlink tag inside, looks like this:

<button class="btn"><a href="#"></a></button>

This works well in Chrome and Safari, BUT doesn't work in Firefox (ver 20 tested).

What's wrong?

like image 605
cjmling Avatar asked Apr 29 '13 14:04

cjmling


People also ask

Can I nest an anchor inside a button?

Using button tag inside <a> tag: This method create a button inside anchor tag. The anchor tag redirect the web page into the given location. Adding styles as button to a link: This method create a simple anchor tag link and then apply some CSS property to makes it like a button.

Can I put an A tag inside a button?

Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.). That is not possible with a button created with the <input> element! Tip: Always specify the type attribute for a <button> element, to tell browsers what type of button it is.

Can I put an A tag inside a button tag in HTML?

Having an anchor inside a button tag is not correct HTML afaik. It would seem better to simply style the anchor tag to look like the button style you want and get rid of the button tag altogether.

Can you put a link inside a button?

No, you can't. HTML forbids nesting <button> inside <a> .


3 Answers

To make it work in all browser, Firefox too you have to change it to

<a href="#"><button class="btn"></button></a>

or as suggested by Billy Moat in case of bootstrap there was no need of <button> you could just do

<a href="#" class="btn">GO</a>
like image 69
cjmling Avatar answered Oct 06 '22 16:10

cjmling


Probably better to just do this:

<a href="#" class="btn">Go!</a>
like image 21
Billy Moat Avatar answered Oct 06 '22 15:10

Billy Moat


This issue is happening in FF and IE(< 10). The browser simply doesn't like the tag button when it's used as a link.

Quick solution in bootstrap is to use and give a class of btn btn-default (or your choice of button style).

However, you can use in a form (submit button for instance) and you shouldn't have an issue.

like image 3
ToTech Avatar answered Oct 06 '22 17:10

ToTech