Is it acceptable to embed a hyperlink within another hyperlink?

Is it possible to insert a link within another link? Check out the image attached below:

I am attempting to make the entire grey bar clickable, but if the user clicks on the wheel or the move arrow, they should be directed to different links. Here is my current code:

<a href="#" class="sp_mngt_bar">
    <h1><?php echo $v; ?></h1>
    <a href="#" class="t_icons t_icons_settings sp_mngt_settings"></a>
    <a href="#" class="t_icons t_icons_move sp_mngt_move"></a>
</a>

Is this considered proper practice? Am I approaching this incorrectly? How would you handle this situation? Thank you for your assistance!

Answer №1

Directly from the W3C guidelines for HTML4:

12.2.2 Prohibition of Nested Links

It is against the rules for links and anchors designated by the A element to be nested; an A element should not contain any other A elements.

Furthermore, since the LINK element is specified as empty in the DTD, LINK elements are also prohibited from being nested.

HTML 5 Rules

In terms of HTML5 guidelines, the regulations slightly vary.

You are not allowed to place Interactive Content within an A element. This encompasses anchor tags as well.

Answer №2

Simply put, the answer is: No.

However, there is a clever workaround using pure html/css:

https://codepen.io/pwkip/pen/oGMZjb

.block {
  position:relative;
}

.block .overlay {
  position:absolute;
  left:0; top:0; bottom:0; right:0;
}

.block .inner {
  position:relative;
  pointer-events: none;
  z-index: 1;
}

.block .inner a {
  pointer-events: all;
}
<div class="block">
  <a class="overlay" href="#overlay-link"></a>
  <div class="inner">
    This entire box is a hyperlink. (Kind of)<br><br><br><br>
    <a href="#inner-link">I'm a W3C compliant hyperlink inside that box</a>
  </div>
</div>

Answer №3

To enhance your nested link, encapsulate it within an object tag:

<a href="#" class="sp_mngt_bar">
    <h1><?php echo $v; ?></h1>
    <object><a href="#" class="t_icons t_icons_settings sp_mngt_settings"></a></object>
    <object><a href="#" class="t_icons t_icons_move sp_mngt_move"></a></object>
</a>

Answer №4

While I completely agree with the chosen response and indeed, it is not recommended to have Interactive Content within an A element, there are instances where a workaround may be necessary.

For instance, consider the scenario where you must include an interactive element inside an A tag, such as a small close button on the top right.

https://i.sstatic.net/m1HvE.png

Here is the simplified HTML code for this situation:

    <a href="#">
        <span class="hide">X</span> <!-- THIS IS THE SMALL 'X' WHICH HIDES THE WHOLE BOX -->
        <img src="images/camera.svg" width="50" alt="Camera" />
        <em>
            Upload a profile picture
            <small>
                Here's the deal. Make your profile look awesome and even get 25 karma for it. We're not kidding.
            </small>
        </em>
        <strong>
            + 25 K
        </strong>
    </a>

In essence, we want to hide this box when the user clicks on the 'X', while still maintaining basic functionality of the A tag. The following jQuery code achieved this:

    $('.hide').click(function(e) {
            e.preventDefault();
            e.stopPropagation(); // THIS IS THE KEY PART
            
            // PERFORM CUSTOM ACTIONS HERE, SUCH AS FADING OUT THE BOX
            $(this).parent().fadeOut(300);
    });

I trust that this solution may assist others facing similar challenges. ;)

Answer №5

To enhance its appearance, I would consider reformatting it to resemble the following layout:

<div class="custom_management_bar">
    <h2><a href="#"<?php echo $v; ?></a></h2>
    <a href="#" class="t_icons t_icons_settings custom_mgmt_settings"></a>
    <a href="#" class="t_icons t_icons_move custom_mgmt_move"></a>
</div>

Answer №6

Avoid using nested links, as it is not allowed. To simulate the same functionality without nested links, follow these steps:

Utilize HTML formatting by @mikevoermans and attach a click event to it

<div class="sp_mngt_bar">
    <h1><a href="#"><?php echo $v; ?></a></h1>
    <a href="#" class="t_icons t_icons_settings sp_mngt_settings"></a>
    <a href="#" class="t_icons t_icons_move sp_mngt_move"></a> 
</div>

The click event function should be structured like this:

