How can I center an image within another image in a two-column layout?

Hey there, I'm working on a 2-column layout where I need to position an image in the center of another one in the second column. However, my attempts at styling seem to be failing as the image keeps showing up in the top left corner of my page...

Here's the HTML snippet:

<div class="Parents">
<div class="child1">
  <img class="IMAGEA1" src="assets/images/imageborder.png">
  <img src="assets/images/operation.png">
</div>
<div class="child2">
  <h4>Operation</h4>
  <p>We provide customised and well-designed Standard Operating Procedures for every farm, making it easy for anyone to run smoothly and effectively.</p>
</div>

For the CSS columns:

.Parents {
  display: flex;
  flex-direction: row;
  column-gap: 50px;
  width: 756px;
  height: 300px;
  margin: 0 auto;
}

.child1 {
  width: 50%;
  height: 200px;
  text-align: center;
  color: black;
  margin: 0 auto;
}

.child2 {
  width: 50%;
  height: 200px;
  margin: 0 auto;
}

.Parents p {
  text-align: justify;
  margin-top: 40px;
}

And for the image section in CSS:

.IMAGEA1{
  z-index: 1;
}
.Parents img{
  position: absolute;
  top: 25px;
  left: 25px;
  z-index:3;
}

Note: I've been experimenting with using z-index to solve this issue, but so far the image doesn't align as expected within its parent div.

Answer №1

Enclose the images in a separate div and utilize position: absolute as well as transform: translate to center them on top of each other.

.Parents {
  display: flex;
  flex-direction: row;
  column-gap: 50px;
  width: 756px;
  height: 300px;
  margin: 0 auto;
}

.child1 {
  width: 50%;
  height: 200px;
  text-align: center;
  color: black;
  margin: 0 auto;
}

.child2 {
  width: 50%;
  height: 200px;
  margin: 0 auto;
}

.Parents p {
  text-align: justify;
  margin-top: 40px;
}

.Parents .centered-images{
  width: calc(100% - 25px);
  height: calc(100% - 25px);
  position: absolute;
  top: 25px;
  left: 25px;
}

