how to implement an anchor on a background image using CSS

Is there a way to wrap an anchor around the image only if I have a DIV with a height of 200px and a background applied to it with a height of 175px?

<div style="background: url('http://www.crmpicco.co.uk/downloads/Image/11218.jpg') no-repeat scroll 0 0 transparent; background-position: bottom;" class="block block2 ">
    <ul class="elem"></ul>
    <span class="left_border"></span>       
</div>

I haven't found any obvious solution other than restructuring my markup, which I'd prefer to avoid right now.

Answer №1

By adding the CSS property position:relative to the parent div (the one with the background), you can then insert an anchor in this way:

.block2 a.block_link {
    position:absolute;
    bottom:0;
    height:175px;
    left:0px;
    width:100%;
    z-index:1;
    background-image:url(transparent.png);
    text-indent:-9999px;
}

Note: It is advised to use a transparent PNG for the link to resolve the IE bug. A 1px by 1px transparent image should work perfectly.

<div style="background: url('http://www.crmpicco.co.uk/downloads/Image/11218.jpg') no-repeat scroll 0 0 transparent; background-position: bottom;" class="block block2 ">
    <ul class="elem"></ul>
    <span class="left_border"></span>    
    <a href="#" class="block_link">&nbsp;</a>   
</div>

To ensure that other elements remain above the link, such as ul and span.. apply the following CSS:

.block2 ul,.block2 span {
    position:relative;
    z-index:2;
}

Answer №2

To create a visually appealing design, consider placing a div element with absolute positioning over your background image. Set the width and height of the div to match the dimensions of the background image, then enclose it within an anchor link for added functionality.

Answer №3

According to the comments, it's technically not possible to wrap a link around a background image. However, if you're looking to make that area clickable in some way, here's a solution:

Here is an example of how you can achieve this using HTML and CSS:

<div id="my-div" style="background: url('http://www.crmpicco.co.uk/downloads/Image/11218.jpg') no-repeat scroll 0 0 transparent; background-position: bottom;" class="block block2 ">
    <ul class="elem"></ul>
    <span class="left_border"></span>      
    <a href="http://www.example.com/"></a> 
</div>

CSS:

#my-div {
position: relative;
}
#my-div a {
display: block;
position: absolute;
top: 0;
left: 0;
height: 175px;
width: 100%;
}

If it's unclear how you want the link to interact with the UL element, keep in mind that you may need to adjust the positioning or z-index to ensure both are functional.

Answer №4

Below are some things you can accomplish:

<div style="position:relative; background: url('http://www.crmpicco.co.uk/downloads/Image/11218.jpg') no-repeat scroll 0 0 transparent; background-position: bottom;" class="block block2 ">
   <ul class="elem"></ul>
   <span class="left_border"></span>
   <a href='#'>link</a>       
</div>

CSS for link <a>:

.block a{
    display:block;
    width:100%;
    height:25px; // extra height from the top;
    overflow:hidden;
    text-indent:-99999px;
    position:absolute;
    left:0;
    bottom:0; // or try with top:175px;
}

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

Are the elements in Chrome overlapping due to the box model?

I'm currently experiencing a problem of cross-browser compatibility between Chrome and Firefox. Upon inspecting the webpage in Chrome, it's evident that the box for the #content DIV is overlapping with the box for the H3. When viewed in Firefox ...

Crafting an interactive saturation effect for mouseover interactions in a circular design

I'm looking to create a unique hover effect for my images. I have both a desaturated version and a full color version of the same image. My idea is to have mousing over the desaturated image reveal a circle spotlighting the color version underneath, a ...

Is it possible to integrate Processing JS on top of an HTML element?

I have currently integrated Processing JS with the HTML canvas element on a website. The goal is to have the Processing JS animation run smoothly over the existing HTML elements on the page. You can check out the site at [usandthings.com][1] to get an idea ...

The perspective shifts as the windows.location is redirected to a new URL, yet the elements (objects) remain connected to the previous destination

