anchor inside span inside anchor, in chrome - google-chrome

i'm having an issue in chrome (and firefox, too, i think)
i have a bit of html that looks like this:
<a id="outside">
<span id="middle>
:contents
</span>
</a>
if :contents evaluates to a string or a tag other than an anchor, it renders as intended;
<a id="outside">
<span id="middle">
inside
</span>
</a>
or
<a id="outside">
<span id="middle">
<div id="inside"></div>
</span>
</a>
however, if it evaluates to an anchor, it renders as follows:
<a id="outside">
<span id="middle>
</span>
</a>
<a id="inside"></a>
the anchor is jumping outside the span. why? what can i do to fix this?

To fix it, you have to make sure that you never embed an anchor tag insider another anchor tag - results are unpredictable as it is not standards compliant.
If you have control over the generated code, make sure you tidy up that HTML by substituting that top level anchor with something that can contain both a div and a a tag - perhaps another div element?
References:
http://www.w3.org/TR/html401/struct/links.html#h-12.2
http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-a-element
http://www.w3.org/TR/html5/text-level-semantics.html#the-a-element

This is likely happening because your browser is trying to 'fix' your illegal HTML. According to the W3 documentation:
12.2.2 Nested links are illegal
Links and anchors defined by the A element must not be nested; an A element must not contain any other A elements.
Since the DTD defines the LINK element to be empty, LINK elements may not be nested either.
Here is the link to the documentation: http://www.w3.org/TR/html4/struct/links.html#h-12.2.2

Related

HTML not rendering as written: Anchor tag being duplicated outside parent [duplicate]

