views:

49

answers:

5

My google fu is not up to scratch this evening. What are the valid html elements, if any, that can be contained within a <a> tag?

<a> ?? </a>
+3  A: 

Inline elements ( a, span, strong, em among others ) can contain other inline elements and text nodes. An anchor can contain a span, which can contain a text node.

Generally, block-level elements may contain inline elements and other block-level elements. Generally, inline elements may contain only data and other inline elements. Inherent in this structural distinction is the idea that block elements create "larger" structures than inline elements.

From http://www.w3.org/TR/html401/struct/global.htmlelements.

As noted in other answers, you can't nest an a in an a.

meder
so `<a><span>ble</span></a` is valid since they are both inline elements, right?
Ahmad
You can find out if you make a html document and validate @ http://validator.w3.org/ :)
meder
@meder - question was actually inspired by another SO question :). I'm not actually doing this and somehow dont ever see myself using a `<span>` within an `<a>`.. Thanks.. another 6 minutes to go before i can mark this as accepted.
Ahmad
A: 

An <a> tag can contain any Inline Element besides another <a> tag.

Mike Sherov
A: 

It can contain plain text and inline elements. Inline elements are following:

TT | I | B | BIG | SMALL | EM | STRONG | DFN | CODE | SAMP | 
KBD | VAR | CITE | ABBR | ACRONYM | A | IMG | OBJECT | BR | 
SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO

But A can not be nested in another A and nesting SCRIPT doesn't make senese.

rarouš
You forgot the [formctrl](http://www.w3.org/TR/html401/sgml/dtd.html#formctrl) elements there. (Hell, who would use a `select` inside an `a`?)
Pumbaa80
I forgot them by intention :) There is no need to nesting forms elements in anchor. Maybe without href attribute, but who use it today? :)
rarouš
+1  A: 

See the anchor section of the specification.

<!ELEMENT A - - (%inline;)* -(A)       -- anchor -->

The relevant section is (%inline;)* -(A), which means "Anything in the group %inline excluding A elements". %inline is hyperlinked to make it easier for you to expand it.

David Dorward
A: 

An anchor tag is an inline element, so it can contain other inline elements (except other anchor tags).

If you want to put a block element inside an anchor, you have to use an inline element and turn it into a block element using CSS, along with the anchor tag itself.

Example:

<a href="page.html" class="blocklink"><span>eat me</span></a>

CSS:

.blocklink { display: block; }
.blocklink span { display: block; }
Guffa