I would like to have a div that contains an image and some text be a link. So that one can click anywhere (not necessarily the image or text) in the div and be taken to one location. However I would also like there to be another link at the end of the text that brings the user to another location. However, once I add this second link the whole div no longer is a link and just the image and text are links. Is what I am want to do possible?
You can fill a whole div or other parent element container in HTML with a link tag inside the div using some positioning attributes in CSS. The link tag is absolutely positioned to its parent which is relatively positioned, and will now fill the whole parent tag.
You can put an <a> element inside the <div> and set it to display: block and height: 100% . @Teddy: Adding position:relative; to the div and then position: absolute; height: 100%; to the a as well... should make the entire area of the div is active. You can put all of that content inside the <a> .
Nothing is wrong with placing a div inside a tag. In fact, you can place just about anything inside a tag as long as they are categorized as transparent , except that no descendant may be interactive content (eg: buttons or inputs) or an a element, and no descendant may have a specified tabindex attribute.
* When copying the URL of the page, right-click anywhere on the page and open the context menu, then select the format to copy. * When you right-click on a link (an anchor element), you can copy the URL of that link.
It is not possible in HTML, since an a
element inside an a
element is not allowed. By existing HTML specs, you cannot have e.g, div
inside a
either, but this restriction was never really enforced by browsers and it has been lifted in HTML5 drafts. Nested a
elements are a different issue, since it would create an active area inside an active, and its meaning would need to be defined and it would be confusing.
What happens in practice when using
<a href=foo>bla bla <a href=bar>inner link</a> stuff</a>
is that it works the way you want except that content of the outer a
element after the inner a
element is not a link at all. Apparently browsers are not prepared to handling constructs like this. Effectively, they imply a closing </a>
tag when they encounter an <a>
tag when an a
element is open. Much like they do for p
elements.
So an inner link at the end of a link would sort-of work. There is of course no guarantee that it will keep working in future versions of browsers, or that it works on all browsers.
You could use two separate, non-nested a
elements in succession and to place the second one visually inside the first one, using CSS positioning. But this would get somewhat complicated. A much simpler approach is to set up a JavaScript pseudo-link:
<span onclick="document.location.href = 'foo'; return false">inner link</span>
The downside is that it won’t act in a link-like manner when JavaScript is disabled, and search engines won’t treat it as a link either.
There is a way to do this without any javascript using absolute and relative positioning. This also keeps the code semantic for SEO, which the javascript struggles to do.
.outside-container { width: 900px; margin: 0 auto; position: relative; background: #888; padding: 5px; } .overlay { position: absolute; height: 100%; width: 100%; top: 0; bottom: 0; right: 0; left: 0; background: white; opacity: 0; } .overlay:hover { opacity: .2; } .text-box { position: absolute; top: 5px; right: 5px; width: 30%; color: white; font: georgia, serif 700 14px; } .image-box { width: 68%; }
<div class = "outside-container"> <img src="https://upload.wikimedia.org/wikipedia/commons/b/bc/George_Berkeley_by_Jonh_Smibert.jpg" alt="george_wiki" class = "image-box"> <a class = "overlay" href = "#div"></a> <div class = "text-box"> I was considering the odd fate of those men who have in all ages, through an affectation of being distinguished from the vulgar, or some unaccountable turn of thought, pretended either to believe nothing at all, or to believe the most extravagant things in the world. This however might be borne, if their paradoxes and scepticism did not draw after them some consequences of general disadvantage to mankind. But the mischief lieth here; that when men of less leisure see them who are supposed to have spent their whole time in the pursuits of knowledge professing an entire ignorance of all things, or advancing such notions as are repugnant to plain and commonly received principles, they will be tempted to entertain suspicions concerning the most important truths, which they had hitherto held sacred and <a href = "#text">unquestionable.</a> </div> </div>
Naturally I thought I was the only one who thought of this, but the question has been answered before. I added this because all the other answers were javascript or jQuery, and felt a pure HTML/CSS one could be useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With