The z-index isn't functioning properly when trying to layer an image over a text

Check out my JSFiddle example

I'm having an issue with positioning an image and a text box using z-index. The image is scaling over the text box when I want the text box to be on top of the image.

Here's the HTML code:

<a href="#">
  <img src="http://images.meredith.com/content/dam/bhg/Images/2009/07/101422234.jpg.rendition.200sq.jpg" />
  <div class="text">
    This is some text.
  </div>
</a>

And the CSS styling:

a {
  height:400px;
  width:200px;
  display:block;
  text-decoration:none;
  color:black;
  overflow:hidden;
}

img {
  transform: scale(1.1);
}

a:hover .text {
  color:white;
  background-color:black;
}

.text {
  text-align:center;
  height:200px;
  margin-top:-4px;
  background-color:white;
  transition:all 1s ease;
}

Answer №1

When you apply the transform property to the img, it creates a new stacking context. To ensure that the element with the class .text appears on top of the img, you can add position: relative to .text to create a new stacking context for that element.

a {
  height: 400px;
  width: 200px;
  display: block;
  text-decoration: none;
  color: black;
  overflow: hidden;
}

img {
  transform: scale(1.1);
}

a:hover .text {
  color: white;
  background-color: black;
}

.text {
  text-align: center;
  height: 200px;
  margin-top: -4px;
  background-color: white;
  transition: all 1s ease;
  position: relative;
}
<a href="#">
  <img src="http://images.meredith.com/content/dam/bhg/Images/2009/07/101422234.jpg.rendition.200sq.jpg" />
  <div class="text">
    This is some text.
  </div>
</a>

Answer №2

Try using this code snippet:

<html>
<head>
<style>
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
opacity:.5
}
</style>
</head>
<body>

<h1>Heading Goes Here</h1>

<img src="http://example.com/image.jpg" width="600" height="200">

When the image has a z-index of -1, it will be positioned behind any text on the page.

Answer №3

Give it a shot by using the z-index property in combination with position:absolute;

.text {
  text-align:center;
  height:200px;
  margin-top:-4px;
  background-color:white;
  transition:all 1s ease;
  position:absolute;
  bottom:0px;
  z-index:99;
}

a {
  height:400px;
  width:200px;
  display:block;
  text-decoration:none;
  color:black;
  overflow:hidden;
  position:relative;
}

img {
 // transform: scale(1.1);
}

a:hover .text {
  color:white;
  background-color:black;
}

.text {
  text-align:center;
  height:200px;
  margin-top:-4px;
  background-color:white;
  transition:all 1s ease;
  position:absolute;
  bottom:0px;
  z-index:99;
}
<a href="#">
  <img src="http://images.meredith.com/content/dam/bhg/Images/2009/07/101422234.jpg.rendition.200sq.jpg" />
  <div class="text">
    This is some text.
  </div>
</a>

Answer №4

Here is the solution that I believe fits your needs:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.wrapper {
  width: 500px;
  height: 500px;
}

.wrapper img {
  position: absolute;
  top: 0;
  left: 0;
}

.wrapper div {
  color: white;
  background-color: rgba(0, 0, 0, 0.6);
  position: absolute;
  opacity: 0;
  transition: all .5s ease;
  width: inherit;
  height: inherit;
  text-align: center;
}

.wrapper:hover div {
  opacity: 1;
}
.wrapper div span {
  font-size: 25px;
  line-height: 25px;
  position: relative;
  top: calc(50% - 12.5px);
  transform: translateY(-50%);
}
<div class="wrapper">
  <img src="http://placehold.it/500" />
  <div>
    <span>Text on Image</span>
  </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

Tips for utilizing data attribute in jQuery programming

How can I modify my jQuery code to select the data attribute "data-price" instead of the value attribute? Sample HTML code: <span id="priceToSwap3"">$238</span> <select id="trapsize"> <option data-price="238">item1</option> ...

Using Multiple Submit Buttons in an HTML form with Express.js and Node.js

I'm currently implementing a like/dislike feature with two buttons on the HTML side. <form method="post" name="ratings"> <input type="submit" name="vote" value="like"> <input type="submit" name="vote" value="dislike"> </f ...

The HTML image tag is not functioning properly when using a symlinked path

Is there a method to display symlinked images correctly using the src attribute? The server is powered by express, and the symlinked image is served using static file. When using the symlink file <img src="http://server:3000/public/1-symlinked. ...

