Overlaying images with cropped elements

When uploading an image on a webpage, I want to display a preview of the image. I am looking to create an overlay that showcases the image in a rectangle at the center, surrounded by a semi-transparent background outside the rectangle.

Something resembling this example:

https://i.sstatic.net/fYzVZ.png

I have successfully created a div overlay, but now I need to modify it so that the opaque black backdrop covers everything except for the rectangle. This way, users can see what the final result will look like.

This is my current code setup:

.wrapper {
  position: relative;
  height: fit-content;
  width: fit-content;
  display: flex;
}

.overlay {
  max-width: 100%;
  height: 100%;
  aspect-ratio: 1 / 1.42;
  background-color: rgba(0, 0, 0, 0.4);
  position: absolute;
  top: 0;
  left: 150px;
  border-radius: 0.5rem;
}
<div class="wrapper">
  <img src="https://picsum.photos/500/300"/>
  <div class="overlay"></div>
</div>

Specifications

  1. The rectangle's aspect ratio and size must remain consistent.
  2. Avoid using hardcoded values or fixed dimensions unless calculated dynamically based on the image. The layout should adapt responsively to various screen sizes.
  3. Restrictions on changing HTML structure due to use of drag and drop API to adjust the rectangle within the image wrapper by altering its positioning.
  4. Preference for utilizing CSS or JavaScript solutions rather than additional HTML modifications.
  5. Exclusion of object-fit: cover as requirement dictates displaying the complete image in its original proportions.

Answer №1

Here is a code snippet that should fulfill your requirements and address the issue at hand.

.container {
  position: relative;
  height: fit-content;
  width: fit-content;
  display: flex;
}

.overlay {
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 0.5rem;
  width: 100%;
  overflow: hidden;
}
.overlay:before{
    position: absolute;
    content: '';
    aspect-ratio: 1 / 1.42;
    height: 100%;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    box-shadow: 0px 0px 0px 145px rgba(0, 0, 0, 0.4);
  
}
<div class="container">
  <img src="https://picsum.photos/500/300"/>
  <div class="overlay"></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

From HTML to Python to Serial with WebIOPi

I am facing a dilemma and seeking help. Thank you in advance for any guidance! My project involves mounting a raspberry pi 2 b+ on an RC Crawler rover, utilizing WebIOPi for the task. However, I am encountering challenges and unable to find useful resourc ...

What could be causing me to not receive the prepackaged error messages from Angular in my WebStorm 8?

Having some trouble here... my angular errors are always so cryptic, like this: I usually manage to figure out the issue on my own, but I'm really hoping someone can offer guidance on how to get those nice error messages that angular supposedly displ ...

MongoDB Integration of Collections - No Data Population

Having trouble merging a client and an account collection. When I use res.send(client), only the account id's are returned. Unsure how to include account information in clients. Have seen one to many solutions, but struggling with this two-way relati ...

When representing audio as sound bars on a canvas, the previous drawing is retained if canvas height is not specified

After obtaining an audioBuffer containing an audio clip, I proceed to create a visualization by drawing a series of sound bars in the shape of a circle: const { audioContext, analyser } = this.getAudioContext(); const source = audioContext.createBufferSou ...

How to create a bottom border in several TD elements using a combination of CSS and jQuery

Check out my Website: Search Page I have written some CSS that functions well when searching by Name, but not by Specialty: .displayresult { display: block; } #fname, #lname, #splabel, #addlabel, #pnlabel { border-width: 4px; border-bottom-st ...

Using JQuery to delete an item by clicking on the delete button and then clicking on the item to remove it

I am currently using jQuery to dynamically create items on an HTML canvas for users to drag around and create drawings in a style similar to Microsoft Visio. However, I am struggling with how to remove these items once they have been created. While I know ...

Bring in numerous documents utilizing a glob pattern

Currently, I am in the process of developing a modular React application. However, I have encountered an issue where I am unable to dynamically import the routes for my app. Consider the following file structure: app ├── app.js └── modules ...

Is the ngShow directive dependent on the parent variable in any way?

There is a piece of code running in the $rootScope to establish a global value for userLoggedIn: mod.run(function($rootScope) { $rootScope.userLoggedIn = false; $rootScope.$on('loggedIn', function(event, args) { $rootScope.userL ...

What is the best way to prevent automatic trimming in EJS variable assignment in Node.js?

When I attempt to write a variable from the database into an EJS table, it is being displayed with default trimming by the EJS template. However, I would like to display the variable from the database without any default trimming. I consulted the EJS temp ...

What is the best way to incorporate a sidebar menu in an HTML webpage?

I am interested in creating a sidebar menu for my website using either HTML, CSS, or JavaScript. The W3 Schools website has a side menu that I find appealing and would like to create something similar. ...

Using JQuery to make a GET request and receive a JSON response, then selecting particular objects

I am in the process of developing a straightforward web application. The concept involves users inputting a license plate number, which will then connect to an OpenData API based on Socrata that houses information about all registered vehicles in my countr ...

Tips for organizing a Material UI DataGrid table effectively while keeping the total sum rows within the DataGrid unaffected

Check out my codeSandbox link here: https://codesandbox.io/s/making-sum-of-column-in-datagrid-mui-zjzfq6?file=/demo.js I've noticed that when I sort by ascending, the subtotal, total, and tax rows move up, but when I sort by descending, they move dow ...

Tips for disabling autofocus on Mui Menu Items

Incorporating a search functionality within a custom Mui Select component where the user can input a city or country to filter down the options list. The current issue is that when a user types a letter that matches the first letter of an option, the opt ...

The functionality of Angular.js ajax and apply is experiencing technical difficulties

As I venture into the world of angular.js, I am facing a challenge with displaying the correct content on my website. The pages' content is fetched via AJAX (currently from static data, but eventually from a database). When I place a block with the ng ...

Using jQuery to swap an image with a div element and then using the image as the

I have a collection of images in a div, like so: <div class="image-container"> <img width="174" height="174" src="http://mysite.com/images/portfolio/p1.jpg" class="image-style" typeof="foaf:Image"> <img width="174" height="174" src= ...

I must update a bootstrap class name within multiple layers of divs by referring to the parent class

My code structure is set up like this: <div id="main-parent"> <div class="child2"> <div> child2 </div> </div> <div>child3</div> - - - - <div class="ch ...

JavaScript validation is failing to return false when re-entering the password

My JavaScript validation code is working fine. Javascript: All fields return false when re-entering the password, but JavaScript does not return false if it's not working The Re-Enter password JavaScript code is failing to work... An alert is displ ...

I am struggling to find the right way to center those images

https://i.sstatic.net/aRkvl.jpg Hello everyone, I'm currently attempting to set up the main content of the homepage resembling the image provided, but I'm encountering some challenges. Every approach I take seems to cause the image to overflow ...

Learn how to retrieve information using the dash operator in ReactJS

I find it a bit amusing, but I'm encountering an issue. Can anyone lend me a hand with this? So, I'm using an API to fetch some data and it appears that the response from the API is in this format: start-time:2323232 end-time:2332323 Now, when I ...

What is the best way to save the output of an asynchronous (AJAX) function in a variable?

This isn't a repeat query. I'm seeking assistance in locating a technical solution that hasn't been covered in the post How do I return the response from an asynchronous call? If .then() doesn't resolve the Promise, how can I pinpoint ...