Tips for positioning text in the center of an image

I'm struggling to center a text block both horizontally and vertically above an image. I have searched through various resources, but none of the solutions seem to work for me.

Below is the code snippet in question:

<body>
   <div id="top-of-page">
     <img src="images/etudes.png">
     <div id="text-block">
       <h1>Title</h1>
     </div>
   </div>

#haut-de-page {
  position: relative;
  margin: 10px;
}

#top-of-page img {
  width: 100%;
}

#text-block {
  position: absolute;
  top: 5px;
  margin: 0 auto;
  background-color: rgba(0, 0, 0, .5);
  color: white;
  padding-top: 20px;
  padding-left: 20px;
}

Despite setting the div "text-block" position as absolute, the text remains below the image. Any suggestions on how to fix this?

Would appreciate your thoughts!

Answer №1

Give this a shot:

.picture {
  position: relative;
  width: max-content;
  height: max-content;
}

#description-block {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 100;
  background-color: rgba(0, 0, 0, .5);
  color: white;
}
<div id="top-of-page">
  <div class="picture">
    <img src="https://www.mooreseal.com/wp-content/uploads/2013/11/dummy-image-square.jpg">
    <div id="description-block">
      <h1>Title</h1>
    </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

button click event blocked due to unforeseen circumstances

Recently, I've been attempting to change the CSS properties of a div by triggering a click event. However, no matter what I do, it doesn't seem to be working and it's starting to frustrate me. Can anyone shed some light on why this might be ...

Is it possible to enhance the appearance of the select2 options in any way?

I am currently using a select2 plugin to handle select boxes, where the options are generated by concatenating two values together. However, I am looking for a way to style one of the values in the select2 box with a different color. Are there any options ...

How can I activate a jQuery function only when it is within the viewport?

I have installed a jQuery Counter on my website, but I am experiencing an issue where the counter starts or finishes before reaching the section where the HTML code is embedded. Any assistance with this problem would be greatly appreciated! <script& ...

Issues with creating modal images using only JavaScript

I am facing an issue with modal popup images. I have a collection of images and attempted to implement this code (with some modifications): https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal_img However, it seems to only work for the initi ...

Is it possible to shift the "ul" block of the navigation bar to the right without compromising the responsiveness toggle button functionality?

I'm currently using bootstrap NavBar and encountering an issue where moving the ul block to the right is disabling the toggle button responsiveness. <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" c ...

Obtaining the value and other attributes of a select option in Vue.js 2

I have implemented vuejs in my project and encountered a situation where I need to work with a select element containing multiple options. Here is an example of the code: <select v-model='line.unit' name='unit[]' required @change=& ...

Unable to render HTML in Simple Product Name on Magento platform

Is there a way to include HTML in my basic product titles, such as: <strong>A.</strong> Product Title Name However, the frontend displays these HTML tags. Is there a method to enable parsing of all HTML tags? ...

Using JavaScript to cheat on a typing test by automating the typing of letters

I am currently working on a solution to simulate key presses for a typing test. My idea is to extract individual letters from the text, store them in an array, and then simulate pressing each key with a delay between each press. Below is the HTML layout o ...

Adjust the class of the iframe element when clicked

I am attempting to change the class of an iframe (soundcloud embedded track) when it is clicked, in order to apply different properties. However, my code doesn't seem to be working as expected. Here is what I have: Here is a snippet from my index.htm ...

Is it achievable to remove the wrapper from elements using jQuery?

Is it possible to modify this markup without altering core files in the CMS? I have a particular structure that I would like to adjust using jQuery for a temporary solution. <div class="homepage-boxes"> <div class="row-fluid"> < ...

Is verifying email and password with jquery possible?

I am currently working on a jQuery form validation project: While the password and username validation are working fine, I am facing issues with email and password confirmation validations. Surprisingly, I have used the same technique for both. If you wa ...

Elevate the Appearance of Material UI Elements with custom CSS Sty

I am currently facing an issue while trying to customize the styling of a Material UI component using CSS. Here is the code snippet: <IconButton className="my-class"> <Close /> </IconButton> CSS: .my-class { float: right ...

Ensure that blocks with ribbon attachments are perceived as equal in size

I have been utilizing the bootstrap grid system to generate different-sized "blocks" on my webpage. Some of these blocks require a ribbon (such as "new" products). The challenge lies in how I create these ribbons, which involves wrapping the block's c ...

Exploring the utility of the load_file function in the simple HTML DOM method

After writing this code and it was functioning properly. <?php include ('require/simplehtmldom_1_5/simple_html_dom.php'); echo $html=file_get_html('http://www.site.com'); ?> Now I want to make it work using the object method. He ...

What is the best way to create a smooth transition for the box-shadow of a div when hovering over it, causing it to fade in and out seamlessly

I'm exploring animations and experimenting with a div containing three box-shadows. My goal is to create a hover effect where the box-shadows fade in one after the other, starting with the closest one to the div. I also want them to fade out in revers ...

Ensure that the div is styled with a white background and positioned next to another div with a right margin

I have a white-colored div that needs to be positioned next to another element on a webpage. Additionally, I need a paragraph placed on top of this div for information purposes. Please refer to the following link to see what I am trying to achieve: https:/ ...

Creating a dynamic multi-select feature in AngularJS with ng-repeat

I'm relatively new to AngularJS and JavaScript, but I've managed to create a functional multi-select list. The ng-model linked to the dropdown is part of a "user" DTO object, specifically a property that stores an array of "groups" the user can j ...

CSS: Eliminate unnecessary space at the top by implementing a margin and padding reset

I am puzzled by the presence of approximately 10px of whitespace at the top of my HTML file. Despite setting margin and padding to 0 in the "body" section of my CSS, the whitespace persists. Any suggestions? Thank you! Update: I discovered that removing t ...

Discrepancy in Syntax Highlighting in HAML Code

Hopefully this is an easy fix, as I am struggling with highlighting some HAML code using the prism library. %pre %code.language-haml &#37;header.post-header &#37;h1= data.title &#37;time{ datetime: data.date }= pretty_date(data.d ...

What is the process of modifying a list using a custom tag in JSP?

I encountered an issue with a custom tag in JSP. When the page finishes loading, the HTML content list generated by the div custom tag appears. I then aim to replace this list with new data using AJAX. First, I clear out the old list and create an HTML str ...