Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a <button> element contain a <div>?

Tags:

My company is building a website and we had some problems with a JavaScript library not replacing something. We decided to throw our HTML in to the W3C validator and it informed us it's illegal to have a <div> tag inside a <button> tag.

<button class="button" type="submit">     <div class="buttonNormalLargeLeft"><!--#--></div>     <div class="buttonNormalLargeCenter">Search Flights</div>     <div class="buttonNormalLargeRight"><!--#--></div> </button> 

Results in:

Line 287, Column 46: Element div not allowed as child of element button in this context. (Suppressing further errors from this subtree.) 

Edit: To clarify what we're trying to do here. We want to make a button with rounded corners that doesn't rely on box-radius. We made 3 divs in the button element each has his own sprite to make it appear rounded and allow for different widths. Other resources state that the button element was created for users that wanted a button to contain sub elements such as images but divs appear to be invalid for some reason.

Why are divs not allowed inside button elements?

What is the desired solution to this issue?

Edit2:

Why not use input? Because inputs can't have the desired layout

Why not use divs? Because users without JavaScript won't be able to submit the form.

like image 717
TFennis Avatar asked Apr 08 '13 17:04

TFennis


People also ask

Can button element contain div?

html. twig div have a parent element button, which is not allowed. The issue is that a button can contain only Inline Elements, and div is a Block Element.

What elements can a button contain?

HTML button element can contain inline elements except for A, INPUT, SELECT, TEXTAREA, LABEL, BUTTON, and IFRAME.

Can you give a div an onClick?

To set an onClick listener on a div element in React:Set the onClick prop on the div. The function you pass to the prop will get called every time the div is clicked. You can access the div as event. currentTarget .

Can you put a div inside an anchor tag?

Nothing is wrong with placing a div inside a tag. In fact, you can place just about anything inside a tag as long as they are categorized as transparent , except that no descendant may be interactive content (eg: buttons or inputs) or an a element, and no descendant may have a specified tabindex attribute.


2 Answers

Well I posted a comment but no love.

You can achieve a W3C valid button by simply putting an image inside the button.

Fiddle

Granted you'll have to create your images and add the text to them. But since you've already created images for the corners that shouldn't be too hard.

Also image sprites are a great thing.

.test {    width: 258px;    height: 48px;    background: none;    border: 1px solid transparent;  }  .test:active {    outline: 0;  }  .test > img {    width: 258px;    height: 45px;    opacity: 1;    background: url('http://www.copygirlonline.com/wp-content/plugins/maxblogpress-subscribers-magnet/lib/include/popup1/images/btn/blue-button.png');    background-position: -3px 0px;  }  .test > img:hover {    background-position: -3px -50px;  }
<button class="test">    <img src="http://alistapart.com/d/cssdropshadows/img/shadowAlpha.png" />  </button>

If you want to stick with 3 images here's an updated version.

button {    margin: 0;    padding: 0;    border: none;  }
<button>    <img src="http://placehold.it/50x50" /><img src="http://placehold.it/50x50" /><img src="http://placehold.it/50x50" />  </button>

Also, without additional CSS, it seems you need to keep all of the <img /> tags on the same line or it treats the images as having space between them.

like image 94
Wild Beard Avatar answered Oct 11 '22 01:10

Wild Beard


If you want to use a custom button, you'll have to replace the button tag with a div or an input. It looks like a div is what you want in this case. You'll just have to add any necessary event handlers, (for your submit).

This might help clarify things a bit.

like image 43
RestingRobot Avatar answered Oct 11 '22 01:10

RestingRobot