This question already has answers here:
Why are nested anchor tags illegal?
(4 answers)
Closed 1 year ago.
I've run into a rendering issue I can't explain. I'm hoping someone can shed some light.
In its most simplified form I have the following HTML:
<html>
<body>
<a id="anchor1" href="#">
<div>
<a id="anchor2" href="#">Edit</a>
</div>
</a>
</body>
</html>
When this actually renders in a browser the result is this:
<html>
<body>
<a id="anchor1" href="#"> </a>
<div>
<a id="anchor1" href="#"> </a>
<a id="anchor2" href="#">Edit</a>
</div>
</a>
</body>
</html>
Notice #anchor1 adjustments:
Its been closed in its original location, no longer wrapping around the div tag.
It's been copied as a standalone element just inside the div tag.
If I remove the innermost anchor tag (#anchor2) there is no rendering issue.
Can someone please explain what's going on here? I've been able to wrap div tags in anchor tags before, but for some reason this but of HTML has unexpected results. I've reviewed in Chrome, Safari, Firefox, Edge...I've even tried Codepen.
You cannot have an anchor <a> tag inside of another anchor <a> tag unfortunately.
Browser wouldn't know which one is clicked when a user clicks it, so browser tries to correct your incorrect implementation and closes the first anchor <a> tag and in turn it creates a havoc of html.
You can't have an <a> tag inside another <a> tag. It's not valid HTML. A browser will do the best it can, but it's probably not going to be what you want.
Solution: don't do that.

Why does href appear not to work with id?

<h1><a id="Didn't-answer-your-question?">Didn't answer your question?<!--ID end--></h1></a><!--link end-->
Can I have an id element inside a href element, for the link text or does this not work, if so do people have an alternative?
There are several problems with your markup.
You don't close the <h1> tag properly. Your <a> id may technically work, but I wouldn't recommend it as an element ID.
The one that's giving you an issue though is the fact that you have an <a> tag nested inside another <a> tag. That is not valid markup.
Your inner tag does not need to be an anchor tag, change it to a span or something and it should work fine.
<a href="http://www.website.com">
<h1>
<span id="Didn't-answer-your-question??">Didn't answer your question?</span>
<!--ID end-->
</h1>
</a>
<!--link end-->
You didnt properly close the h1 tag but other than that it looks like it should work.
This is from the w3 website:
I just returned from vacation! Here's a
<A id="anchor-two">photo of my family at the lake.</A>.
http://www.w3.org/TR/html401/struct/links.html#h-12.2.3

Browsers rendering multiple nested a tags

Imagine this block of HTML:
<a href="/somewhere/">
<div class="nested">
<div class="sub-nested">
<div class="sub-sub-nested">
button
</div>
</div>
</div>
</a>
This gets rendered in my browsers like this:
<div class="nested">
<div class="sub-nested">
<div class="sub-sub-nested">
button
</div>
</div>
</div>
This happens only if there is another a tag inside the outer a tag.
I totally don't understand why this is happening. How this could even be. And it's driving me insane.
The problem looks so basic, that i wonder what it was about the HTML standard that i have misunderstood? After all, shouldn't as of HTML5 any tags be allowed within a tags?
What am i missing here?
You can't next anchor tags. As the W3 says:
12.2.2 Nested links are illegal
Links and anchors defined by the A element must not be nested; an A
element must not contain any other A elements.
If you try to validate your code, you will get
Document type does not allow element "div" here; (...)
One possible cause for this message is that you have attempted to put
a block-level element (such as "<p>" or "<table>") inside an inline
element (such as "<a>", "<span>", or "<font>").
So you can't put a <div> inside an <a>.
To expand a bit on why you can't nest A tags, the browser would not know where to direct the user, since the multiple A tags would have multiple HREF attributes. This is why it is illegal to nest A tags.

<a> tag in <a> tag for seo

Old code:
Image
Title
Download
New code:
<a href="test.html">
Image
Title
Download
</a>
I need to keep the classes because otherwise it will screw up the CSS and it's too much to change.
But is the code bad for SEO?
It's bad full stop - nested <a> elements are illegal in HTML.
Would something like this suit your needs?
<a href="test.html">
<span class="class1">Image</span>
<span class="class2">Title</span>
<span class="class3">Download</span>
</a>

IE6 Bug - Div within Anchor tag: inline images not links

I'm trying to get everything in the anchor tag to be a clickable link. Unfortunately, in IE6 (which is the only browser I'm concerned with currently), the only thing that isn't a clickable link are the inline images. I know that it's not valid html to put a div inside of an anchor but it's not my markup and I've been asked to avoid changing it. Any suggestions to altering the CSS to enable the images as clickable links? If changing the markup is the only solution... any suggestions there? My initial thought was to set the image as a background of it's parent (.ph-item-featured-img), although I'm unclear if that will solve the problem.
Thanks!
<div class="tab-panel-init clear ui-tabs-panel ui-widget-content ui-corner-bottom" id="ph-flashlights">
<a href="#" class="last ph-item-featured clear">
<div class="ph-item-featured-img">
<img src="#">
</div>
<strong>
PRODUCT CODE
</strong>
<p>
PRODUCT CODE Heavy Duty Aluminum Led Flashlight
</p>
<span>Learn more ></span> </a>
<a href="#" class="last ph-item-featured clear">
<div class="ph-item-featured-img">
<img src="#">
</div>
<strong>
PRODUCT CODE
</strong>
<p>
PRODUCT CODE Heavy Duty Aluminum Led Flashlight
</p>
<span>Learn more ></span> </a>
</div>
The problem is that it isn't valid html. Explain that you have to change the markup to make it work as desired. Changing the div to a span and setting the class .ph-item-featured-img to display: block should produce the same look-and-feel and be correct html.
Edit: Another, not as clean solution, is to add a click-listener with JavaScript and invoke the link upon a click on the image.
If you can't change the mark up (which you admit isn't valid), I don't think there is anything you can do here.
You should reconsider changing the markup. This example is bad in so many ways it could serve as a textbook example of what not to do.
Alternate strategies:
Remove everything but the image and
give it an onclick handler that does
the link mechanics.
Remove the DIV and just have the IMG
inside the anchor tag.
etc.
Well i looks like youre already using jQueryUI so why not just through a click even on the containing DIV. Also you should definitely change the markup. If its not valid, its not valid. That can lead to all kinds of problems other than the one youre currently facing. If there is a good reason for change this is it.
This is what the w3c validator returns when I pass in the snippet you posted:
Line 15, Column 46: document type does not allow element "DIV" here; missing one of "OBJECT", "MAP", "BUTTON" start-tag
<div class="ph-item-featured-img">
The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.
One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").
If I remember correctly, IE6 requires that every element inside of the <a> tag to be an element with CSS display: inline set on it (or inline-by-default elements like <span>, <b>, <strong>, etc.), or else it doesn't get linked, or links act weird.
Perhaps it is even IE6's HTML parser that is to blame. Maybe it sees the <img src="#"> and thinks, "that's not a valid URL to an image! :ignore:". IE6 is strange that way, often acting in a way that is a diametric opposite to how standards-compliant browsers act.
Truth is, this I have no way of checking all this; thankfully, every Windows computer I have access to has IE7+ on it. Perhaps you should take Google's route and just explicitly say that you're not going to support IE6, redirecting all IE6 browsers to a place where they can upgrade.
I believe you can do this with conditional comments like so:
<html>
<head>
<!--[if lte IE 6]>
<meta http-equiv="refresh"
content="2;url=http://www.microsoft.com/windows/internet-explorer/default.aspx" />
<![endif]-->
...
</head>