Tips for inserting text onto an image using a hover effect

I have designed a section on my website where an image darkens upon hovering, and I would like text to appear centered both horizontally and vertically when the image is hovered over. However, despite my efforts, the text does not display on the image as intended.

Below is the HTML code snippet:

<div id="countries-hitched-div">
    <h1> Places Hitched </h1>    
    <div id="first" class="image">
        <h1 id="first-text"> India </h1>
        <div class="overlay"></div>
    </div>
</div>

And here is the corresponding CSS styling:

#first {
    background: url('Images/Home Page/first-quote-image-final.jpg');
    position: relative;
}

.image {
    width: 500px;
    height: 500px;
    position:relative;
}

.image:hover > .overlay {
    width:100%;
    height:100%;
    position:absolute;
    background-color:#000;
    opacity:0.5;
}

#first-text {
    position: absolute;
    color: black   
}

Answer №1

Apply the following modifications to #first-text:

.image:hover #first-text {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    height: 50px;
    text-align: center;
}

This will result in the absolute element being centered both vertically and horizontally within a relative element.

Answer №2

Insert the following snippet into your code to ensure that your overlay text is perfectly centered:

#first-text {
 text-align:center;
 position: absolute;
 color: black;
 width:100%;
 top:45%;
}

Answer №3

To begin with: Avoid adding too many new properties on :hover. It is recommended to set all properties on page load and only modify the necessary ones when :hovered or :focused.

Additionally, there are various methods to centrally align the h1:

  • Specify a line-height for the h1 and apply text-align: center (such as this example)
  • Utilize a table to center the h1 (similar to this)
  • Implement flexbox layout techniques (as shown in this guide)

Answer №4

To center the content, use text-align: center and for hover effects, adjust the opacity like this:

#first {
    background: url('Images/Home Page/first-quote-image-final.jpg');
    position: relative;
}
.image {    
    width: 500px;
    height: 500px;
    position:relative;
}
.image:hover > .overlay {
    width: 100%;
    height: 100%;
    position: absolute;
    background-color: #000;
    opacity: .5;
}
#first-text {
    position: absolute;
    color: black
}
 #first-text {
       text-align:center;
       position: absolute;
       color: black;
       width:100%;
       top:45%;
       opacity:0.0
 }
#first:hover #first-text{
    opacity:1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="countries-hitched-div">
        <h1> Places Hitched </h1>   
            <div id="first" class="image">
                <h1 id="first-text"> India </h1>
                <div class="overlay"></div>



            </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

The card-img-overlay in Bootstrap 5 seamlessly integrates into the following div container

Currently, I am in the process of creating a painting website layout for my CS50 homepage. However, there seems to be an issue with the background-color: rgba(0,0,0,0.7); not aligning correctly with the images within the card-group. <div class="car ...

ajaxform is not providing the correct response

There is a form below that logs into Instagram based on the response it receives. If the login is successful (returns "ok"), then it shows success, otherwise it should display an error state. However, in my case, it always returns a null state for some un ...

jQuery function failing to produce the intended results

I am trying to apply the active class to the last img element using HTML and jQuery Below is my code in HTML and jQuery: function initialSteps() { $(".row").find("column:last").find("img:first").className += " active"; // this does not work either ...

Upon loading the page, there is a sudden downward movement

I've been struggling with this issue for some time now. After the page finishes loading or is nearly done, it suddenly jumps down about halfway. What I'm hoping for and what I would expect is that the page stays at the top once loaded. Could so ...

Adding options to an HTML select dropdown menu using data stored in an array

Having reviewed several questions related to this issue, I am still encountering difficulties. Below is the code snippet that I am struggling with: <body> <?php //To begin, there is a form with a drop-down menu and a button, which will trigge ...

Check out the HTML display instead of relying on console.log

Need help displaying the result of a JavaScript statement in an HTML page instead of console.log output. I am new to coding! Here is the JS code snippet: $.ajax({ url: 'xml/articles.json', dataType: 'json', type: ...

What are the steps to create an endless scrolling feature?

I'm trying to create a slider with a horizontal scrolling effect, but I've hit a roadblock. How can I make the slider scroll infinitely? In my code, you can see that after Item 6, it stops scrolling and I have to scroll backward. However, I want ...

Emphasizing specific terms by changing them (but only within p tags)

After spending about 8 hours searching for a solution yesterday on various platforms, I almost gave up. But then I realized I should turn to this community for some much-needed help. So here is my first question - a big thank you to everyone who has helped ...

Can the placement of divs impact search engine optimization (SEO)?

My website is currently structured with div containers in the following order: Less important left side bar Less important right side bar The core center content at last I am concerned about the impact on SEO as the core content comes after the sidebars ...

Scrollable panel feature in Flexbox

I am attempting to create a design that expands to fill the screen, but allows for scrolling when the content exceeds the available space. My current approach using basic flexbox, as suggested in this answer, successfully fills the screen. However, overfl ...

JavaScript visibility disappearing intermittently

I have created a custom image viewer box that overlays a thumbnail gallery page. The image viewer should appear when a user clicks on a thumbnail. However, currently, the viewer only pops up briefly and then disappears again. I am looking for a way to mak ...

Issue with data(): "Error: The property 'length' cannot be read because it is undefined" (Vue.js)

Hey, I'm new to Vue.js and I think I might have made a silly mistake because I just can't figure out why I'm getting this error. Can someone please help me out? Here's the code snippet I'm working with: const bootstrap = require(" ...

Enhance the aesthetics of placeholder text in Semantic UI React using CSS styling

Currently, I am utilizing Semantic UI dropdowns within my React application to generate drop-down menus similar to the ones showcased in their documentation found here: The initial text displayed is "Select Friend", and it appears with a semi-transparent ...

Chrome and Safari disregarding navbar height dimensions

Recently, I encountered an unexpected issue in a local project that I've been working on. Two weeks ago, everything appeared correctly, but now Chrome and Safari are ignoring the height of the navbar. Surprisingly, Firefox is displaying it correctly, ...

I am interested in creating a design where two DIVs are layered on top of each other with one having transparency, allowing visibility through to the background DIV. This can be achieved using a combination of

Looking to enhance the visual appeal of my website, I decided to incorporate a background image within a div. To add more depth and creativity, I am now striving to overlay a semi-transparent black box on top of the image. This overlay will not only allo ...

using padding to vertically center an input element

Hey everyone, a few days ago I faced a challenge with centering an input element and someone suggested a solution which worked perfectly. You can view the solution on Stack Overflow. The CSS for centering the input element is quite simple and standard: h ...

Navigating the ins and outs of Bootstrap 4 grids

I am trying to create a layout with two columns, each using the class col-md-6. In both columns, there are six images. However, I want to have only one image displayed in the left column if the right column is empty. How can I achieve this? The current se ...

When the page loads, the HTML side menu will automatically scroll to the active item in a vertical

My website features a vertical side menu with approximately 20 items. The issue I am facing is that when a user clicks on an item, the destination loads but the side menu must be manually scrolled to find the active item if it's located at the bottom ...

Insert a fresh item into the existing unordered list

Every time I enter something in the prompt, it shows up as "undefined". What I actually want is for whatever I type into the prompt to be added as a new list item. For instance, if I type "Food" in the prompt, I expect to see "Food" appear on the second li ...

Incorporating Tabs with HTML, CSS, and jQuery

After searching for a way to enable tab selection on click within my HTML fragment representing a tabbed interface, I was unable to find a solution. I resorted to manually programming it using JavaScript, which did work, but I feel there must be a more p ...