Press the button obscured by an image

Recently on my HTML website, I encountered an issue where I tried adding a transparent image over a button but found that I was unable to click on the button afterwards. The problem seems to be related to the following code snippet:

#container {
  height: 400px;
  width: 400px;
  position: relative;
}
#image {
  position: absolute;
  left: 0;
  top: 0;
}
<div id="container">
  <img id="image" src="http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png" width="200px" ;height="200px" />
  <a href="#">
        Click here
    </a>
</div>

Any insights or solutions would be greatly appreciated. Thank you!

Answer №1

A solution for this issue is to adjust the z-index of the image so that it is lower than the a element (which is typically set to 0 by default)

#container {
  height: 400px;
  width: 400px;
  position: relative;
}
#image {
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
}      
<div id="container">
  <img id="image" src="http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png" width="200px" ;height="200px" />
  <a href="#">
    Click here
  </a>
</div>

Another option is to use the image as a background on the container, eliminating the need for absolute positioning or adjusting z-index.

Answer №2

If you want to make the button appear in front, remember to set a z-index value and set its position to relative

.button {
  z-index: 1;
  position: relative;
}

Below is the code snippet you should use:

html

<div id="container">
<img id="image" src="http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png" width="200px";height="200px"/>
<a class="button" href="#">
    Click here
</a>

css

#container
{
    height:400px;
    width:400px;
    position:relative;
}

#image
{    
    position:absolute;
    left:0;
    top:0;
}

.button {
  z-index: 10;
  position: relative;
}

For a live demo, check out the js fiddle here

Answer №3

You have the flexibility to adjust the width and height of the image-container to your liking.

#container {
  height: 400px;
  width: 400px;
  position: relative;
  }
#image-container{
  width:100%;
  height:100%;
  background-image: url("http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png");
  background-position:center;
  background-size:contain;
}
<div id="container">
  <div id="image-container">
  <a href="#">
        Click here
    </a>
    </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

creating tables in PHP using HTML code

I can't figure out what I'm doing wrong here. I'm trying to create a PHP table but some of the code isn't working properly. Any assistance would be greatly appreciated. Thank you. <?php echo" <table border='1'> < ...

Ways to determine if content is visible within a div

My website contains a script that brings in content from an ad network and displays it within a div element. Unfortunately, this particular ad network only fills ads 80% of the time, leaving the remaining 20% of the time without any ads to display. This la ...

What is the best way to exclude a field from a Joi schema?

I've defined a Joi schema for a User with the following rules: const userRules = Joi.object({ name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(), email: Joi.string().email().requ ...

Is it possible to reverse engineer a UI by starting from its current state and debugging backwards?

When it comes to web development, users often provide screenshots of their "invalid state". Using React, I had a thought about debugging in a different way: starting from the current state: A user sends us a screenshot of an invalid UI state. We use dev ...

The latest version is available, but remember to update @react-navigation/bottom-tabs, @react-navigation/stack, and @react-navigation/drawer to at least version 5.10.0

As a newcomer to react-native, I am currently attempting to execute a program using expo but encountering a yellow error message. The error states: 'It seems that you are utilizing an outdated version of the react-navigation library. Please ensure th ...

CSS styles are not being applied correctly to the Bootstrap modal in combination with Leaflet

After importing leaflet-bootstrapmodal.js into my project, I am facing an issue with styling it. Take a look at the plugins listed below: <!-- Bootstrap modal--> <link rel="stylesheet" href="bootstrap-modal/bootstrap ...

Initiating an AJAX call to send a PHP request from one webpage to another

I am currently in the process of setting up my Instagram API site to allow users to click on photos and "like" them. I have a like.php script in place to handle the 'like' action and communicate with the API. Ideally, when a user clicks on an ima ...

Missing ghost image appears when you drag and drop the file

New to JavaScript... As a rookie in coding, I often get interesting requests from my partner who is a kindergarten teacher. Recently, she asked me to create a "Function Machine" for her classroom activities. With some trial and error, I managed to put tog ...

JavaScript, keep it easy: globalize

Looking for help with a simple regex expression... document.getElementById('testing').value=f2.innerHTML.replace(">",">\n"); I'm having an issue where it only stops after the first line break. How can I make it work for the enti ...

Achieving consistent hover effects in both Firefox and Chrome with CSS

Currently, I am faced with a dilemma regarding some CSS hover effects and table formatting. Despite successfully implementing most aspects of the design, there is an issue with how different browsers interpret padding within elements during color changes o ...

Converting request.body into a string before building a JSON object

Currently facing a challenge with extracting JSON data from a string. The request body is in string format, but I am having difficulties parsing out the data correctly. This is what I have: String to parse: uniq1.data.data.data 7 bleep\nuniq2.data.d ...

aligning a form vertically in a container

<div class="parent"> <div class="left-child"> <img src="logo.jpg" alt="logo"> </div> <div class="right-child"> <form action="#"> <input type="text" value="search"> &l ...

Applying CSS dynamically to a mat-cell in Angular 6

In my material table, I need to apply specific CSS classes based on the content of the cell. https://i.sstatic.net/YN6EO.png These are the CSS classes that I am using .status-code{ flex: 0 0 10% !important; width: 10% !important; } .status-code- ...

Unable to successfully transfer JSON data from JavaScript file to .NET controller using AJAX POST request

I'm facing an issue where I am trying to send a JSON string from the client (js) to the server (.NET controller) using AJAX post. However, when the data reaches the controller, the list is empty with a count of 0, which is puzzling me. JS Code btnAd ...

Utilizing display: flex for creating table padding

Hey there! I'm currently working on a webpage layout that includes a logo and a table. To achieve this, I've used display flex for the wrapper element. However, I've noticed that when viewing the page on mobile devices, the padding of the ta ...

`Display PowerPoint file within your internet browser`

I need assistance with viewing pptx files in a web browser that have been uploaded to a specific location using .net core. (approximately 40 mb) (approximately 4 mb) While attempting to view both files using the code below, I am able to see the MonitorP ...

Achieving the extraction of a particular string from an HTML element using JavaScript

<input id="WD01B3" ct="CB" lsdata="{2:'WD01B4',4:'Any',20:'\x7b\x22WDA_TYPE\x22\x3a\x22DROPDOWN_BY_KEY\x22,\x22WDA_ID\x22\x3a\x22ABCA950297D2C0C432BAB9BB ...

Transmitting a multidimensional array in JavaScript to an AjaxPro method on a front-end webpage: A step-by-step guide

Imagine a scenario where I have a C# method that requires an array parameter: [AjaxPro.AjaxMethod] public int Test3(string[,] array) { return array.Length; } Next, I define a multidimensional array on the front page using JavaScript: var array = ...

Struggle between Angular and fundamental CSS principles

Upon following the steps below, I aim to achieve my desired grids: How to set auto-margin boxes in flexible-width design using CSS? The solution provided is effective when implemented outside of Angular. However, when inserted inside an Angular component ...

How to eliminate a hyperlink from an HTML element with the help of JQuery

Recently, I was assigned to revamp a website for the company I work for. However, upon closer inspection, I realized that the website is quite messy and relies heavily on templates, resulting in certain elements being auto-generated as active links. The i ...