I'm having some trouble with the scale() property when trying to hover over an image

I am facing a challenge with styling an image that is contained within a div with the class "image-wrapper". Here's what I'm attempting to achieve:

  1. Show the content of ".image-wrapper::before" when hovering over the image to act as a title for the image
  2. Apply the CSS property "transform: scale(1.1)" on the image so that it slightly enlarges when hovered over

My Description of the Issue

To display a title inside the image (part 1) using "::before", I couldn't apply it directly to the "img" tag because they are replaced elements. So, I wrapped the "img" in a "div" with class "image-wrapper" and used "::before" on the "div", which worked as expected. However, I'm struggling to implement both properties simultaneously for the second part. It's quite confusing for me.

.image-wrapper {
  border: 2px solid black;
  display: inline-block;
  overflow: hidden;
}

.image-wrapper::before {
  content: 'Hello';
  height: auto;
  width: auto;
  padding: 4px;
  margin-left: 8px;
  background: rgba(0, 0, 240, 0.3);
  color: white;
  font-size: 20px;
  position: absolute;
  opacity: 0;
}

img {
  width: 100%;
}

img:hover {
  transform: scale(1.1);
  transition: all 0.2s;
  cursor: pointer;
}


/*HERE IS THE PROBLEM*/

.image-wrapper:hover::before {
  opacity: 1;
}
<div class="image-wrapper">
  <img src="https://images.unsplash.com/photo-1624215824600-ed3118b76873?ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDMwfDZzTVZqVExTa2VRfHxlbnwwfHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
</div>

Answer №1

The information is mostly contained within your code; however, the before pseudo-element will not be visible above its element unless a higher z-index value is assigned to it.

This snippet also adjusts the CSS slightly so that it triggers the hover effect on the image-wrapper class for both the "Hello" text and the scaling of the img element.

.image-wrapper {
  display: inline-block;
  overflow: hidden;
}

.image-wrapper::before {
  content: 'Hello';
  height: auto;
  width: auto;
  padding: 4px;
  background: pink margin-left: 8px;
  background: rgba(0, 0, 240, 0.3);
  color: white;
  font-size: 20px;
  position: absolute;
  opacity: 0;
}

img {
  width: 100%;
}

.image-wrapper:hover img {
  transform: scale(1.1);
  transition: all 0.2s;
  cursor: pointer;
}

.image-wrapper:hover::before {
  opacity: 1;
  z-index: 100;
}
<div class="image-wrapper">
  <img src="https://images.unsplash.com/photo-1624215824600-ed3118b76873?ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDMwfDZzTVZqVExTa2VRfHxlbnwwfHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
</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

Live CSS editing tool

Are there any Javascript libraries or plugins available that enable real-time modification of CSS classes/styles on a webpage? ...

php code for bulk insert into mysql database

Looking for help on optimizing my PHP code with add code implementation, here's what I have so far in my files: mysql_select_db("koro", $con); $sql = "INSERT INTO students (IDNUMBER, LNAME, FNAME, MNAME, GRADEYR, ADDRESS) VALUES ('$_POST[INAME ...

Designing a solid sidebar of a specific width alongside a flexible container for content that adapts to different screen sizes using

I have a sidebar that is fixed at 200px on the left side of the page, and a responsive content container on the right side. To center the content in the right container, I used the following CSS: .sidebar { width: 10%; position: fixed; height: 100%; bac ...

Retrieve documents from a nearby directory using an online platform

I have a gridview in an aspx page that needs to display all the xml files from a folder on the client machine. Is there a way to retrieve the contents of a folder using the directory path? I currently have code that works when running it locally, but I am ...

Primeng dropdown component with Right-to-Left layout support

Can the PrimeNG drop-down icon be positioned on the left side of the select? I attempted: <div class="ui-rtl" dir="rtl"> <p-dropdown></p-dropdown> </div> However, it did not function as expected. Are there any alternative so ...

The Bootstrap Grid Leaves Blank Spaces Unfilled

View the screenshot here Take a look at the image above. I'm struggling to fill the empty space with div elements. I've experimented with display:inline-block and other display properties, but they haven't yielded the desired results. < ...

How to position one element relative to another using CSS

I need to strategically place four div elements in relation to another element. I have a rectangular div, and my goal is to insert four div elements at each of its corners. While I am aware of the CSS attribute "position: relative", it only allows me to ...

Problems arising from navigating between rtl and ltr modes on Internet Explorer 7

I have created a basic sliding animation for a div (developed from scratch without third-party plugins) that works well in IE10, FF, and Chrome, but has issues in IE 7. It still works in IE 8 though. HTML <div class="aboutus"> <div class="ma ...

Remove the ability for the user to delete if they are the rightful owner of the specific property

I am in need of showing a deletion option for a user who has created a specific exhibit. I have retrieved the current user's ID using the getCurrentUser service and obtained an array of exhibits containing a field called "userId". My goal is to compa ...

What is the method for keeping the first column of the table fixed in place?

Incorporating jquery EasyUI, I have created a table where I want the first column to remain static while the rest of the columns scroll when the screen size is reduced. Code snippet available here. Is there a way to freeze just the Title1 column? I attem ...

What is the most effective method for integrating Bootstrap CSS into an Angular project?

When incorporating a Bootstrap CSS file into an Angular project that has already been added using yarn add <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2909d9d868186809382b2c6dcc3dcc3">[email protected]</a>, th ...

What is the secret behind the functionality of the download stats page on the Firefox website?

Check out the Mozilla website for real-time download statistics on Firefox: I noticed that there doesn't seem to be a continuous connection in Firebug. How are they retrieving and displaying this live data? What method is being used for this functio ...

How can I use AngularJS to filter ng-repeat values based on the input ng-model value of another field?

Can I Repeat Values Based on Another Input NG-Model Value in AngularJS? There is one input value, let's say Social Psychology, and the corresponding ng-model for this value is kpcategory. I am using ng-repeat="question in questions" to list all ...

Display or conceal a div based on the input value

Displaying div#cool should be triggered when the user enters the term "cool" into the input field. Similarly, showing div#good should be activated when the user types "good", and this functionality should operate seamlessly in real-time. ...

Bind ng-model with button click in AngularJS

I am encountering a frustrating issue with data binding utilizing ng-model and button. The functionality of my website is as follows: The HTML site showcases a list of projects (imported from an external .json file). Each row contains an Edit button tha ...

Troublesome code with Ajax, Jquery, and PHP is causing issues

I've been trying to send an array from php to js using ajax, but for some reason it's not working no matter what I try. I'm convinced it must be a simple fix, but I seem to have exhausted all my options. <!doctype html> <html lang= ...

Using Array Data Format to Customize Color in Highcharts Funnel Chart

I have included the funnel visualization code that I've been working on. $(function() { var dataEx = [ ['1 Visit', 352000], ['2 Visits', 88000], ['3+ Visits', 42000] ], len = dataEx.length, ...

Consolidate the indexes of arrays into a single object

Is there a way to combine array indexes into one object using a key-value pair format, like {authors[i]: quotes[i]} in my example? If you'd like to review the code, please visit my Codepen: http://codepen.io/anon/pen/Ndezeo Thank you. ...

Adding javascript code to HTML using Express

Currently, I am in the process of building a web application utilizing the Express framework to create Impress.js presentations and implementing a visual editor for them. I have configured the app.js to only fetch the .html file, but my objective is to inc ...

State of Active Scroll group

How can I dynamically add the "active" class to a link based on which section it has scrolled to? I want the active state to be applied without a hover effect, only when the corresponding section is currently in view. #pagemenu{ display: block; positio ...