Tips for aligning an image on top of another image

Here is the code I am working with (also pasted below). My goal is to create a layout with two columns, where the first column features two images and the second column displays some text.

In the first column, I specifically want the first image to have a width of 70% and the second image to be positioned absolutely on top of it. The desired final result can be seen here.

What I'm aiming for is to have the second image partially overlapping the first image on all screens larger than 768px.

I've managed to achieve this partial overlap, but it's not responsive - the layout collapses when screen dimensions change.

Despite my efforts, I haven't been able to achieve the desired result.

.checkoutWrapper {
  display: grid;
  grid-template-columns: 50% 50%;
}

.coverIMGLast {
  width: 70%;
  height: 100vh;
}

/* This .phone class should dynamically position the phone image over the first image */
.phone {
  position: absolute;
  height: 90%;
  top: 5%;
  bottom: 5%;
  margin-left: 18%;
}

.CheckoutProcess {
  padding: 5.8rem 6.4rem;
}

.someContent {
  border: 1px solid red;
}
<div class="checkoutWrapper">
  <img src="https://via.placeholder.com/300" class="coverIMGLast" />
  <img src="https://via.placeholder.com/300/ff0000" class="phone" />

  <div class="CheckoutProcess">
    <Content />
  </div>
</div>

Answer №1

To achieve the desired outcome, you can utilize the CSS properties transform and aspect-ratio:

.checkoutWrapper {
  display: grid;
  grid-template-columns: 30% 70%;
  height: 100vh;
}

.left {
  background-color: #baafa0;
  position: relative;
  
  background-image: url('https://picsum.photos/id/1028/200/300');
  background-size: cover;
}

.phone {
  height: 60%;
  aspect-ratio: 1/2;
  
  position: absolute;
  top: 50%;
  left: 100%;
  transform: translate(-50%, -50%);
  
  border-radius: 15%;
}

.right {
  padding-left: calc(15vh + 1rem);
}
<div class="checkoutWrapper">
  <div class="left">
    <img class="phone" src="https://via.placeholder.com/300/444" />
  </div>
  <div class="right CheckoutProcess">
    <Content>
      <h1>Check Out</h1>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda consectetur ducimus officiis dicta vero distinctio odio sit explicabo illum dolorem quasi facilis repellendus sapiente fuga corporis, iure dolore ad, quis quisquam? Dolorum nostrum, veritatis
        molestiae culpa maiores exercitationem cumque qui harum, unde est ratione doloremque necessitatibus et numquam itaque neque!
      </p>
    </Content>
  </div>
</div>

Answer №2

I made some adjustments to your code

Everything you wrote is accurate.

You simply overlooked adding margin-left: 25%. This will ensure that no matter the screen size, it will always be centered on the first one.

Answer №3

take a look at this code snippet I crafted specifically for you, hoping it will be of assistance...

view image description here

    body {
        padding: 0;
        margin: 0;
    }

    .parent {
        display: flex;
        position: relative;
        align-items: center;
    }

    .left {
        width: 37%;
        background-color: blue;
        height: 100vh;
    }

    .right {
        width: 63%;
        background-color: red;
        height: 100vh;

        display: flex;
        justify-content: center;
        align-items: center;
    }

    .image-container {
        position: absolute;
        height: 60%;
        width: 100%;
        display: flex;
        align-items: center;
    }

    .image {
        height: 500px;
        width: 250px;
        background-color: grey;
        margin-left: 30%;
        border-radius: 20px;
    }
<body>


    <div class='parent'>
        <div class='left'></div>
        <div class='right'>
            <div>
                <h3>This is a title</h3>
                <p>this is a paragraph</p>
                <p>this is a paragraph</p>
                <p>this is a paragraph</p>
            </div>
        </div>

        <div class="image-container">
            <div class="image"></div>
        </div>
    </div>


</body>

Answer №4

To create a unique visual effect, consider setting the first image as the background image and adjusting the size using CSS. Then insert the second image within the <div> element.

