The anchor tag fails to function properly when nested within an image element inside an animated container

As I am currently developing a solar system webpage, my aim is to redirect users to a specific page when they click on a planet. Unfortunately, the anchor tag does not seem to be functioning as intended. I attempted to animate the anchor tag with an image, but that did not resolve the issue.

The HTML

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
        <div id="one"><a id="aone" href="http://google.com"><img src="one.png"></a></div>
        <div id="two"><img src="two.png"></div>
        <div id="three"></div>
    </body>
</html>         

The CSS

body{
  position: absolute;
  top: 50%; left: 50%;
  margin: -150px;
  border: dashed 1px;
  width: 300px; height: 300px;
  border-radius: 50%;
  content: '';
}

#one {
  text-align: center;
  position: absolute;
  top: 50%; left: 50%;
  margin: -25px;
  width: 50px; height: 50px;
  animation: rot1 8s infinite linear;
}

@keyframes rot1 {
  0% { transform: rotate(0deg) translate(300px) rotate(0deg); }
  100% { transform: rotate(360deg) translate(300px) rotate(-360deg); }
}

#two {
  text-align: center;
  position: absolute;
  top: 50%; left: 50%;
  margin: -25px;
  width: 50px; height: 50px;
  animation: rot2 34s infinite linear;
}

@keyframes rot2 {
  0% { transform: rotate(0deg) translate(150px) rotate(0deg); }
  100% { transform: rotate(360deg) translate(150px) rotate(-360deg); }
}

#three {
  text-align: center;
  position: absolute;
  top: 50%; left: 50%;
  margin: -25px;
  width: 50px; height: 50px;
  background: 
    linear-gradient(transparent 49%, black 49%, black 51%, transparent 51%), 
    rgba(0,0,255,.3) linear-gradient(90deg, 
          transparent 49%, black 49%, black 51%, transparent 51%);
  animation: rot3 34s infinite linear;
}

@keyframes rot3 {
  0% { transform: rotate(0deg) translate(50px) rotate(0deg); }
  100% { transform: rotate(360deg) translate(50px) rotate(-360deg); }
}

http://jsfiddle.net/DJsU9/

Answer №1

Is there a way to adjust the actual anchor position (not just the visual position) using CSS's transform property? Surprisingly, there is a simple workaround without resorting to JavaScript:

You can create three hidden circles, assign links to each one, and then position them over the current animation. This approach ensures that even if a user clicks in the wrong area, the link will still function.

Please note that I have given the circles a transparent red color for visibility, but you can delete the background:rgba(255,0,0,0.5); lines to remove the color.

Check out the relevant CSS code:

.circle1{
    display:block;
    position:absolute;
    top:50%;
    margin-top:-325px;
    left:50%;
    margin-left:-325px;
    width:650px;
    height:650px;
    background:rgba(255,0,0,0.5); /* can be removed */
    border-radius:100%;
}
.circle2{
    display:block;
    position:absolute;
    top:50%;
    margin-top:-175px;
    left:50%;
    margin-left:-175px;
    width:350px;
    height:350px;
    background:rgba(255,0,0,0.5); /* can be removed */
    border-radius:100%;
}

HTML setup:

<div id="one"><a id="aone" href="http://google.com"><img src="http://dedobbelaere.sisamul.com/one.png"></a>
</div>
<div id="two"><a id="aone" href="http://google.com"><img src="http://dedobbelaere.sisamul.com/two.png"></a>
</div>
<div id="three"></div>

<a class="circle1" href="http://dedobbelaere.sisamul.com/two.png"></div>
<a class="circle2" href="http://google.com"></div>

View JSFiddle demo

Answer №2

Only corrected syntax on nested HTML element, tested on IE10 and Chrome35 (Windows 8 Pro), but you may need to address some platform/cross-browser compatibility issues. Regardless, remember to assign a unique id to your hyperlinks

http://jsfiddle.net/InferOn/DJsU9/11/

HTML

