Is it possible to overlap precisely half of one image with another using CSS?

Is it possible to overlap exactly half of an image in CSS using another image while maintaining a set height? The width of the images will vary based on their aspect ratios. Can this be achieved with CSS alone or would JavaScript need to be involved?

The following code demonstrates positioning one image over another, but I want to achieve the overlapping effect automatically without manually adjusting positions. Is there a way for CSS to handle this? Or perhaps a JavaScript solution exists? In the code provided, the height should remain the same, and half of any image used should overlap horizontally.

<html>
    <head>
        <style type="text/css">

            #collage-container{
                width:300px;
                height: 200px;
                position: relative;
                background:#f22;
            }

            #collage-one, #collage-two{
                height:200px;
                position:absolute;
            }

            #collage-one{
                z-index:1;
                left:100px;
                position:absolute;
            }
        </style>
    </head>
    <body>
        <div id=collage-container>
            <img src="http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp" id=collage-one />
            <img src="http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp" id=collage-two />
        </div>
    </body>
</html>

Answer №1

When dealing with images of varying widths, a useful technique is to utilize the CSS transform function translate() along with a percentage value to adjust their positioning based on their width:

SEE EXAMPLE HERE

#collage-container {
  height: 200px;
  position: relative;
}

#collage-container img {
  height: 100%; /* Takes up full container height */
  width: auto;
  float: left;
}

#collage-container img + img {  /* Translate second image by 50% of its width to the left */
  transform: translateX(-50%);
}

Keep in mind that CSS transforms are compatible with browsers from IE9 onwards

Answer №2

In my opinion, it's quite straightforward:

<html>
<head>
<style type=text/css>
  .container {
    float:left;
  }
  .half-img {
    display:inline-block;
    width:25%;
  } 
  .clear {clear:left;}
</style>
</head>
<body>
<div class="container">
  <span class="half-img">
    <img src="http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp" width="100">
  </span><img src="http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp" width="100">
</div>
<div class="clear"></div>
</body>
</html>

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

Retrieve an element within a jQuery each loop

I'm currently implementing AJAX functionality to retrieve cart items from the server and display them within a cart when a customer clicks on the "My Cart" button. Here is the model for the cart: public class Cart { [Key] public i ...

Guide for adding a button in HTML table with event creation using ASP.NET and C# language

I am trying to dynamically add a button in each row of an HTML table column using C#. The button is being created for each row, but the click event is not being triggered. Here is the code snippet from the code behind and ASP.NET: public string ge ...

Toggling a div updates the content of a different div

I am looking to make the content within <div class="tcw-content"> dynamically change when a different div is clicked. I have little experience with Ajax, so I'm wondering if there are alternative ways to accomplish this using JS/CSS. If anyone h ...

Discover the process of dynamically importing JavaScript libraries, modules, and non-component elements within a Next.js

Lately, I have been utilizing Next.js and mastering its dynamic import feature for importing components with named exports. However, I recently encountered a particular npm package that functions only on the client-side (requires window) and has a substant ...

What is the solution to prevent the div tag from being empty within Jquery UI Tabs?

I found this example that I am currently following: http://jqueryui.com/demos/tabs/#ajax After clicking on Tab1 and Tab2 in their example, the text disappears immediately, and the div box shrinks before new data is loaded. Is there a better way to handle ...

Tips for enhancing redundancy in HTML and ROR using partials

Looking for a way to merge two partials in my Ruby on Rails app without copy-pasting. Here's what I'm aiming for: @member = 'Player' render 'team/team_partial' @member = 'Staff' render 'team/team_partial&a ...

What is the method to choose an invisible link without an identifier using Selenium WebDriver?

I am attempting to click on the link labeled 'User Requested'. This link only appears when hovering over the menu. This is the HTML code: <ul class="k-widget k-reset k-header k-menu k-menu-horizontal" id="menu" data-role="menu" tabindex="0" ...

Transforming a Blob document into JSON format

I am attempting to convert a blob file into JSON format in order to transmit it via AJAX requests. Despite my efforts with the code below, I have not been successful in achieving this task. When I attempt to parse the JSONified file, I end up with a comp ...

Amazon S3 is not sending the 'Access-Control-Allow-Origin' header for the requested resource

Currently utilizing reactjs and axios Encountering an issue while attempting to fetch a PNG file from S3 to the local environment. Access to fetch at 'https://xxx-xxx-xxx.s3.ap-northeast-1.amazonaws.com/0.png' from origin 'http://localhost: ...

Displaying data retrieved from Angular by clicking a dropdown button

Struggling with a minor issue while using Angular on the frontend and .NET Core on the backend? Look no further, I have created a video demonstration below to help you out. Despite spending countless hours trying to solve the problem, it still persists. T ...

Utilizing Django fixtures: Importing HTML content as JSON into a Text Field

Currently in the process of transitioning my website to Django from another content management system, I am facing challenges with importing HTML into Django using fixtures. The specific issue revolves around "Events", which is a model within my Django app ...

What are the steps to changing the navbar's color when scrolling down?

I am having trouble changing the color of my navigation bar from transparent to black after the user scrolls down. Despite trying various methods and watching tutorials, I can't seem to get it to work. HTML <nav role='navigation' class= ...

What is the best option: using a Javascript template or exploring another

Just starting out in web development. I want to include the same menu on every page of my new website, but I don't want to manually update it in each file whenever there's a change. Is there an easy template engine for JavaScript or another sol ...

Is there a way to change the source attribute instead of the text content?

Is there a way to update the src attribute instead of the text content? For instance, if I want a winter image, title, and description for December, and a summer image, title, and description for August. How can I store images in this JSON format and use t ...

Guide on displaying an EJS page with AngularJS on a Node.js server

Having an issue with rendering an ejs page through nodejs. The page works fine independently, but fails to load when rendered via nodejs. Any help would be appreciated. ejs page: <html> <script src="/home/aniruddha/Downloads/angular.js"></ ...

What is the best way to eliminate specified users from the list of command arguments?

I am looking for a way to remove user mentions from a command's arguments array. Although I tried to follow the instructions in the official Discord JS guide here, the example provided only works if the mention is in a specific argument position. Th ...

Exploring the World of PHP, MySQL, and AJAX

Recently, I came across an issue where I needed to extract values from a MySQL database using JavaScript. What I intended to achieve was to dynamically add a div element when a PHP page is loaded for editing information. The plan was to populate this div w ...

Prevent UI-Router from Interacting with the browser's window location

Seeking input on a unique challenge regarding UI-router in AngularJS. In the process of migrating a project to Angular JS, I encountered an issue with multiple panels being dynamically loaded onto a page using AJAX and managed by jQuery History. Each pane ...

Error: Unable to define $scope function in Angular and Jasmine integration

I am currently working with a controller var videoApp = angular.module('videoApp', ['videoAppFilters', 'ui.unique', 'angularUtils.directives.dirPagination']); videoApp.controller('VideoCtrl', function ($sc ...

Footer displaying incorrectly

My webpage has a white footer set at 30px in height. However, when I scroll down the page, the footer expands and overlaps with the text part (a div with a light grey background). Can you help me figure out what's causing this issue and how to fix it? ...