putting a block-level element inside an inline element (<div> inside <a>)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    putting a block-level element inside an inline element (<div> inside <a>)

    I'm trying to use the W3C Markup Validator to validate my HTML and I'm getting some errors:

    Code:
    document type does not allow element "div" here; missing one of "button", "map", "object", "ins", "del", "noscript" start-tag 
    …   <li ><a href="/" title=""><div>Home</div></a></li>
    Apparently I'm not allowed to put a block-level element, like div or table, inside an inline element, like span, or a. I tried moving the div tags to the outside of the link tag, but then it breaks my navigation links. It's supposed to look like this:



    When I move the div outside the link tag it looks like this:



    What can I do to fix this? I want the site to validate, but I want it to look good too.
  • Death Slaught
    Top Contributor
    • Aug 2007
    • 1137

    #2
    Change the div to a span and give it all of the stylings you gave the div. Make sure to give it a set width and height and then add the style

    Code:
    display: block;

    Thanks, Death

    Comment

    • drhowarddrfine
      Recognized Expert Expert
      • Sep 2006
      • 7434

      #3
      If you don't mind declaring HTML5, putting a block element inside an anchor is allowed.

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        I'd rather not declare HTML5, I think I'll do Death's suggestions, I just didn't think a span would work for this since a span is also an inline element. I didn't think I could set things like width and height.

        Comment

        • Death Slaught
          Top Contributor
          • Aug 2007
          • 1137

          #5
          You can give just about anything a width and height through CSS. Adding display: block is the trick. It makes the span display like a block element even though it's an inline element.


          Regards, Death

          Comment

          • drhowarddrfine
            Recognized Expert Expert
            • Sep 2006
            • 7434

            #6
            The difference is one of semantics more than anything else. span and div are both generic containers but span is considered inline while div is considered block level.

            You can, however, declare the span inline-block where you can set the height/width but I've lost track of the problem here.

            Comment

            • Death Slaught
              Top Contributor
              • Aug 2007
              • 1137

              #7
              He's wanting his navigation menu to appear the same without using a div inside of his anchors/list, so the validator won't fuss.

              Comment

              • JKing
                Recognized Expert Top Contributor
                • Jun 2007
                • 1206

                #8
                What is it you are setting in the div that you can't set on the a tag itself?

                Comment

                Working...