<div id="one">
 <a id="aone" target="_blank" href="http://google.com">
  <img src="http://dedobbelaere.sisamul.com/one.png"/>
 </a>
</div>
<div id="two">
 <a id="atwo" target="_blank" href="http://google.com">
  <img src="http://dedobbelaere.sisamul.com/two.png"/>
 </a>
</div>
<div id="three"></div>

CSS

/**
 * Prevent an element from rotating in a circular CSS3 animation
 * http://stackoverflow.com/q/14057780/1397351
 */
 a {
    border: 1px solid red;
}
body {
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -150px;
    border: dashed 1px;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    content:'';
}
#one {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -25px;
    width: 50px;
    height: 50px;
    animation: rot1 8s infinite linear;
    -webkit-animation: rot1 8s infinite linear;
}
@-webkit-keyframes rot1 {
    0% {
        -webkit-transform: rotate(0deg) translate(300px) rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg) translate(300px) rotate(-360deg);
    }
}
@keyframes rot1 {
    0% {
        transform: rotate(0deg) translate(300px) rotate(0deg);
    }
    100% {
        transform: rotate(360deg) translate(300px) rotate(-360deg);
    }
}
#two {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -25px;
    width: 50px;
    height: 50px;
    transparent 49%, black 49%, black 51%, transparent 51%);
    animation: rot2 34s infinite linear;
    -webkit-animation: rot2 34s infinite linear;
}
@-webkit-keyframes rot2 {
    0% {
        -webkit-transform: rotate(0deg) translate(150px) rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg) translate(150px) rotate(-360deg);
    }
}
@keyframes rot2 {
    0% {
        transform: rotate(0deg) translate(150px) rotate(0deg);
    }
    100% {
        transform: rotate(360deg) translate(300px) rotate(-360deg);
    }
}
#three {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -25px;
    width: 50px;
    height: 50px;
    background: linear-gradient(transparent 49%, black 49%, black 51%, transparent 51%), rgba(0, 0, 255, .3) linear-gradient(90deg, transparent 49%, black 49%, black 51%, transparent 51%);
    animation: rot3 34s infinite linear;
    -webkit-animation: rot3 34s infinite linear;
}
@-webkit-keyframes rot3 {
    0% {
        -webkit-transform: rotate(0deg) translate(50px) rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg) translate(50px) rotate(-360deg);
    }
}
@keyframes rot3 {
    0% {
        transform: rotate(0deg) translate(50px) rotate(0deg);
    }
    100% {
        transform: rotate(360deg) translate(50px) rotate(-360deg);
    }
}

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

Is it possible to remove a particular div after clicking the delete button, especially if multiple divs are displayed upon clicking the add

var count = 2; var countMax = 5; function adddt() { if (count > countMax) return; document.getElementById('dt-' + count + '').style.display = 'block'; count++; } <link href="https://maxcdn.bootstrapcdn.com/bo ...

What is the best way to extract the frameset from a frame window?

