What is the best way to display letter p when hovering over an image?

Even though the issue of overlapping has been addressed before, I am struggling to center the text on top of the image when hovered. I have experimented with z-index, relative and absolute positioning, but I'm still seeking assistance.

.flex-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    margin: 0;
    padding-left: 7%;
    padding-right: 7%;
    padding-top: 25px;
    padding-bottom: 50px;
    justify-content: center;

}
.flex-container div {
    margin: 0;
    position: relative;
    margin-bottom: -5px;
    font-size: medium;
    text-align: center;
    z-index: 0;
}

.flex-container div:hover {
    filter: brightness(50%);
}

.flex-container div p {
    display: none;
}

.flex-container div:hover p {
    position: absolute;
    color: black;
    z-index: 1;
    text-align: center;
}
<div>
    <a href="Printing/menus.html">
        <img src="../Images/menus-01.png" alt="Printing" style="width:100%; height:auto;"/>
    </a>
    <p>Menus</p>
</div>

Answer №1

It's recommended to define the text CSS first before setting up the hover effect. Additionally, don't forget to apply styles to the container in your code.

If this is what you intended:

HTML :

<div class="container">
  <img src="../Images/menus-01.png" alt="Image" style="width:100%;">
  <div class="topTxt">Text on Center/div>
</div>

CSS :

.container {
  position: relative;
  text-align: center;
}

.topTxt {
  position: absolute;
  display: none;
  top: 0;
  left: 50%;
  transform: translate(-50%, 0%);
}

.container:hover .topTxt {
  display: block;
}

OR use JavaScript :

document.getElementsByClass("container").addEventListener("mouseover", function() {
  document.getElementsByClass("topTxt").style.display = "block";
});

For a better visual representation of your request, here's a snippet:

... (CSS and HTML code snippets continue)

To prevent the hover effect from affecting the text color, place the text before the image so they are siblings rather than parent-child elements.

Answer №2

Perhaps this is the solution you've been searching for:

How to overlay text on images using HTML?

The recommended approach involves utilizing a "div" tag instead of an "img" tag. See the example below:

.image {
       width:400px;
       height:400px;
       background-image: url(http://lorempixel.com/output/cats-q-c-640-480-4.jpg);
       background-size:cover;
 }
<div class="image">Text on top of image</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

retrieve the data-task-IDs from the rows within the table

I am currently working with a table that looks like this: <table id="tblTasks"> <thead> <tr> <th>Name</th> <th>Due</th> ...

Dynamic change of table cell text color based on condition utilizing ng-if and ng-style

I am looking to create a table cell with alternating color combinations based on specific conditions. Here is the code snippet: <tr ng-repeat="x in data"> <td> <span ng-if="((x.time2 < x.time1) || (x.time2 < x.time3))" ng-style="colo ...

What is the best way to align an html table to the top of the page?

Can anyone help me with positioning an HTML table at the top of the screen in IE7 without the automatic top border? I am using HTML and CSS for this. I'm currently working on development in VS2008. ...

The columns in the table are hidden when resizing

There are three tables in my code. The first table does not have any margin set, while the next two tables have a margin-left property to slightly move them to the left side. However, when I resize the window, I'm unable to see the last column in the ...

Modify the background color of the <li> element without using JavaScript after 3 seconds

Is it possible to change the background color of a <li> element without using javascript or JQuery, only with CSS? I've seen incredible things done with CSS alone and believe this is achievable. My <li> element functions as a horizontal m ...

Proper alignment of div elements using Bootstrap

I am attempting to align 2 div elements side by side using Bootstrap code: <div class='col-12'> <div class='row'> <div class='col-6'> </div> <div class='col-6&a ...

The addClass method in jQuery is failing to append the specified class to the element

I'm currently working on a registration form that includes selecting your gender: My goal is to have the male icon turn blue when clicked, and the female icon turn pink. The color change currently works when hovering, but not when clicked. Here is th ...

Angular JS failing to display error messages

I'm experiencing difficulties displaying login validation errors with the following code. After clicking on the Login button, it redirects to the next page without showing error messages as expected. Any suggestions? index.html:- <!DOCTYPE ht ...

Issue with LESS CSS: Inability to divide two pixel units and provide a value without units

I want to temporarily strip the units from my variables @baseLineHeight and @baseFontSize so that I can divide them to get a relative line-height. Here is my attempt: @baseFontSize: 12px; @baseLineHeight: 18px; font: @baseFontSize~"/"@baseLineHeight/@baseF ...

Tips for stopping variables from leaking in JavaScript

I'm currently working on a JavaScript code for my task manager website. Each page has its own JS file, but I've noticed that the data saved in one file seems to leak over to the others. How can I contain these variables so that tasks don't s ...

iOS 8 home screen web apps with status bar overlay and footer bar integration

After installing a web application to 'home' and working with it, I noticed that a recent update to iOS has made the usual black status bar transparent and float above the web content below. In addition, there is an unseen horizontal bar at the ...

Highlight the title with a stylish CSS animation effect

I have a challenge with animating div tags once they come into view on the page. Thanks to waypoint.js, I have successfully achieved the 'in viewport' trigger, but now I need help with the animation part. Specifically, I am looking to add a grey ...

Safari disables any animation when redirecting or submitting a form

When a link is clicked, I have an animation that enlarges a div and fades out using jQuery. At the same time the link is clicked, a redirect is triggered by submitting a form to ensure speed. The redirect cannot be placed in the success function of jQuery& ...

The issue I am facing is that when I click on a checkbox, only one of them seems to respond

When I click the button, only the first checkbox event is being checked while the rest of them are not. Can someone please provide some guidance on how to fix this issue? $("#cascadeChange").click(function() { //alert("Clicked"); ...

Assigning the Style property to an element using a string that includes HTML tags

My HTML string is populated with elements such as button, li, span, and more. I am looking to add specific styles to buttons based on their class name. For example, if button.btn { some styles } and button.btn-success { some other styles } O ...

What is the best way to center a form within a jumbotron using CSS?

I'm struggling to replicate a Bootstrap V4 template where I cannot seem to align the form in the center within a jumbotron div. However, all other text is centered as expected. Even though I copied the code from a tutorial, it functions perfectly the ...

Is there a method to individually collapse each div?

Each question in the database has a corresponding button. When one of these buttons is clicked, only the answer to that specific question should be displayed. However, the issue currently is that clicking on any button reveals all the answers simultaneousl ...

CSS - vertical and horizontal lines vary in thickness depending on the design

I am working on creating a grid that is automatically generated in my Angular 2 application. Currently, it consists of 1px wide divs as vertical lines and 1px high divs as horizontal lines. However, there seems to be an issue with inconsistent thickness be ...

No Google Adsense ad in sight

Having an issue with my Adsense ad, the script is in the page source code but the ad itself is not appearing. You can check it out for yourself by clicking the link below. The ad should be displayed at the top of the topic. To view the page, click here: ...

AngularJS is used to vertically collapse three different div elements. This feature

I am facing an issue with the design of my page that contains three panels. Two of these panels have tables, and when viewing the page on a smaller display, it disrupts the layout. I am wondering if there is a way to add collapsible buttons to the panels s ...