24

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.

TFennis
  • 1,353
  • 1
  • 13
  • 23
  • What are you trying to do? It seems there is a better way. What's wrong with using button normally? I don't understand what you're trying to accomplish here. are you trying to center your label? – Thomas Wright Apr 08 '13 at 17:44
  • http://www.w3schools.com/tags/tag_button.asp – Thomas Wright Apr 08 '13 at 17:44
  • @Thomas Wright The css classes have separate images to give the button rounded corners. We do not want to use the CSS property for this yet. – TFennis Apr 08 '13 at 17:46
  • 11
    It's not a bad question, what you're doing is actually widely used, (one of the benefits of the ` – RestingRobot Apr 08 '13 at 17:50
  • You could use a `div` for the button, display & submit with JS, then use a standard button in ` – JKirchartz Apr 08 '13 at 22:50
  • @TFennis - the reason we have js and css is to add functionality and capability to [x]html. Have you solved your problem yet? I would recommend that you not shy from such utilities. They have become an integral part of web design and development. Despite the general consensus in this thread, you can keep to w3c standards, xbrowser compatibility, and make it look exactly the way you want. Don't worry about that <.0001 a="" and="" be="" by="" color.="" css="" css.="" don="" experience="" have="" if="" jilted="" js="" off="" percent="" picture="" technical="" that="" the="" their="" turned="" was="" would=""> – Thomas Wright Apr 12 '13 at 18:17
  • How about just adding an `` tag inside your `button`? Validates and you get to create your button with rounded edges without `box-radius` and will work on any browser. – Wild Beard Mar 25 '15 at 19:29
  • 4
    Instead of *div*s, use *span*s with css of *display:inline-block*, so that you can size them. – Patanjali Oct 24 '16 at 00:43

4 Answers4

4

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.

Wild Beard
  • 2,881
  • 1
  • 11
  • 24
  • 1
    This is obviously a pretty old question of mine, and I'd love to approve an answer but... The reason we used 3 images is because the buttons needed to be able to have variable width. Using a single image plus stretching will give it weird corners. Althought I think your solution is probably possible if you just include 3 images in a – TFennis Jul 04 '16 at 11:55
2

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.

Community
  • 1
  • 1
RestingRobot
  • 2,888
  • 1
  • 23
  • 36
  • Like I commented above, we want buttons with rounded corners in browsers that do not yet support `border-radius` such as IE8. The 3 divs all have their own image. – TFennis Apr 08 '13 at 17:52
  • The input element does not allow rounded corners, and divs do not function as buttons for users without javascript. – TFennis Apr 08 '13 at 18:11
  • 2
    @TFennis You stated in your question that you were using Javascript. So why can't you use it here? – RestingRobot Apr 08 '13 at 18:26
2

Per the HTML standard a <button> may only contain Phrasing content. A div element is not a valid element for Phrasing Content.

andersra
  • 1,083
  • 2
  • 12
  • 32
1

If you're using a form, you could change the button to be a clickable div instead. For example:

<div role="button" onclick="$(this).closest('form').submit()">
    <!-- your child content here -->
</div>

Alternatively, if you know the form name or ID you could change the inline javascript to something like document.getElementById("form-id").submit()

Jonathan
  • 10,022
  • 8
  • 58
  • 72