How can I create an image link with a hover effect using HTML and CSS?

I've streamlined my code to focus on the issue at hand - I have an image that changes when hovered over, but how can I make it so that clicking on the image redirects you to a specific website? http://pastebin.com/h83yi3e3

Here is the code snippet:

<html>
  <head>
    <style>
       #buttonOne {width:140px;height:60px;background-image:url(buttonOne.png);}
       #buttonOne:hover {width:140px;height:60px;background-image:url(buttonOneH.png);}
       #buttonTwo {width:140px;height:60px;background-image:url(buttonTwo.png);}
       #buttonTwo:hover {width:140px;height:60px;background-image:url(buttonTwoH.png);}
       #buttonThree {width:140px;height:60px;background-image:url(buttonThree.png);}
       #buttonThree:hover {width:140px;height:60px;background-image:url(buttonThreeH.png);}
    </style>
  </head>
  <body>    
      <div id="buttonOne">
      </div>

      <div id="buttonTwo">
      </div>

      <div id="buttonThree">
      </div>    
  </body>
</html>

Answer №1

Transform those divs into a tags instead. This is what they are meant for. If you need an element that can be clicked to redirect to another URL, the <a> tag is what you should use.

<a id="buttonOne" href="http://letssaymywebsite.com/"></a>
<a id="buttonTwo" href="http://letssaymywebsite.com/"></a>
<a id="buttonThree" href="http://letssaymywebsite.com/"></a>

Answer №2

One way to utilize anchor tags is by following this method-

<a id="linkButton" href="example.com"></a>
...

Afterwards, you can apply a:hover to modify the visuals

Using CSS-

#linkButton a:hover
{
background-image:url(linkButtonHover.png);
}
...

Answer №3

When you mentioned removing the divs, I wasn't completely convinced it would solve the issue. Instead, I came up with a different solution. Within those empty divs, I cleverly inserted a hidden link-image.

<div id="buttonOne">
<a href="mywebsite.com" target="_blank"><img src="inbut.png">
</a>
</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

Transfer data from href link to specific detail page

I am currently working on a table that displays all the users. My goal is to turn the usernames into clickable links that direct you to their profile page. What steps should I take to convert the username into an href link and send it to the detail page? ...

How can I use Angular 7 to create a background image for a View made up of multiple components?

Here is the HTML code that I am working with: <app-navbar></app-navbar> <router-outlet></router-outlet> My goal is to have an image set as the background for the home view, which consists of two components: navbarComponent and hom ...

Is it possible to submit a HTML5 form and have it displayed again on the same page?

Is it possible to simply reload the sidebar of a page containing an HTML5 form upon submission, or is it necessary to load a duplicate page with only the sidebar changed? I am unsure of how to tackle this situation; firstly, if it is achievable in this m ...

Is there a way to align the image to the left within the div contained in my header section?

I am facing an issue with positioning the logo in the header section of my website. It appears correctly on the left side for tablet and mobile versions, but on desktop, it does not align to the corner as desired. I tried adding a right margin, but I belie ...

Why is only jQuery 3.2.1 or jQuery Mobile 1.4.5 being loaded?

When I load my html file, only jquery or jquery mobile is loading from the head. Changing the order in which they appear in the head causes a switch in which one is loaded. Is this possibly due to a compatibility issue? <head> <title& ...

Incorporating a touch of dark opacity into an image

Check out the layout of my webpage here: https://i.sstatic.net/Ap4nx.jpg The background image I used is this one: https://i.sstatic.net/dIhLt.png I am trying to achieve a black transparent overlay on the image, similar to this: https://i.sstatic.net/FMdi ...

JavaScript: Launching the current page in a new tab or window

Is it feasible to open a new window or tab from the existing HTML page? Absolutely, I am referring to the current HTML page itself. Not triggered by an external call like this: window.open('page.html', '_blank'); Possible through Jav ...

Mounted class not initiating transition in Vue.js

After attempting to apply a class to an element in the mounted lifecycle, I noticed that the transition effect was not taking place. The element would immediately display in its final state: However, when I used setTimeout to delay the class change, the t ...

Issue with random pages where global header.php is not showing a specific image

I'm currently revamping a website that was originally developed using codeigniter. The backend is quite chaotic with no clear identifiers for anything. I recently updated the banner in the header.php file, adjusting the style to suit the requirements. ...

The accuracy of real-time visitor numbers in Google Analytics is often unreliable

My website uses Google Analytics to track the page chat.php. The code snippet is correctly placed on the page according to the documentation. Within this page, there is a Flash object that connects users to an IRC chat room. Currently, there are 50 unique ...

Browser Fails to Recognize AJAX Links as Visited

It appears that the styles for a:visited do not apply to links that are loaded via JavaScript. When a user clicks on a standard link, it shows as visited right away and even after refreshing the page. I'm uncertain if this issue is specific to jQuery ...

Can basic logic be applied to CSS?

Recently, I have been experimenting with CSS to blur NSFW images in a chatroom until they are hovered over. I have successfully accomplished this task and now my next goal is to implement checkboxes that can toggle the display of these images. I am aware o ...

Restrict the width of CSS columns to the value of an assigned variable

I'm struggling with centering a one-row table within a div. Specifically, I'm having trouble adjusting the size of two columns to match the length of the content inside them (team name and round). The HTML code is: <div id="greeting"> ...

Every time I attempt to visit my website on a mobile device, it seems to appear larger than normal, as

After developing a responsive website and adding media queries to make it mobile-friendly, I encountered an issue. Despite my efforts, the website's width is still larger than the mobile viewport, requiring users to zoom out to view it properly as sho ...

Instant data entry upon clicking

In an attempt to automate input based on image pairs, I encountered a challenge. On one side, there are two images that need to be matched with input fields, and on the other side, there are corresponding letters for each image to fill in the inputs upon c ...

Can an element on a webpage be dynamically assigned an ID using JavaScript or PHP?

Is it possible to dynamically set the ID of an element during runtime of a webpage? Let's consider this scenario - <body id="body1"> Now, I would like to dynamically set the ID of this "body" element using a variable value in PHP code. Is som ...

Is it possible to serve CSS using relative paths that go beyond the URL root?

I've encountered a file path issue with my HTML and CSS files. My HTML file is located in: src/test/html/index.html And the corresponding CSS file is in: src/test/css/index.css In the HTML file, the CSS is linked using: <link rel="stylesheet" ...

Jump to Anchor when Page Reloads

Hey there! I'm currently developing a gallery script and I'm trying to figure out how to automatically position the page to the top of a specific anchor when it is loaded. I attempted to use the following code: <script>location.href = "#tr ...

Completing the remainder of the page with CSS styling

Currently, I have three Divs positioned absolutely on the page - leftDiv, middleDiv, and rightDiv. The middleDiv has a width of 900px, which is fine. However, I need the leftDiv and rightDiv to fill the remaining space on the left and right sides, regardle ...

Creating a video game HUD effect using JavaScript and HTML5

In an attempt to recreate a video game HUD using HTML5, I have styled a div with CSS properties to resemble a game overlay. When a navigation link is clicked, the div should transition from a hidden state (display:none), then flash twice rapidly before fad ...