Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In HTML, should block level elements always wrap <a> tags?

Tags:

html

css

xhtml

In HTML, should block level elements always wrap <a> tags? What if it's necessary for the tag to wrap the block level element to ensure the correct styles are applied? For example can this

<h3><a href="/">Your Header</a></h3>

be this

<a href="/"><h3>Your Header</h3></a>

NB: I'm going for the latter approach to ensure the correct styles are applied (I'm working with legacy code that is just not worth re-working for this one element), but while doing this I was interested to know what the community's views are.

And yes I have read this question In HTML which way round should a and h1 be nested? but I'm not sure if a different or more flexible rule applies for <h3> tags.

Taking in the comments below and looking again at the code, I have two possible solutions:

  1. Wrap the <h3> elements with <a> elements (ok in HTML5)
  2. Add .class a to the CSS so that it inherits parent div styles as follows:

HTML

<div class="class">
    <h3><a href="/">Your Header</a></h3>
</div>

CSS

.class, .class a {
    width:296px;
    height:46px;
    overflow:hidden;
    color:#FE5815;
}
like image 323
br3w5 Avatar asked Jul 11 '12 13:07

br3w5


People also ask

Can I wrap block-level elements with a tags?

A simple A element wrapping DIV s, H1 's, and P 's. Note that you may not wrap A elements with larger A elements. What Do You Think? I have mixed feelings about wrapping block-level elements with A tags.

What is block level element in HTML?

Block-level Elements. A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can). The <div> element is a block-level element.

Does HTML 5 allow wrapping elements in an ‘a’?

I’m so glad HTML 5 allows wrapping most elements in an ‘a’. Good discussion, btw! Mixed feelings about what??? 10 years ago or not that long ago people had mixed feelings about DIVs agains TR and TD and xHtml against HTML,;now it’s ridiculous to even mention old approaches…

What is the difference between inline and block-level elements in HTML?

A block-level element always starts on a new line. A block-level element always takes up the full width available (stretches out to the left and right as far as it can). A block level element has a top and a bottom margin, whereas an inline element does not. The <div> element is a block-level element. An inline element does not start on a new line.


2 Answers

In this context, it is absolutely allowed for the a element to contain the h3 element, at least according to HTML5.

An a element is known as a "transparent" element: it may contain whatever its parent element may contain. The only criterion is that it may not contain any other "interactive" content, e.g. other a elements, button elements, iframe elements. In this case, presuming that the first version is allowed, the second version is also allowed under HTML5.

This is the page in the HTML5 spec that specifies this. It takes a little interpretation to understand, unfortunately...

like image 165
lonesomeday Avatar answered Nov 15 '22 21:11

lonesomeday


Note that there is one case in HTML5 where

<h3><a href="/">Your Header</a></h3>

would be valid, but

<a href="/"><h3>Your Header</h3></a>

would not, and that's when the parent of the <h3> element is an <hgroup> element.

The <hgroup> element can only have <h?> children, so while the transparent content model of the <a> element allows an <h3> to be its child, the <a> element remains invalid as a child of <hgroup>.

In this case

<hgroup>
  <h3>
    <a href="/">Your Header</a>
  </h3>
</hgroup> 

and

<a href="/">
  <hgroup>
    <h3>Your Header</h3>
  </hgroup>
</a>

are the only valid arrangements.

like image 32
Alohci Avatar answered Nov 15 '22 22:11

Alohci