0

Can I use a div element inside an a tag?

In FF, Opera and Safari print in screen very well, but when I use the inspect (or firebug), it doesn't find the element insight of the a tag.

For example, a typical HTML:

<a href="#">
<div>lorem ispum</div>
<div>lorem ispum</div>
</a>

Is it an error?

Kunj
  • 1,980
  • 2
  • 22
  • 34

2 Answers2

0

In HTML5, you can, but in HTML4, it will be regarded as invalid. Why don't you consider using <span> element and set the display to be block? It would work and be valid at the same time.

Now, that doesn't mean you can't use a <div> inside an <a>, it won't be valid, but it will still work. Quoting from the docs, "Generally, inline elements may contain only data and other inline elements."

By the way, contrary to what you said, it does find the element inside the tag on the version of Firefox I am using, which is the latest. In conclusion, with HTML5, you can.

enter image description here

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
-1

The example code is not correct.

A element is inline and Div element is a block.

It is not correct to put DIV element in A element.

Instead put SPAN

<a href="#">
<span>lorem ispum</span>
<span>lorem ispum</span>
</a>
user43506
  • 155
  • 3
  • 11
  • It's perfectly valid in HTML5. The only restrictions on `a` element descendants is that they must not be interactive elements. When making direct claims like "it is not correct..." you should always provide a source even if you're 100% confident in your answer. – James Donnelly Jan 08 '14 at 11:48
  • I downvoted this because it is inaccurate at best and the suggested solution is unacceptable and does not answer the question – CodingIntrigue Jan 08 '14 at 11:48