#bg-img{
  background-image:url('https://images.pexels.com/photos/36029/aroni-arsa-children-little.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500');
  position: absolute;
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-size: 50% 100%;
  background-color: #999;
}
#img-inside{
  position:absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width:15%;
}
<div id="bg-img">
  <img id="img-inside" src="https://images.pexels.com/photos/36029/aroni-arsa-children-little.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="second-img" />
</div>

This snippet demonstrates how to style an interactive layout with two images displayed creatively.

Answer №5

* {
  margin: 0;
}

.checkoutWrapper {
  display: grid;
  grid-template-columns: 35% 25% 40%; /* creating three columns to accommodate both images */
}

.coverIMGLast {
  width: calc(100% - 195px); **/* giving the illusion of real overlapping */**
  height: 100vh; /* please maintain this line */
  grid-column: 1 / 3; /* overlapping effect */
  grid-row: 1;
  object-fit: cover;
}
/* dynamically positioning the .phone class partially on the first image */
.phone {
  height: 90vh;
  width: 380px;
  grid-row: 1; /* placing the images on one row */
  grid-column: 1/3; /* creating overlap */
  justify-self: end;
  transform: translateY(5vh);
}

.CheckoutProcess {
  padding: 5.8rem 0.5rem 6.4rem; /* Adding standard padding for desired effect */
}

.someContent {
  border: 1px solid red;
}

@media screen and (max-width: 768px) {
  .checkoutWrapper {
    display: grid;
    grid-template-columns: 100%;
  }

  img {
    display: none;
  }

  .CheckoutProcess {
    padding: 0;
  }
}
<div class="checkoutWrapper">
  <img src="https://via.placeholder.com/300" class="coverIMGLast" />
  <img src="https://via.placeholder.com/300/ff0000" class="phone" />
  <div class="CheckoutProcess">
    <div class="someContent">
      <h1>Some heading</h1>
      <p> 
        Some text to go with the paragraph 
      </p>
    </div>
  </div>
</div>

From the adjustments made in the code and previewing your edited version on the provided link, you successfully created an illusion of overlapping by manipulating grid-column, utilizing template columns to simulate a book-like appearance.

Utilizing:

  1. grid-template-columns: 35% 25% 40%;
    /* creating three columns to fit both images */
  2. grid-row: 1; /* positioning the images along one row */
  3. grid-column: 1/3; /* generating overlap */
  4. justify-self: end; /* positioning image on right side of grid */

Answer №6

Below is the code structure you desire. Adjust the width, height, etc to customize it as needed.

.checkoutWrapper {
  display: grid;
  grid-template-columns: 50% 50%;
  align-items:center;
}

.checkoutImgs {
  position: relative;
}

img{
  object-fit:cover;
  display:block;
  height:100%;
}

.coverIMGLast {
  width: 70%;
  height: 100vh;
  
}

  
.phone {
  position: absolute;
  height: 80%;
  width:40%;
  top: 10%;
  right: 0;

}

.CheckoutProcess {
  padding: 1rem 2.4rem;
  display:grid;
  place-items:center
}
<div class="checkoutWrapper">
  <div class="checkoutImgs">
    <img
      src="https://images.unsplash.com/photo-1659592516254-c00ca7af3db4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw0fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60"
      class="coverIMGLast"
    />
    <img
      src="https://images.unsplash.com/photo-1659808749760-e5baf9c16f10?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwxMnx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60"
      class="phone"
    />
  </div>

  <div class="CheckoutProcess">
    Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page
    avant impression. Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les
    années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour
    réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles,
    mais s'est aussi adapté à la bureautique informatique
  </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

Removing or hiding elements using jQuery

My HTML code includes a select tag, and when the value changes, I want to retrieve data from the database based on this new value. The data is then displayed in a new div. This new div contains a close sign that should hide the div when clicked by the user ...

Increase the value by one of the number enclosed in the <h3> element

I have a variable number of <h3> tags with the same class .love_nummer <h3 class="love_nummer">1</h3> Each of these tags contains a number, and alongside each .love_nummer tag is another tag with the class .give_love <h3 class="give ...

How to retrieve the row index from a table using JavaScript

This question has cropped up numerous times, and despite trying various solutions suggested before, none of them seem to be effective in my case. Within a modal, I have a table where users can make changes that are then used to update the database. The ta ...

Arrangement of label and input field on my webpage