When it comes to redirecting to a URL on PhoneGap for iOS, I have been using the following JavaScript code: function pedirTaxi(){ var direccion = $('#txtDireccion').val(); alert(direccion); window.location.href = "loading.html"; ...

Properly appears in Chrome, but experiences issues in Internet Explorer 11

The design of the website looks seamless in Chrome and Firefox, however, when I view it in Internet Explorer, I notice a significant gap on the right side of the first two pages. How can I fix this issue specifically for Internet Explorer? Should I use a m ...

Using Jquery to sequentially add a class to individual <li> elements in a list, with a delay of 5 seconds for each addition

Currently, I am working on a website that requires me to assign a class of 'active' to a series of <li> elements. The number of <li> elements varies - sometimes there are six, other times 17. This needs to be adaptable and dynamic. ...

Alternative content can be pasted into the webpage

After finding out a peculiar phenomenon related to copy and paste, I decided to share it. If you head over to the Diablo 3 website () and scroll down to the bottom of the page, you will notice: ©2012 Blizzard Entertainment, Inc. All rights reserved A ...

"Within the boundaries of two tags, eliminate any spaces when using the display property set

I'm a bit confused about the behavior of display:inline-block. I noticed that when I use inline-block, it displays content in a smaller space across two tags. However, when I switch to using float: left, everything works fine. Does anyone have an ide ...

Is there a way for me to convert my HTML code into a Vue component?

I am trying to display a tiff file on a web page, I successfully did it with test.html, but now I need to do it within a vue file. How can I achieve this? Additionally, I am unsure about the specific content in the tiff.js[![enter image description here][1 ...

What is preventing the ng-change from being triggered in this Angular JS test?

Looking to experiment with Angular JS so that every time I update a text input, the corresponding model object is logged to the console. Here's the markup I have: It's contained within a div with the attribute ng-controller="datosPersonalesCont ...

Tips for avoiding accidental unit conversion in jQuery

Imagine having the following code: var $element = $('<div/>'); $element.css("margin-left", "2cm"); console.log($element.css("margin-left")); When tested in Chrome, it doesn't return anything, but Firefox shows "75.5833px". Any sugges ...

React-generated HTML emails are not displaying correctly in GMail's web client

When tasked with creating a newsletter e-mail server-side, I explored different options but ultimately decided on React (server-side rendering) due to its strong TypeScript support and my familiarity with the technology. Designing an e-mail that renders p ...

What is the best way to dynamically convert a lodash object (specifically a filter object) into jQuery's listview component?

After referencing the answer to this topic as my first step, which can be found here, my next goal is to utilize a filter function to retrieve all matching entries from a JSON file based on a search term. The objective is to loop through each match and con ...

Error: It is not recommended to utilize <Link> without a <Router> in the context of a Navbar

I am currently working on a basic React app that includes a Bootstrap Navbar. I've encountered the error mentioned above and I'm trying to figure out what's causing it. Despite checking other posts related to this issue, I can't seem to ...

Manipulating zIndex using a Function does not produce the desired outcome

As I attempt to adjust the zIndex of the Div Boxes so that the targeted Div Box follows the mouse cursor and all surrounding Div Boxes move behind it, an issue arises where the zIndex remains unchanged resulting in the next Div Box overlapping the previous ...

Troubleshooting Django: Missing CSS File Issue

I spent a considerable amount of time trying to resolve the issue with loading all my static files. Eventually, I found a solution that allowed SVGs to load properly, but my CSS files were still not loading. Below are snippets from my settings.py file (sh ...

Bootstrap Sticky Footer Issues

Can anyone help me with correctly setting up a sticky footer for my website? I want it to stay at the bottom of the page even if the content above does not fill the entire screen. I thought it was supposed to be simple, but I can't seem to get it righ ...

Fixing the issue of "additional space" in the Login Card with Bootstrap 5

I am currently working on a basic login form using bootstrap and encountering an issue, below is the code snippet: <div class="container"> <div class="row justify-content-center no-gutters" style="padding-top:150px ...

Using JavaScript to replace static placement

I'm looking to implement a floating navbar on my website, the kind that scrolls down with you and stays at the top of the window. However, I've run into an issue with certain Android versions (like 4.3) when using the fixed position. My navbar w ...

Jspsych: Centering and aligning 3 p tags to the left on the page

Feeling pretty stuck at the moment. Here's what I have visually right now: https://i.sstatic.net/FUzgM.png I added a caption in Comic Sans and an arrow using MS Paint. Each line with color swatches is wrapped in a p tag. The goal is to center thes ...