Issues with Twitter Bootstrap Tabs not functioning correctly on Ruby on Rails with HAML

I am facing an issue with twitter bootstrap tabs in my (HAML) ruby on rails project. Although the tabs are appearing, the content does not change when I click on different tabs. Additionally, some unexpected statements are showing up at the bottom of the f ...

Struggling to deploy my Laravel application with Tailwind CSS on the frontend. However, running "npm run dev" results in an error

While running the npm command `npm run dev` in cmd, I encountered an error stating that I require PostCSS v8 even though I already have PostCSS v8.3 installed. Could it be possible that Tailwind CSS specifically needs version 8 despite newer versions being ...

Modifying the color scheme of the table background and text

After finding a code example online to assist with creating a table in my project, I am faced with the challenge of changing the background color of the white rows to match the dark blue used for others. Additionally, I cannot figure out how to change the ...

The .fill attribute in SVG seems to be having trouble functioning as intended

Could someone please clarify why I am unable to modify the fill of this svg: <div class="container"> <ul class="todo"> <li> This is an item <div class ="buttons"> <button class="remove"> <svg ...

Can you provide the URL for the mailto function on a webpage?

Is it possible to activate the "mailto" email address directly via URL on a webpage? For example, if you have an email address listed as [email protected], would clicking the URL in the browser open the mail client just like clicking the email address its ...

Mapping the preselected values of multiple input fields into an array in Angular 6: A step-by-step guide

When submitting a form with input fields, I need to capture the selected values into an array format like {"userid":1,"newstatus":[1],"mygroup":[1,2,3]}. I attempted using ngmodel but encountered issues. Below is the code snippet: home.component.html & ...

What is the best way to customize the MenuProps of Material UI Select component using createTheme?

I'm trying to update the dropdown menu style of my Material UI Select component using createTheme, but I'm having trouble getting it to work. https://i.sstatic.net/zpVjW.png Here is the code I have so far. Can you spot what might be causing the ...

What should be done when HTML5 anchor tag downloads fail due to a long base64 string in the src attribute?

batchDownloadImages() { const aTagDownload = [ { download:'foo', href:'HD image base64 string from canvas.toDataUrl()' }, { download:'bar', href:'HD image base64 string from canvas.to ...

Tips for displaying two HTML input elements side by side in a single row

This block of code HTML <input type="text" name="category" id="category" value="">&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="category_1" id="category_1" value="" tabindex="1"></div> Despite multiple attempts such ...

Is there a way to automatically remove a video using JavaScript once it has completed playing?

This is my HTML section: <section id ="information"> <video id ="videoName" width="800" height="400" controls onplay="myAnimationPicture()""> <source src="video/videoName.mp4" ; type="video/mp4"> Your browser does not support the ...

iPhone images are taking forever to load in web applications and Safari

I am currently developing a Progressive Web App (PWA) for Android and iOS. While I have not encountered any issues with image loading on Android, there seems to be a delay when loading images on iOS Safari and the web app version. Specifically, when displa ...

prevent clicking on the div element

I have been attempting for quite some time to disable a click on a specific div element. The purpose behind this is to prevent unauthorized users from triggering certain events that are activated when the div is clicked. In my previous attempt shown below ...

Implementing a breadcrumb-style back button using jQuery

The menu is currently displayed on the left side without any issues. My next task involves implementing a back button functionality. When a user clicks from Demo1 to Demo 1.1, additional options like Demo 1.1.1 and Demo 1.1.2 will appear. To facilitate nav ...

Show full-screen images on click using Ionic framework

Currently, I am working on developing a mobile app using the Ionic framework. I have created a layout that resembles the one shown in this Card Layout. My question is: How can I make the card image display in full screen when clicked by the user and retur ...

Trouble arises in trying to match a JavaScript variable with JSON data

I have a table saved as a string in a JSON data within localstorage. My goal is to compare one of the fields stored here with a separate JavaScript variable. Here's what I attempted: var goalsString = localStorage.getItem("goals"); var goalsObject = ...

Using a combination of a bootstrap class and a custom CSS class to enhance your website design

How do I apply two different classes? I have two HTML select fields where the user can choose either repair or another option in the first one. When the user selects one, the second field should appear. But it also needs to have a Bootstrap class: form-con ...

methods for modifying multiple elements with getElementById

I am facing an issue where only the first getElementById code is working and changing the value of the ID. Here is my HTML: <li ><a href="#Home" id="activehome" onclick="load_home()">HOME</a></li> <li ><a href="#Products" ...