I am looking to vertically align all the fields one below another. Currently, they are randomly aligned with Bootstrap CSS. The desired layout should look like this: Label1: Textbox1 Label2: Textbox2 Here is the code snippet: Which class should I use to ...

How to apply gradient colors to borders using CSS

Is it possible to create a simple border bottom color with a gradient effect? div{ border-bottom:10px solid linear-gradient(#FF4000, transparent); height:20px; width:auto; background:#ccc; } <div></div> ...

Tips for Embedding React into a Specific ID using Hooks on an HTML Document

I am currently diving into the world of React hooks. While I understand class components, Hooks seem to be missing a specific render section. When adding React to a webpage (without using CREATE REACT APP), how do I specify where my hook should run on the ...

What is the method for incorporating opacity into the background color CSS attribute using a Fluent UI color?

Currently, my challenge involves applying opacity to a background color sourced from the fluent UI library, which utilizes Design Tokens. Typically, I would add opacity to a background color like this: background-color: "rgba(255, 255, 255, 0.5)" However ...

Is it possible to incorporate a similar box on a webpage?

I am trying to find a way to add a similar box to a webpage like the one shown in this image. The design I am referring to is from Reddit's theme, but I would like to know how to create a box with a different color and then have another box next to i ...

View content from an external file within an iframe

My goal is to showcase a text file within an iframe that updates automatically every second. To achieve this, I have created a simple function: function refresh() { $("#derek").attr('src', $("#derek").attr('src')); }; The automati ...

Ensure that a specific value is maintained for a property of an element throughout its animation

There are two distinct types of components that I am working with, which I will refer to as .a and .b. It is possible that these components have been assigned certain CSS animations. I do not have the ability to control these keyframes, whether they are a ...

Is the hover effect malfunctioning, even though the correct style has been applied?

I'm currently working on a simple design, but I've encountered an issue. When I hover over the number blocks (1, 2, etc.), my hover effect doesn't seem to work. My intention is to hover over the list item and change the entire digit text fro ...

How can you inform TypeScript about a file that will be included using a script tag?

I am currently utilizing TypeScript for my front-end JavaScript development, and I have a situation where I am loading two scripts from my index.html file as shown below: <script src="replacements.js"></script> <script src="socket.js">&l ...

Incorporate a hanging indent within the text enclosed by the <li> element

(revised multiple times to enhance clarity) Disclaimer: I did not create the HTML code. Below is the structure of the HTML (please note that the links and text following them are on the same line): <li> <strong>Heading of section</str ...

ng-model causing simultaneous changes to all selects

Check out this jsfiddle link Hello there, I'm encountering an issue with my application. I need to create multiple dropdowns using ng-repeat and have them all populated with the same options. The problem arises when one dropdown is changed, all ot ...

Issue encountered when submitting form: Error identified as "Unexpected string expression without template curly in string"

As a novice in React.js, I am facing an issue where setState is not functioning properly when submitting a form. Any suggestions on how to resolve this problem? > class Home extends Component{ constructor(props){ > super(props) > ...

Managing events with classes

I have multiple divs with the same class. When one of these divs is clicked, I want to change the display of the other divs to 'block'. Currently, I am using inline JavaScript for this functionality, but I would like to achieve it without inline ...

Press and hold feature using CSS or JavaScript

When working with a jQuery draggable element, my objective is to change the cursor to a move cursor when clicking and holding the header. I have attempted using CSS active and focus properties, but so far no changes are taking place. ...

Modify the textfield label color using Material-UI

Hey there! I'm running into a little issue when trying to change the color of the text label in a MUI text field. I've successfully customized the border colors and hover states, including the label, but for some reason, I can't seem to get ...

Image alignment on a web page

I'm currently working on developing a website that features a unique wave effect at the bottom of the 'hero' image. From my research, I've learned that this can be achieved by cropping the bottom part of the image, making modifications, ...

Creating an animated sidebar that moves at a different speed than the rest of the

I have a layout with a large sidebar and small content, where the "Fixed part" refers to sticky positioning. I'm attempting to achieve this using scrollTop, but the sidebar is causing some issues like this: The code should only be executed when the ...