Connecting an image to its corresponding content through hover effect

Can someone please assist me? I am trying to create a hover effect on an image with an "add to cart" link similar to what is shown here:
I seem to be having trouble with the code, could you provide some guidance?

.hunderter {
    background: url(http://neuronade.com/wp-content/uploads/bfi_thumb/Shop-Produktbild_Bundle_englisch-min-mhvx28jvtfhpc0dhkugvgrvgvg1h17l9ze3zkr118a.jpg);
    display: block;
    background-size: 100%;
    border: transparent !important;
    background-color: transparent !important;
}

a.hunderter:hover {
    background-color: rgba(100,230,230,0.5);
    background-position: 0 0;
    content:"⏵ ➡Add to Cart";
}
<a href="/?preview_id=4212&preview_nonce=a62d30b2b8&preview=true&add-to-cart=5767" class="hunderter"></a>

Answer №1

Before we dive into the main answer, here are some important points to consider:

1) It is recommended that your image is included in the HTML code rather than in the CSS. This allows for easier updating and ensures the image is considered part of the page content.

2) When using unicode characters in CSS, be sure to add a backslash before the number. For more information, refer to this resource.

Now, let's address your primary question...

Option 1: To keep the CSS content value consistent, you can utilize CSS pseudo elements like ::after and/or ::before. These elements are essential for achieving the desired effect in CSS, as demonstrated in the following code snippet.

.cart-item-link {
  position: relative;
  display: inline-block;
  border: transparent !important;
  background-color: transparent !important;
}

.cart-item-link:hover::before {
  content: "Add to Cart";
  position: absolute;
  bottom: 20%;
  left: 50%;
  transform: translateX(-50%);
  bottom: 20%;
  padding: 15px;
  background-color: rgba(100, 230, 230, 0.5);
  min-width: 80%;
  text-align: center;
}
<a href="/?preview_id=4212&preview_nonce=a62d30b2b8&preview=true&add-to-cart=5767" class="cart-item-link">
  <img src="http://neuronade.com/wp-content/uploads/bfi_thumb/Shop-Produktbild_Bundle_englisch-min-mhvx28jvtfhpc0dhkugvgrvgvg1h17l9ze3zkr118a.jpg" alt="" />
</a>

Option 2: An alternative approach is to place the "add to cart" text inside a <span> tag and style it using CSS with a different selector. This method is illustrated in the following code snippet.

.cart-item-link {
  position: relative;
  display: inline-block;
  border: transparent !important;
  background-color: transparent !important;
}

.cart-item-link > .cart-item-info {
  display: none;
  position: absolute;
  bottom: 20%;
  left: 50%;
  transform: translateX(-50%);
  bottom: 20%;
  padding: 15px;
  background-color: rgba(100, 230, 230, 0.5);
  min-width: 80%;
  text-align: center;
}

.cart-item-link:hover > .cart-item-info {
  display: block;
}
<a href="/?preview_id=4212&preview_nonce=a62d30b2b8&preview=true&add-to-cart=5767" class="cart-item-link">
  <span class="cart-item-info">Add to Cart</span>
  <img src="http://neuronade.com/wp-content/uploads/bfi_thumb/Shop-Produktbild_Bundle_englisch-min-mhvx28jvtfhpc0dhkugvgrvgvg1h17l9ze3zkr118a.jpg" alt="" />
</a>

Answer №2

To tackle this issue, you can incorporate a <span> element that only becomes visible when hovered over:

