Organize images with overlays to the center of the page

I need help centering a group of pictures (a 3x3 table) on my webpage. I was able to center them before adding image overlays, but now the images are showing up in the top left corner of the page. How can I group and center them, and how do I specify the image location so that each picture has a different overlay text?

Here is the CSS code:

.container {
   position: relative;
   width: 100px;
   height: 100px;
}

.image {
   display: block;
   width: 100px;
   height: 100px;
 }

 .overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;

 }

 .container:hover .overlay {
      opacity: 1;
 }

.text {
   color: red;
   font-size: 20px;
   font-weight: bold;
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
 }

Here is the HTML code:

<div style="text-align:center">
 <div class="container">
 <img src="wheel1.jpg" class="image">
    <div class="overlay">
        <div class="text">Hello World</div>
    </div>
</div>
<div class="container">
<img src="wheels2.jpg" class="image">
    <div class="overlay">
        <div class="text">Hello World</div>
    </div>
</div>
<div class="container">
<img src="wheel3.jpg" class="image"">
    <div class="overlay">
        <div class="text">Hello World</div>
    </div>
</div>

`

Answer №1

If you want to center it, consider using flexbox. Modify your main div

<div style="text-align-center;">
     ......
</div>

by changing it to

<div style="display: flex; flex-direction: column;align-items: center;">
    .....
</div>

This adjustment should do the trick.

.wrapper{
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.container {
   position: relative;
   width: 100px;
   height: 100px;
}

.image {
   display: block;
   width: 100px;
   height: 100px;
   border: 1px solid red;
 }

 .overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;

 }

 .container:hover .overlay {
      opacity: 1;
 }

.text {
   color: red;
   font-size: 20px;
   font-weight: bold;
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
 }
<div class="wrapper">
   <div class="container">
   <div class="image"></div>
      <div class="overlay">
          <div class="text">Hello World</div>
      </div>
    </div>
  <div class="container">
  <div class="image"></div>
      <div class="overlay">
          <div class="text">Hello World</div>
      </div>
  </div>
  <div class="container">
  <div class="image"></div>
      <div class="overlay">
          <div class="text">Hello World</div>
      </div>
  </div>
  </div>

Take a look at this fiddle for reference.

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

Hide, show, toggle, or empty elements using jQuery

I am having trouble getting this jQuery code to function properly. Within a div, there is a paragraph that I want to hide when a specific button is clicked and show when another button is clicked. Check out the code here $("#details").on("c ...

Maintain dropdown menu visibility while navigating

I'm having an issue with my dropdown menu. It keeps sliding up when I try to navigate under the sub menu. I've spent all day trying to fix it, testing out various examples from the internet but so far, no luck. Any help would be greatly apprecia ...

Tips for customizing the default styles of PrimeNG's <p-accordion> tag

Hello, I have encountered an issue with my html code snippet. I am attempting to create a tab with a label-header named "Users", but the accordion tag consistently displays it as underlined. <div class="ui-g"> <p-accordion id="tabsHeader"> ...

Can a faulty image be cached?

Is there a way to ensure that the browser caches invalid or broken images so that when they are re-fetched, the loading time is immediate? I am particularly interested in two scenarios: 1) The first scenario involves trying to load an image from a URL co ...

Activate the datepicker in Angular by clicking on the input field

This is my html file: <mat-form-field> <input matInput [matDatepicker]="picker" placeholder="Choose a date"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-date ...

Combine going to an anchor, performing an AJAX request, and opening a jQuery-UI accordion tab all within a

My goal is to have the user click on the hyperlink below, which will (1) anchor to #suggestions, (2) navigate to the url suggestions.php?appid=70&commentid=25961, and (3) open the jquery-ui accordion #suggestions. However, I am facing an issue where c ...

From JSON object to HTML table: converting structured data into a user

I'm currently facing a challenge when trying to iterate through JSON data retrieved from an API and display it in an HTML table. After parsing the original JSON, I accessed its elements as follows: JSON (data retrieved from API): {"PrekesID" ...

Activate the textbox without utilizing `.focus()` method

I am encountering an issue with a small iframe on my page. The content within the iframe is larger than the window itself, requiring users to scroll around to view it in its entirety. Within this iframe, there is a button that, when clicked, triggers an an ...

Ways to modify CSS using JavaScript

Can anyone assist me with a custom CSS code that I found? It looks like this: header.type-2:not(.fixed-header) nav>ul>li>a{ color:#000; } I've been trying to change the color to #fff using JavaScript, but haven't had any success yet. ...

What is the most effective way to prevent JavaScript from running during page load or when the document is ready

I am facing a challenge with my asp.net page. Whenever a textbox is left empty, I want to hide the entire page from the code behind. The issue arises because there is javascript and jquery code that runs on document ready, accessing data from certain page ...

Unable to hide the mobile menu button

I am currently working on a fun website project . I am facing an issue with the mobile menu button not disappearing using display:none in Safari on my iPhone when in landscape mode, even though it works fine in Chrome. My goal is to make the #menu-button ...

The #each helper in Handlebars is used to iterate over an array

I have a function that generates an array as output. I am looking for a way to iterate over this array using the each method. Can anyone provide guidance on how to achieve this? Consider if the handlebars helper produces the following array: details: [{ ...

What is the best way to incorporate one Div within another Div using JavaScript?

I have a single main div with an id, and I am looking to insert 4 additional child divs inside it. Each of these child divs will then contain 5 more divs each. The code snippet that I currently have is as follows: $( document ).ready(function() { for( ...

The HTML and body elements do not possess a width of 100% within the document

When viewing my portfolio site on smaller screens such as 425px and below, there is a white gap on the right side. This issue arises because the HTML and body elements do not inherit full width by default. Do you know why this happens and how I can resol ...

What could be the reason why the toggleClass function is not being run

I've been running into a little issue with using the .toggleClass function in my code. It seems to work inconsistently, and despite reading various posts on the topic, I haven't found a solution that works for me. Could you provide some assistan ...

Is the textarea functioning properly with the inclusion of mysterious character variables?

As per the documentation When using an unknown named character reference like &this, it results in an ambiguous ampersand error. This situation is confusing <textarea> &this </textarea> What am I misunderstanding here? Any re ...

Convert the div into a string, including only the visible elements

Within the div #allContent, there are multiple nested divs. My goal is to extract the entire content of #allContent as a string, while excluding any invisible divs contained within it. I believe that this task can be achieved using a combination of filter ...

Make sure to load the <img> html tag before implementing js masonry for optimal

I am facing an issue with jQuery Masonry as it arranges my 'bricks' immediately without waiting for images to load in. Additionally, I need the images to be responsive so I cannot define their height and width like this: <img src="asas" heigh ...

Which method is better for HTML5/JavaScript GeoLocation: Passing data to a callback function or suspending an async call using promises?

Trying to figure out how to utilize HTML5/Javascript Geolocations effectively for passing location data to an ajax call and storing it in a database. The main challenges I'm facing are the asynchronous nature of the Geolocation call and issues with va ...

Transfer information from the client to the server using AJAX and PHP by

When attempting to post a JavaScript variable called posY to a PHP file, an error occurred with the message: Notice: Undefined index: data in C:\xampp\htdocs\Heads_in_the_clouds\submitposY.php The posY variable is defined in the JavaSc ...