Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I nest a <button> element inside an <a> using HTML5?

Tags:

html

I am doing the following:

 <a href="www.stackoverflow.com">
   <button disabled="disabled" >ABC</button>
 </a>  

This works good but I get a HTML5 validation error that says "Element 'button' must not be nested within element 'a button'.

Can anyone give me advice on what I should do?

like image 493
Marie Avatar asked Jun 18 '11 04:06

Marie


People also ask

Can you put a button inside a button HTML?

It is not valid to put a <button> inside a <button> element. In the W3C recomendation for the button element you can read: Content model: Phrasing content, but there must be no interactive content descendant.

How do I anchor a button in HTML?

The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute. 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"> .

Can a button have an href?

HTML buttons cannot have href attribute if they are created using button <button> </button> HTML tags. However, you can use href attribute if you create the buttons using link <a> </a> HTML tags.

Can you put a class on a button in HTML?

Classes (i.e. classnames) are used for styling the button element. Multiple classnames are separated by a space. JavaScript uses classes to access elements by classname. Tip: class is a global attribute that can be applied to any HTML element.


3 Answers

No, it isn't valid HTML5 according to the HTML5 Spec Document from W3C:

Content model: Transparent, but there must be no interactive content descendant.

The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).

In other words, you can nest any elements inside an <a> except the following:

  • <a>

  • <audio> (if the controls attribute is present)

  • <button>

  • <details>

  • <embed>

  • <iframe>

  • <img> (if the usemap attribute is present)

  • <input> (if the type attribute is not in the hidden state)

  • <keygen>

  • <label>

  • <menu> (if the type attribute is in the toolbar state)

  • <object> (if the usemap attribute is present)

  • <select>

  • <textarea>

  • <video> (if the controls attribute is present)


If you are trying to have a button that links to somewhere, wrap that button inside a <form> tag as such:

<form style="display: inline" action="http://example.com/" method="get">
  <button>Visit Website</button>
</form>

However, if your <button> tag is styled using CSS and doesn't look like the system's widget... Do yourself a favor, create a new class for your <a> tag and style it the same way.

like image 163
Andrew Moore Avatar answered Oct 19 '22 10:10

Andrew Moore


If you're using Bootstrap 3, this works quite well

<a href="#" class="btn btn-primary btn-lg active" role="button">Primary link</a>
<a href="#" class="btn btn-default btn-lg active" role="button">Link</a>
like image 23
user2197018 Avatar answered Oct 19 '22 09:10

user2197018


I've just jumped into the same issue and I solved it substituting 'button' tag to 'span' tag. In my case I'm using bootstrap. This is how it looks like:

<a href="#register"> 
    <span class="btn btn-default btn-lg">
        Subscribe
    </span>
</a> 
like image 15
gobeltri Avatar answered Oct 19 '22 10:10

gobeltri