2

I have two pages made using HTML and I use the following standards

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">

The two pages are index.html, the other is about.html. I create a logo inside the index page as the following

HTML markup of Index.html

<div class="logo"></div>

in CSS I do the following

.logo {
background: url ('../images/logo.png') no-repeat;
height: 300px;
width: 100px;
}

In about.html page I write the following html markup

<a href="index.html">
   <div class="logo"></div>
</a>

The above markup is working fine, but when I use the w3c validator it gives me warning as it's not a standard markup.

My question is how can I make the html markup fits with the standards and gives the same result?

HTML Man
  • 937
  • 4
  • 16
  • 40

6 Answers6

3

You should do this rather:

<a href="index.html" class="logo"></a>

And then your css will apply directly to the a instead of to a nested empty div. You'll need to add display: block; to the CSS though because a has a different default display.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
3

You have two choices if you need to keep this markup structure:

  1. Use the HTML5 doctype, where block level elements inside anchors like <a><div>this</div></a> are valid and legal.

    The HTML5 doctype is: <!doctype html>

    Just bear in mind that there is currently no official HTML5 validation service.

  2. Use an inline element like <span> for the logo:

    <a href="index.html">
       <span class="logo"></span>
    </a>
    

If you use #2, just add display:block to your CSS for .logo

Depending on your design goals and other markup, you may very well not need the inner element at all and can simply style the <a> element itself by giving it class="logo".

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
2

The problem you're seeing in the validator is because the <div> is a block-level tag, whereas the <a> tag is an inline tag.

Inline tags cannot contain block-level tags, which is why the validator is complaining.

In fact, this rule is widely ignored, because otherwise a lot of things become quite hard to make into links. In addition, it only applies to older HTML and xhtml versions (xhtml is particularly fussy about it); the current HTML5 standard does allow it, because they saw the problems caused by having the rule.

So the short answer is to use the HTML doctype (<!DOCTYPE HTML>)instead, and your problem will disappear.

The 'correct' answer is to style the <a> tag with display:block; or display:inline-block; so that it can then contain block-level elements. This will make it correct syntactically. However this doesn't usually satisfy the validator, as it just looks at the default display types, not CSS.

Alternatively, switch the <div> for a <span>. This will satisfy the validator, and you can still use display:block and friends to make it work like a <div>.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

Instead of using the logo image as a background put an tag inside your DIV. You cannot wrap an tag around a block element like a DIV.

<a href="index.html">
   <div class="logo">
      <a href='gosomewhere.html'>
         <img src='mylogo.png'alt='' title=''/>
      </a>
   </div>
xXPhenom22Xx
  • 1,265
  • 5
  • 29
  • 63
  • Thanks, I know that, but imagine i have 20 pages using html and your code a little bit long and a little bit noise when you read it from another one else – HTML Man Mar 03 '12 at 17:27
  • Agreed, wasnt sure if you wanted to keep the DIV structure. Also adding the image via the CSS you lose the ability to add TITLE and ALT tags which can be important for SEO and Accessibility, for instance images of products your selling, etc.. I guess it all depends on what the images are. – xXPhenom22Xx Mar 03 '12 at 17:38
  • yes you are totally right, but i use that only for logo, i think no problem at all if no one with disability cannot read the logo or my page have a broken link for logo. – HTML Man Mar 03 '12 at 17:59
  • also i can use title with tag – HTML Man Mar 03 '12 at 18:05
  • Correct, but search engines will look at that differently than an tag with a TITLE and ALT tag. For your need, applying the image via a CSS class is the way to go for a logo, but if you were listing products or other things you would want indexed by the Search Engines then its much better semantics to use an tag with a TITLE and ALT tag. – xXPhenom22Xx Mar 03 '12 at 18:10
  • Yes, you are correct, and i will keep that in my mind. Thanks – HTML Man Mar 03 '12 at 18:20
0

Links (a tags) are inline elements, meaning multiple elements fit on the same line, while divs are block elements, which mean only one per line. It is invalid HTML 4.01 to insert a block-style element inside an inline-style element, since that prevents the inline element from actually being rendered as inline.

For more info on inline and block elements, see this page: http://www.webdesignfromscratch.com/html-css/css-block-and-inline/

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82
0

No it not valid to put and Div tag inside an Anchor Tag According to section 4 of W3c standards.

An anchor tag is an iline element and Div is a block level element. You cannot put a block level element inside and inline element.

Here is another good SO discussion on your matter.
Is putting a div inside an anchor ever correct?

Community
  • 1
  • 1
Rizwan Mumtaz
  • 3,875
  • 2
  • 30
  • 31