Adjust the transparency of the image when hovered over, but keep the text in the center

How can I change the opacity of an image on hover and simultaneously display text in the center of the image?

This is my code:

<div class="col-md-4 j-t">
    <div class="middle">
        <div class="text-middle">Play</div>
    </div>
</div>

CSS:

.middle {
    transition: .5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    text-align: center;
}

.text-middle {
    background-color: white;
    color: black;
    font-size: 16px;
    padding: 16px 32px;
}

.j-t {
    height: 315px;
    background: url("pictures/golden_cut.jpg") center center no-repeat;
    background-size: cover;
    transition: .5s ease;
    backface-visibility: hidden; 
    opacity: 1;
}

.j-t:hover {
    opacity: 0.3;
}

.j-t:hover .middle{
    opacity: 1;
}

However, when implemented as is, the text in the middle is also affected by the 0.3 opacity from the image. How can I make sure the text remains at full opacity?

Your assistance would be greatly appreciated.

Answer №1

Avoid setting opacity on hover; instead, modify the background color by using background: rgba(0, 0, 0, 0.4)

Answer №2

Apologies if my code is not perfect as it has not been tested, but I have encountered this issue before. The opacity of the parent element will affect all its child elements. To solve this, move "text-middle" outside of "middle" and make your j-t column relatively positioned. Then, position "text-middle" on top of "middle" using positioning (both elements will be positioned in relation to the parent - j-t col). This way, the hover effect will only impact ".middle" and not ".text-middle".

<div class="col-md-4 j-t">
    <div class="middle"></div>
    <div class="text-middle">Play</div>
</div>

.middle {
    transition: .5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    text-align: center;
}

.text-middle {
    position: absolute;
    top: -50%;
    background-color: white;
    color: black;
    font-size: 16px;
    padding: 16px 32px;
}

.j-t {
    position: relative;
    height: 315px;
    background: url("pictures/golden_cut.jpg") center center no-repeat;
    background-size: cover;
    transition: .5s ease;
    backface-visibility: hidden; 
    opacity: 1;
}

.j-t:hover {
    opacity: 0.3;
}

.j-t:hover .middle{
    opacity: 1;
}

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

Issue with Bootstrap 5 justify-content causing misalignment and inability to vertically center content

I've encountered a coding challenge while working with Bootstrap 5: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3c31312a2d2a2c3f2e1e6b706e706c">[email protected]& ...

Cordova - Fixed elements flicker/blink when scrolling

I have exhausted all possible solutions, ranging from -webkit-transform: translate3d(0,0,0); to -webkit-backface-visibility:hidden but none of them seem to resolve the issue of an element flickering/blinking/disappearing when scrolling on iOS with - ...

What could be the reason for the malfunctioning toggle button on the Bootstrap navbar?

I'm having trouble with my bootstrap navbar. I've added the .sidebar-collapse class to the body tag so that when I click on the toggle button, the data should appear on the right side. However, this isn't happening. Can anyone spot what&apos ...

Setting a title for an ashx file: A step-by-step guide

Is there a way to change the page title for the opened window when a user views an ashx file in the browser? Currently, when users click on a document that opens in a new tab (Word, Excel, PDF, etc.), the page title shows something like "file.ashx?id=5". I ...

What is the preferred method for writing `*zoom: 1;` in CSS?

Trying to create my code, I decided to borrow some CSS files from older code. However, upon running yarn build I encountered several errors like: ▲ [WARNING] Expected identifier but found "*" [css-syntax-error] <stdin>:122:2: 122 │ * ...

The background color should only cover a specific percentage of the element

Here is the current structure I have: <li class="myclass" ng-class="myAngularClass"> <div> div1 <div> div2 </div> </div> </li> The li element has dynamic dimensions and width. The class "myAngularClass" i ...

Envelop the phrases onto a fresh line

I'm having trouble getting the text inside a div to display correctly. When the text is too long, it just keeps stretching instead of breaking into new lines. How can I make sure that the content displays as a block within the div? Thank you. .box ...

**Issue Resolved:** Looking to have the form results appear only when a valid input is submitted; otherwise, the

As a newcomer to PHP, SQL, and HTML, I've been learning for around 3 months now. Can someone assist me in achieving the following: I would like my form to display the results table only if a valid search has been submitted, showing only the database r ...

Show the div in the right position for both desktop and mobile screens, utilize an accordion or consider shifting the div

My webpage is designed using bootstrap 4 and showcases images of our team members along with accordion functionality to display bios upon clicking the image. On a desktop screen, everything functions smoothly with four images of team members displayed in ...

Opera bug causing issues with Bootstrap's :active list item styling

Lately, I've been delving into Bootstrap and have come across an issue while using Opera. Upon the webpage loading, the navbar should display a list structured as follows: <li class="hidden active"> <li class="page-scroll"> <li class=" ...

There appears to be an issue with the onmousemove function

I am currently troubleshooting an issue with a script that I wrote for my button. Interestingly, the code works flawlessly when I test it on CodePen, but I encountered an error when I attempted to run it in VSCode. Even after trying to recreate the script ...

Progressive zoom effect applied to an image in a JavaScript slideshow

Can someone assist me in replicating the zooming effect demonstrated in this code snippet: `http://codepen.io/docode/pen/EjyVQY`. I am specifically focusing on the zooming feature of this slideshow. How can I replicate this zooming effect in JS rather t ...

Enhancing Your WordPress Menu with Customized Spans

I have a WordPress menu structured like this: <div id="my_custom_class"> <ul class="my_custom_class"> <li class="page_item"><a href="#">page_item</a> <ul class='children'> <li class="page_item chil ...

How can I extract the HTML value of a JSON array element using jQuery?

My jQuery function takes PHP JSON encoded data as an argument. function html_modify(html) { var str = JSON.stringify(html); var arr = $.parseJSON(str); var tot = ''; $.each(arr, function(i, val){ if(i == 'options') { ...

What are some ways to address the performance challenges associated with resizing images for responsive design?

When it comes to certain images, I find that scaling them based on the page size is the best way to make them responsive. <img class="img_scale" src="img.png" alt"this img doesn't have width and height definition"> In my CSS: .img_scale{wid ...

Messing up Bootstrap ES6 JavaScript code leads to the problem of the "Modal redeclared" error

Encountering the issue of "Modal redeclared" when attempting to minify Bootstrap 4 JS code using Grunt. Discovered these are due to ES6 so found the ES6 Uglify for Grunt. Current dependencies include: "bootstrap": "~4.0.0", "grunt": "~1.0.1", "grunt-contr ...

Cover the entire page with a solid background color

Is there a way to extend the green color from top to bottom on the page? Also, how can I move the CRUD section to the left? https://i.sstatic.net/pXxhl.png I'm having trouble filling the entire page with the content. Below is my code written in Vue ...

``Why Ionic 3 Popover Sizes Should Adapt to Different Screen

Currently in my Ionic 3 project, I am utilizing a popover with a set height using the following code snippet: editOpty(rw){ let popover = this.editOptyPopup.create(EditOptyPopoverComponent, rw, { cssClass: 'edit-opty-popover'}); popover ...

Tips for including data labels in a scatter plot using d3.js

I am currently studying d3.js for data visualization and I have chosen to work with the titanic dataset My objective is to incorporate passenger names into my visualization so that when a cursor hovers over a point, the person's name is displayed. H ...

Enhancing ag-grid with alternating row group colors following row span

My data structure is shown below: https://i.stack.imgur.com/fy5tn.png The column spanning functionality in ag-grid is working for columns 1 and 2 as expected. However, I am looking to implement alternate row colors based on the values in column 2 (animal ...