.hunderter {
  position: relative;
  background: url(https://unsplash.it/200/300);
  display: block;
  width: 200px;
  height: 100px;
  background-size: 100%;
}
.hunderter span {
  position: absolute;
  bottom: 0;
  visibility: hidden;
}
a.hunderter:hover span {
  background-color: rgba(100, 230, 230, 0.5);
  visibility: visible;
  color: white;
}
<a href="#" class="hunderter">
  <span>&#9205; &#10145;Add to Cart</span>
</a>

Answer №3

It seems like you're looking for a way to display a hand cursor on hover. You can achieve this by using the following CSS:

cursor:pointer

a.hunderter:hover {
    background-color: rgba(100,230,230,0.5);
    background-position: 0 0;
    content:"&#9205; &#10145;Zum Warenkorb hinzufügen";
    cursor:pointer
}

.shop_bar{
background:black;
  color:white;
  opacity:0;
  transition:all 0.5s;
}
.product_img:hover .shop_bar{
opacity:1;
}

Edit: The code snippet below demonstrates adding a bar at the bottom of the product image. When you hover over the image, the bar with some details will appear. You can customize the details within the HTML tag.
<div class="product_img">
  <img src="https://spacewallet.de/wp-content/uploads/2016/06/businessblack-1-ohne-Rand.jpg">
  <div class="shop_bar">
    Buy this
  </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

How to dynamically change the border-top color of an <a> tag in a menu using a jQuery loop

I am interested in adding a colorful border-top to my menu using jQuery. I know this can be achieved with HTML by including style="border-top-color: red;", but I want to explore the jQuery method. Here is what I have attempted so far: var colors = [ ...

Ways to present a pop-up dialog box featuring word corrections

I have developed a word correction extension that encloses the incorrect word in a span element. Upon hovering over the word, a drop-down menu with possible corrections should be displayed. However, my current code is not functioning properly. How can I en ...

Expanding the width of an image beyond its parent <div> without compromising on vertical space

Is it possible to have a child <img> wider than its parent div, while maintaining proportional width and preserving vertical space? Using position:absolute; might move the image out of the normal document flow, but I want to keep the vertical space ...

Having issues with my JavaScript timer - seeking assistance in troubleshooting the problem

Trying to set a timer for my little game, but facing some difficulties. The timer causes the balance to randomly increase from 0 to 13000 + some extra amount. <script> var coins = 0; var speed = 1; </script> <center> <h4> ...

What is the best way to connect my 'Projects' folder to app.js within a React application?

import "bootstrap/dist/css/bootstrap.min.css"; import Navbar from './components/Navbar'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './components/Home'; import Project ...

Is it possible to animate share buttons using Framer Motion but staggering siblings?

I have a Share Icon that looks like: I'm looking to display the first 5 icons, with the 5th icon being the share icon. The remaining icons should appear below the share icon and expand to the right when someone taps or hovers over it (either tap or h ...

Struggling to position Bootstrap navbar links completely to the right side

The navigation bar link items are not aligning all the way to the right when using mr-auto. It appears like this Here is my HTML template: <nav class="navbar navbar-default navbar-expand-lg navbar-custom"> <div class="navbar-co ...

The width of table cells within a div is completely disregarded

The cell width property seems to be ignored in this situation. Despite trying numerous approaches, the cells are splitting into equal portions and not respecting the specified width. Inspecting the code did not reveal any unexpected inheritance that could ...

Navigate the cursor beyond the span within the content that is editable

I am currently developing a tag input system for a template builder. My main focus right now is on assisting the editor in distinguishing between regular text and formatted text. I am structuring it similar to WordPress shortcodes, where a templated elemen ...

Using CSS transitions to animate elements with specified opacity values

My goal is to transition buttons from opacity 0 to opacity 1 with a delay, but I am facing an issue. Some of these buttons have the "selected" class which gives them an opacity of 0.35. However, when the transition starts, the buttons with the "selected" c ...

The clingy navigation bar hinders smooth scrolling to the content

My website has an image where users can click on elements to scroll down to text. However, I'm facing a problem because there is a sticky menu at the top of my website. How can I ensure that the scrolling works correctly with the sticky menu included? ...

Creating a blur effect on an image tag when hovering using CSS and HTML

I am currently developing a Portal website for one of my classes, and the template provided by my professor utilizes the <picture> tag to adjust images depending on the browser's size. My goal is to add a blur effect and darken the image while d ...

Can a hyperlink exist without a specified href attribute?

One scenario could be when I receive this specific code from a third-party source <a class="cs_import">Add contacts from your Address Book</a> Curiously, the link "Add contacts from your Address Book" does not redirect to any page as expected ...

Using a JavaScript loop to modify the color of the final character in a word

I am curious to find out how I can dynamically change the color of the last character of each word within a <p> tag using a Javascript loop. For example, I would like to alter the color of the "n" in "John", the "s" in "Jacques", the "r" in "Peter" ...

Each Browser Approaches Simple Search Fields in its Own Unique Way

In the process of creating a search field with an input field and an svg icon, I encountered different browser behaviors in Chrome, Firefox, and IE11. Chrome – the preferred version https://i.sstatic.net/ikBgD.png Firefox https://i.sstatic.net/9lcqQ.pn ...

How can I make my div disappear when I click outside of it using jQuery? I am finding the solution on Stack Overflow to be confusing

Utilizing jQuery to conceal a DIV upon clicking outside of it Although I've heard positive feedback about this method, I'm uncertain about its functionality. Can someone provide an explanation? I understand that .has() identifies elements contai ...

4 products - all discounted by 25% - featuring stylish borders and margins

After extensively searching the internet, I have come across similar issues but not quite the same as mine. I have a list that I want to display in table form within a parent div taking up the entire width (100%). I aim to show 4 items per row and attempt ...

Step-by-step guide on replacing a visible div with a new div when a button is clicked

I have organized my website with links on the left sidebar and matching content in the right sidebar, structured within appropriate div elements. My goal is to display only one div at a time when a user clicks on one of the links located on the left side. ...

The CSS Grid container fails to resize based on the content inside

I am facing an issue with the grid element inside a parent container with display: grid;. The parent container has a maximum size and allows scrolling for the grid if necessary. The problem is that the grid element does not expand to fit its children. Whe ...

How can I modify the color of a div when a validation error occurs?

I have recently completed a contact form with validation using the constraint validation api. Although the files are functioning as intended, I am curious if there is a method to make the error boxes turn red when an error occurs and white (or hidden) when ...