Here is a code snippet to consider: function conceal(frameElem) { var frameSet = frameElem.frameSet; //<-- this doesn't seem to be working $(frameSet).attr('cols', '0,*'); } //conceal the parent frame conceal(window.pa ...

Tips for displaying cards with responsive design utilizing Bootstrap 5

How can I display cards responsively for mobile, desktop, and iPad? My goal is to have: - One column for mobile - Two columns for iPad - Four columns for desktop The current code only works for mobile and desktop, not for iPad. On my iPad, it's sh ...

Stop the div from expanding because of oversize text in Bootstrap

I have the following HTML source code for a Bootstrap card: <div class="card shadow-none card-fluid mb-3 mb-md-5"> <div class="row"> <div class="col-md-3 col-lg-3 mb-3 mb-sm-0"> <img class="img-fluid rounded" ...

Tips for creating responsive designs with specific media queries for various device sizes

I am looking to create a responsive website using media queries and have already created different media queries for mobile, tablets, and desktops. However, I am unsure if I should be writing the same CSS code multiple times for various device sizes, such ...

Error retrieving resource: server returned a 404 status code indicating file not found for javaScript and CSS files

I can't seem to get my css and js files to load on the server. Here is how my file structure looks like: GAME_Folder https://i.sstatic.net/wzfB3.png HTML doctype html head link(href='https://fonts.googleapis.com/css2?family=Press+Start+2P& ...

When attempting to open a popup form by clicking a button, the code fails to function on IE6

Everything seems to be running smoothly on Firefox, however I am encountering issues with Internet Explorer 6. Here is a snippet of the problematic code: document.getElementById('layout').style.opacity = .7 document.getElementById('layout&a ...

Tips for inserting active class on the main menu and adding an active class along with an icon on the sidebar menu

I am working on a website where I want to add the active class only when a user clicks on the top menu. However, if the user chooses something from the sidebar menu, I also want to add an icon arrow next to it. Any ideas on how I can achieve this? Check o ...

What is the relationship between three.js transforms and CSS3 3D-transforms?

I am developing a unique open-source tool for exploring and visualizing the complexities of human anatomy. At the center of this tool is a dynamic 'chessboard' that occupies the majority of the screen. As you drag the board, various CSS3 3D-tran ...

How come the nth-child selector remains unaffected by other selectors?

Here is an example that I have created for this issue: http://jsfiddle.net/SXEty/ <style> table td, th { padding: 8px; text-align: left; } table td:nth-child(1) { color: red; } table td { color: blue } </style> ... <table> <tr> ...

Is it possible to encounter the issue of "Context Lost" and "Importing multiple instances" in Three.js?

I am in the process of developing a 3D editor that allows users to manipulate a 3D scene and then view the result by pressing a "play" button. In order to display the output, I am utilizing an iframe. Below is the snippet of my HTML code: <iframe id=&qu ...

What is the best way to create collapsible rows in AngularJS with ng-repeat?

In my current project, I am utilizing ng-repeat to display objects in rows. To achieve my desired functionality of only displaying elements present in the DOM, I decided to use a helpful plugin called angular-vs-repeat. However, I am facing an issue with u ...

Ways to incorporate a scroll feature and display an iframe using JQuery

Is there a way to animate the appearance of hidden iframes one by one as the user scrolls down the website using jQuery? I have come across some solutions, but they all seem to make them appear all at once. I'm looking for a way to make the iframes s ...

Ways to format table using flex-wrap sans the use of flexbox

My HTML and CSS structure is as follows: .item { background: white; padding: 5px; display: inline-block; text-align: center; } .item > div { background: yellow; width: 60px; height: 60px; border-radius: 50%; display: table-cell; ...

Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. H ...

Retrieve information from an HTML input field that does not have an assigned ID or name

I am facing a challenge with an HTML table that contains multiple inputs which do not have any id or name attributes. I need to retrieve data from these inputs but struggling due to the lack of identifiers. Upon inspecting the DOM, I noticed that each inp ...

Is a CSS-only autoexpanding label possible within a list?

I am interested in having all the labels automatically expand to the size of the widest one. Below is the HTML structure: <div class="body"> <ul class="list"> <li> <span> <label>condition</label> ...

How come the width property is not being applied in my media query?

This issue is new to me. I am facing difficulty modifying the width in my media query. Interestingly, in the developer tools, the width appears crossed out, as if I toggled it off, but that's not the case; it's the default setting. All I want to ...

Interactive jQuery feature for dynamic search suggestions across multiple entries

Here is the HTML code snippet I am working with: <input name="data[Content][0][name]" type="text" class="content_name_1" id="content_name"> <input name="data[Content][1][name]" type="text" class="content_name_2" id="content_name"> ...

The attribute aria-required is necessary for a radio group

When creating a required radio group, how should the aria-required="true" attribute be assigned? Specifically, I have multiple <input type="radio"> elements with the same name grouped under a <fieldset>. Should the aria-required be placed o ...