Tips for aligning an image of varying dimensions in the center

Although I've done some research and attempted to splice together code from various sources, including here, here, and other places, I'm still struggling with centering an image on a webpage.

The page in question features three layered .png images. The top two are dynamic in size, covering 100% of the page. The topmost image has a transparent hole in the center, revealing the middle one when JavaScript is activated. Both the top images and the JavaScript functionality work as intended.

My goal is to have the bottom image perfectly centered both vertically and horizontally, maintaining proportionality relative to the page size. Despite my efforts, I have not been able to achieve both vertical and horizontal centering simultaneously. Using the bottom image as a background isn't ideal due to color discrepancies or tiling issues.

Below is a snippet of the relevant code:

HTML:

<body>
    <div>
        <p0 id="face"><img src="face2.png"; class='face'></p0>
    </div>
    <p1 id="circuit"><img src='circuit.png' width='100%' height='100%' /></p1>
    <p2 id="hole"><img src="hole.png" width='100%' height='100%' /></p2>
</body>

CSS:

p0 {
    width: 50%; /* Unable to set height to 50% due to differing sizes */
    center
    z-index: 1;
    position: absolute;
}

p1{
    z-index: 2;
    width: 100%;
    padding: 0;
    margin: 0;
    height: 100%;
    position: absolute;
}

p2{
    z-index: 3;
    width: 100%;
    padding: 0;
    margin: 0;
    height: 100%;
    position: absolute;
}

Answer №1

To center everything you desire, place it within a div with the following properties:

   .div{
     width: 100%;
     position: relative; // or absolute
     text-align: center;
     text-align: -moz-center;
     text-align: -webkit-center;
     }

Check out this link for morejsfiddle

Below is the complete CSS code to stack two images of different sizes in the exact center on top of each other as requested,

.parent{
 width: 100%;
 position:  absolute;
 text-aligh: -align: center;
 text-align: -moz-center;
 text-align: -webkit-center;

 }

.center1{

height:120px;
width: 275px;    
background-image: url("http://www.google.com/images/srpr/logo3w.png");
}

.center2{       
height: 100px;
width: 200px;
 background-image: url("http://www.google.com/logos/2012/turing-doodle-static.jpg");
}

Here is the HTML structure:

     <div class="parent" style="z-index: 1;">
         <div class="center1"></div> 
     </div>
     <div class="parent" style="z-index: 10;">
         <div class="center2"></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

Displays a white screen once the code is executed

I am attempting to retrieve data from a database, and it appears that I am fetching the information correctly. However, when I run the code, I am presented with a blank page. Since I am new to PHP and MySQL, I would appreciate some guidance on this issue ...

Cross out an item in a list made with Material-UI and React.js

I am attempting to add a strike-through effect to a list item after a user clicks on it. To achieve this, I have developed a function that changes the style: const completed = () =>{ return document.getElementById("demo").style.textDecoration=&ap ...

Prevent mobile view from activating when zoomed in on the screen

I've built a webpage with a responsive design that adjusts to mobile view on mobile devices or when the screen size is reduced using developer tools. While this functionality works correctly, I have noticed that the design also switches to mobile vie ...

Is combining Nuxt 3 with WP REST API, Pinia, and local storage an effective approach for user authentication?

My current project involves utilizing NUXT 3 for the frontend and integrating Wordpress as the backend. The data is transmitted from the backend to the frontend through the REST API. The application functions as a content management system (CMS), with all ...

Utilizing Capture Groups in CSS File for Regex Search and Replace

When it comes to extracting critical styles from a CSS file wrapped in a @critical rule, I run into some issues: @critical { .foo { ... } @media bar { ... } } The command I'm using for extraction involves sed search ...

What is the best way to remove column and row gaps in a Bootstrap grid system?

Despite adjusting padding with the image, div section, and grid gap to 0px in the grid layout, the image still fails to cover the entire cell. I am seeking a solution to eliminate these unwanted paddings that are highlighted in blue. The Bootstrap 5 .g-0 ...

What causes the "Error: method not allowed" message to appear when attempting to send a "DELETE" request from a Next Js component? (The POST method is

This is my first time on this platform, and I'm currently following a tutorial from Javascript Mastery to create a clone of a thread application. After watching the entire video and building the basic functionality based on it, I decided to enhance th ...

What could be causing me to receive a 401 error when making a Rails jQuery POST request?

I am currently utilizing devise within a rails application. I have successfully logged into my rails server (devise) using the following curl command: curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST h ...

Embracing the beauty of incorporating nested quotations

I've been experimenting with jquery's append functions lately. I'm adding a substantial amount of html, specifically a button with an onclick event function. It might sound complicated, but due to some technical restrictions, this is my onl ...

Optimizing Bootstrap for Tablet Browsing in Internet Explorer

Hey there, I have a question regarding Internet Explorer and tablets. As I revamp this website and integrate Bootstrap into it, my focus is currently on the small-sized IE browser for tablets. My dilemma arises from not knowing if there are any tablets th ...

Pressing the Enter key on a textarea does not trigger data fetching

After clicking the button, all values are displaying correctly. However, when I try to use the ENTER key on the textarea to send the data, the data is not showing up. I have attempted the code below but it only shows an empty div. You can find the jsfiddle ...

Effortlessly move and rearrange various rows within an HTML table by utilizing JQuery alongside the <thead> elements

My JsFiddle has two tables and I want to switch rows <tr> between them using Jquery. However, due to the presence of the <thead> tag, it is not functioning properly. I suspect that the issue lies with the items in the selector, but I am unsure ...

Button switch in Bootstrap 4

Currently in the process of designing a basic vehicle inspection form, I have an idea to utilize checkboxes as buttons for capturing true/false values. I've discovered that this task can be easily accomplished using Bootstrap 4 as indicated by their ...

Fill in a text box with a chosen value from a linked drop-down menu

My database contains two tables: 1 - tbl_category 2 - tbl_shelf_place I am working towards displaying the shelf_code in a textbox based on the selected category_name from a drop-down (book_category) with a value of category_id, instead of using another ...

What is the process for showing and hiding text when a div is clicked?

Hey, I'm completely new to web development and decided to practice some of the concepts I've been learning about. I created a simple program that toggles between day and night. Clicking on the sun reveals the moon, and clicking on the moon revea ...

Having trouble with making the function to remove duplicate items from a JavaScript array?

Before anything else, I want to acknowledge that there are numerous answers on SO regarding this issue. However, I am encountering some difficulties with them, which is why I am posting a new question on the topic. Here is my Array of Objects: 0: {id: &ap ...

Exploring Next.js: Advanced capabilities of shallow routing in combination with dynamic routes

Many people seem to be confused about the behavior of shallow routing with dynamic routes in Next.js. When attempting shallow routing, the page is refreshed and the shallow option is ignored. Let's consider a scenario where we start on the following ...

How to build a login page with a static header and footer using Angular2

For my latest project, I am currently in the process of developing an application using Angular2 and eclipse Neon. Utilizing angular-cli for this app, I am now focused on creating the login page. Within the app.component.html file, you will find the follow ...

Set the android camera resolution to a lower setting automatically when a user attempts to upload a file from their browser

In my current application, I have implemented a feature that allows users to upload files. When the user clicks on the input field, they are presented with options to either select a video from their gallery or open the camera to record a new video and u ...

Utilizing the current state within a React callback function closure: A guide to maximising efficiency

I'm currently working on a web page that features a dynamic list of form inputs. Users have the ability to add or remove input fields using designated buttons. To manage this functionality, I've created a parent object called <Ingredients /> ...