.Parents img{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.Parents img.IMAGEA1{
  z-index: 1;
}

.Parents img.IMAGEA2{
  z-index:3;
}
<div class="Parents">
<div class="child1">
  <div class="centered-images">
    <img class="IMAGEA1" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Chamomile%40original_size.jpg/367px-Chamomile%40original_size.jpg">
    <img class="IMAGEA2" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Gentiana-clusii-ClusiusEnzian.jpg/254px-Gentiana-clusii-ClusiusEnzian.jpg">
  </div>
</div>
<div class="child2">
  <h4>Operation</h4>
  <p>We offer personalized and intricately designed Standard Operating Procedure for every HAVVA farm, making it easy to operate seamlessly and effectively by anyone.</p>
</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

Issue encountered while integrating crispy forms with Django and bootstrap

After developing a sign-in page for my blog using bootstrap and django, I decided to incorporate crispy forms. However, upon attempting to access the page, an error message popped up: TemplateDoesNotExist at /register/. Despite ensuring all redirects and U ...

Creating distance between two text boxes in bootstrap is easy with the use of margins. By

I am looking to create a small white space between two text boxes. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="row col-lg-12 col-sm-12"> <div class ...

Implement horizontal scrolling feature to code container

One cool feature on my blog is the codebox where I can insert snippets of HTML, CSS, and Javascript codes. Here's an example of how my CSS code looks: .post blockquote { background-image: url("https://dl.dropbox.com/u/76401970/All%20Blogger%20Tr ...

Inspecting element value on a website

On a marketplace website, I extracted the attribute (capacity_gold) from a table. Since the values are dynamic and constantly changing, I aim to develop a basic script that will notify me when the attribute value exceeds 100. The current value of the attr ...

I'm having trouble with using setInterval() or the increment operator (i+=) while drawing on a canvas in Javascript. Can anyone help me out? I'm new

I am looking to create an animation where a square's stroke is drawn starting from the clicked position on a canvas. Currently, I am facing an issue where the values of variables p & q are not updating as expected when drawing the square at the click ...

The radio buttons are stuck and not changing their selection

Having a group of radio buttons with the same name, when one is checked, it automatically selects another one in the group. Here is my current code: <input name="a" type="radio"> <input name="a "type="radio" checked> JS $("input[type='r ...

Can a PHP function be invoked in an URL using ajax?

I have created a PHP code with a single function that includes an output statement and I want to invoke that function as a URL within an AJAX script. I attempted to do this. <html> <head> <title>PHP Function Example</tit ...

Declare a jQuery variable containing HTML elements

Greetings! I am facing an issue while attempting to create a jQuery variable that stores a table meant for displaying in different sections of a website. Unfortunately, I am encountering an error and struggling to comprehend the reason behind it. Below is ...

Dynamic HTML5 elements in constant motion

I'm interested in creating an HTML5 canvas that features bouncing circle and square elements that interact with each other, potentially spinning upon collision. The best example I've come across so far is located at this link. var canvas = docu ...

Uh oh! AngularJS just threw an error: Uncaught TypeError - angular.service is not a function. It happened in taskService.js

I've run into some issues with AngularJS, even though I have imported all the necessary libraries. Specifically, I am encountering the following errors: Uncaught TypeError: angular.service is not a function at taskService.js:2 taskFactory. ...

"Enhance text formatting by making it stand out when hovered over

I am encountering an issue where the hover action for a specific letter begins before the mouse actually touches the text. In this case, I want the letter 'P' in 'Paul' to turn white when hovered over. The image illustrates the distanc ...

Seeking assistance with changing the pages

Looking for assistance with troubleshooting my function. All variables are accounted for. var swith = document.getElementById("sub"); var one = document.getElementById("rule"); var two = document.getElementById("cool"); var three = document.getElementByI ...

Upon clicking on a different div, a div will elegantly slide down from above the footer

I'm struggling with sliding a div from the bottom of the page. Here is my JSFIDDLE Currently, when I click on the blue box (arrow-hover), the div slides up (box-main). However, to hide the div (box-main) again, I have to click inside the box-main d ...

Guide on utilizing font-awesome icons stored in node-modules

After successfully installing font-awesome 4.0.3 icons through the npm install command, How can I incorporate them into an HTML file from the node-modules directory? If edits need to be made to the less file, do they have to be done within the node-modul ...

Manipulating the size of one div automatically adjusts the size of the other, as they are

I have encountered an issue while trying to manage two resizable divs. I am looking for a solution where changing the size of one div will automatically adjust the size of the other div along the grid, and vice versa. Currently, I am only able to achieve ...

Erase embedded frame from a Google Sheets document

Is there a way for me to customize the frame and replace it with my own design? Click here to view the Google Sheet embedded frame ...

Tips for managing a series of images in a line within a bootstrap form

Can someone help me troubleshoot my bootstrap row handling? Here is my code: <div class="form-group col-md-6"> <?php if($saved_image != ''): ?> <div style="width:100px; height:100px"> <img class="i ...

What is the best way for me to insert a paragraph between two lists?

Is it possible to insert a paragraph in between lists even though it's not allowed? Here is an example of the code: <ol> <li>One</li> <p>Paragraph 1</p> <li>Two</li> <p>Paragraph 2</p ...

The data insertion query in MYSQL seems to be malfunctioning

I am facing an issue with inserting data from a form into a database table named "sumation". Despite using PhpStorm IDE, I am getting errors indicating that no data sources are configured to run the SQL and the SQL dialect is not set up. Can anyone help ...

Is there a way to have my grid not apply grid-gap if a specific area is vacant?

I am working with a grid layout that has defined grid areas and a grid-gap, but some areas may not always have content. How can I prevent a double gap between items when an area is empty without changing the grid-template? For example: .grid { displa ...