$(".sp_mngt_bar").bind("click", function(e) {   
    var target = $(e.target);
    if(target.has('.t_icons_settings') { //Perform action for settings } 
    else if(target.has('.t_icons_move') { //Perform action for move }
    else { //Perform default action for sp_mngt_bar
});

Answer №7

Instead of directly answering the question, one possible solution is to attach the click event to a span or div:

<a href="outer-link">
  Outer Link
  <span class='inner-link'>Inner Link</span>
</a>

$('.inner-link').click(function (e) {
    // Prevent the click-through to the underlying anchor
    e.stopPropagation();

    // Navigate to a different page
    window.location.href = 'new-page.html';
    // Alternatively, execute a JavaScript function
    doSomething();

    return false;
});

Answer №8

To solve this issue, one approach is to absolutely position a link inside the parent link container:

<div style="position: relative">
  <a href="#">
    <h1><a href="#"<?php echo $v; ?></a></h1>
    <div id="placeholder" style="height: 24px">
  </a>
  <div style="position: absolute; bottom: 0">
    <a href="#"></a>
  </div>
</div>

Answer №9

@Jules provided a concise answer:

.parent {
  position:relative;
}

.overlay-link {
  position:absolute;
  inset: 0;
}

This is how it would look in HTML:

<div class="parent">
    <a class="overlay-link" href="..."/>
    <div class="child">
                    
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting dates from JavaScript to Central Daylight Time (CDT) using the new

I have a function below that is used to format a date from an API Response. However, when the date "2021-10-02" is received in the response, the new Date() function converts it to Mon Oct 01 2021 19:00:00 GMT-0500 (Central Daylight Time), which is one da ...

Adaptive video player for complete YouTube experience

Is there a way to incorporate a YouTube video as the background of a div, similar to how it is done on this site, using a <video> tag and ensuring that the video "covers" the entire div without any empty spaces while maintaining its original ratio? ...

Changing position with flair in CSS

Is there a way to create a transition effect on an element when it changes position from static to fixed? I attempted using the code: transition: position 2s linear;, however, it does not seem to have any impact. Are there any other methods I could experi ...

Compatibility of AngularJs form options across various browsers

My inquiry regarding AngularJs revolves around how it manages forms and form validation, a subject I am struggling to find answers for through Google search. I am aiming to create a form that is cross-browser compatible while adhering to the 'Angular ...

Animating the width of an image with CSS3 animations

Looking to utilize CSS3 animations to shrink an image size when hovered over. Here's what I currently have: #logo-icon img { width: 80px; }; #logo-icon img:hover { width: 50px; transition: width 0.2s; }; However, the transition seems to be from 0px ...

Using jQuery and cheerio to iterate through a collection of items

I am currently reviewing the elements within 2 HTML tables: <table class="content"> <tbody> <tr> <th>title1</th> <td>info1</td> </tr> <tr> ...

Expanding image in card to fit device width using Ionic

Currently utilizing the ionic framework and attempting to expand the image within the card to fit the device width. Referencing the example provided here, here is my current code progress. <div class="list card"> <div class="item item-avatar" ...

The top navbar's dropdown menu gets concealed by the overlapping second navbar

Having encountered an issue with my website, I recently made a change to the top navbar from `fixed` to `static` due to problems on mobile devices caused by the `fixed` layout. Unfortunately, this adjustment resulted in another problem - the dropdown menu ...

Issue with radio button list not functioning properly in Mozilla and Chrome browsers

My web application has a radiobuttonlist with an onclick event. It functions properly in IE, but not in some other browsers. Here is a snippet of the code: <asp:RadioButtonList ID="rbgThreadStatus" runat="server" RepeatDirection=&quo ...

Assigning a unique identifier to a button that will vary depending on the selected radio input

UPDATED HTML: The circumstances have changed because I am no longer in my office, but the concept remains unchanged. The situation is that on a certain part of a website, users have the option to choose from multiple saved addresses. Each address has a un ...

Switch out a static full-page image background for an engaging HTML5 video

Is there a simple method to switch out a full-page image background with a video? video { position: fixed; top: 50%; left: 50%; min-width: 100%; min-height: 100%; width: auto; height: auto; z-index: -100; transform: translateX(-50%) tr ...

Ways to adjust the text size in jqGrid?

The current theme is ui-lightness. How can I adjust the font size within the grid? Your suggestions are appreciated. Many thanks. ...

The collapsed navbar button in Bootstrap is unresponsive

I run two websites. The Collapsed Navbar functions properly on the first site, located at (Index Site). <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" ...

Is there a way to access a CSV file, process its content line by line, decode it using base64, and store the decoded data in a different file?

My goal is to read a csv file, use PHP base64_decode() to decode it, and then write the decoded data to a new file in the same format. I previously attempted to read the file line by line and decode it as it was being read, but the resulting data was corr ...

Unfortunately, I am unable to paste the specific URL into the browser's address bar

After developing my main page, I encountered an issue when trying to open news articles. The interpreter is throwing a "ValueError: Field 'id' expected a number but got 'news1'" error message (full traceback and code provided below). Wh ...

Issue with HTML CSS: There is a gap between the words "Get" and "started", causing "get" to appear on

Greetings, it appears I am encountering an issue with my website. When I refer to "Get Started," it displays as "GetStarted." However, when I insert a space between "Get" and "started," it separates onto two lines as "Get" on the first line and "started" o ...

ElegantFrameload - Displaying an HTML webpage

I want to implement a monetary conversion tool on my webpage using fancy box. However, I am facing issues with getting it to work. Here is the link to the page that is not functioning properly: The link at the bottom of the page should trigger the fancy b ...

Interactive YouTube Video Player that keeps video playing in original spot upon clicking the button

Currently, I'm working on a project that involves combining navigation and a video player within the same div container. Here is an image link And another image link The concept is that when you click on one of the four boxes, a new video will appe ...

Switching the choice within a dropdown menu to trigger an onchange event using JavaScript

After spending a significant amount of time trying different solutions without much progress, I am still facing difficulties with implementing a specific functionality on my website. The main goal is to have images change the selected value of a dropdown ...

Using HTML and CSS to stack a DIV on top of another using z-index

I have 3 main layers on my website: 1) The main view with elements inside (#views in jsbin) - BOTTOM LAYER 2) An overlay (with a white background opacity of .8 #overlay in jsbin) - MIDDLE LAYER 3) A context menu (#contextmenu in jsbin) - TOP LAYER Wh ...