establish the parent child size with CSS

In my current project, I am faced with a challenge regarding the CSS and HTML code. I am attempting to have a <div> element positioned after an <img> element inherit the width and height of the image. This <div> is meant to act as an overlay. Is there a way to make the <div> automatically adjust its dimensions based on the width and height of the <img> without resorting to JavaScript?

At this point, I have yet to experiment with JavaScript and would prefer a solution that relies solely on CSS. However, if necessary, I am willing to incorporate JavaScript. It is worth noting that I am not using jQuery in this project and have no plans to do so.

HTML

<div class="div_1">
  <div><img src=""/><div></div></div>
  <div><img src=""/><div></div></div>
  ....
</div>

CSS

.div_1 > div {
  float: left;
  margin: 8px;
}

.div_1 > div > img {
  height: 300px;
  width: auto;
}

.div_1 > div > div {
  width: .div_1.div.img.width;
  height: .div_1.div.img.height;
}

Answer №1

To achieve this effect, you can adjust the inner div to have a position of absolute and the outer div to have a position of relative:

.div_1 > div {
  float: left;
  margin: 8px;
  position: relative;
}
.div_1 > div > img {
  height: 300px;
  width: auto;
  display: block; /* remove default spacing from bottom of <img> */
}
.div_1 > div > div {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 0, 0, 0.5);
}

/* Additional styling... */

.div_1 > div > div:after {
  content: "Ummmmm, bacon!";
  color: #FFF;
  font-size: 30px;
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="div_1">
  <div>
    <img src="http://baconmockup.com/200/200" />
    <div></div>
  </div>
  <div>
    <img src="http://baconmockup.com/200/200" />
    <div></div>
  </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

Transmitting personalized information with Cylon.js and SocketIO

After making adjustments to my code based on the example provided here https://github.com/hybridgroup/cylon-api-socketio/tree/master/examples/robot_events_commands This is the complete server code, currently running on an Edison board. Everything functio ...

When switching from JavaScript to jQuery, the button values become invisible

Currently, I have a functional app that can dynamically change the values of buttons based on user input. The current implementation is in vanilla JavaScript within the script.js file. However, I am looking to enhance the functionality and user experience ...

Creating a dynamic hyperlink variable that updates based on user input

I am attempting to create a dynamic mailto: link that changes based on user input from a text field and button click. I have successfully achieved this without using the href attribute, but I am encountering issues when trying to set it with the href attr ...

Does Angular's compile function operate asynchronously?

In my service, I use $compile to compile the template. The JavaScript functions are executed one after another, but in order to retrieve the final HTML content, I have to place html() within a timeout callback. Otherwise, the template includes {{ placeho ...

Are items placed into an array passed by reference or by value?

When working with custom Objects in an Angular context, I define two objects "a" and "b" using an interface. These are created as class attributes along with an empty array of these objects. public a: CustomObj; public b: CustomObj; public array: ...

Issue with Laravel - JavaScript not functioning properly following the passage of a parameter through the route

I am currently working on a website that will display search results from various e-marketplaces. Everything was going smoothly with the JavaScript implementation until I attempted to pass parameters through the route. Once I did that, the results stopped ...

Step-by-step guide on how to load an Ext JS tab after clicking on it

I have an Ext JS code block that creates 4 tabs with Javascript: var tabs; $(document).ready( function() { fullscreen: true, renderTo: 'tabs1', width:900, activeTab: 0, frame:true, ...

Achieving proper variable-string equality in Angular.js

In my Angular.js application, I am utilizing data from a GET Request as shown below. var app = angular.module('Saidas',[]); app.controller('Status', function($scope, $http, $interval) { $interval(function(){ ...

Mastering Bootstrap: Achieving flawless column stacking in succession

I am currently integrating bootstrap into my existing HTML code, but I only require it in two specific sections to avoid rewriting the entire code. Everything looks fine on my 1080p screen as intended. However, when I resize the screen slightly The titl ...

Troubleshooting: Express JS and MongoDB Post Request Issue

Today, I delved into building a cutting-edge MERN App and embarked on crafting a restful api. To kick things off, I opted to utilize mlab as the storage solution for my mongodb database. Following the creation of a user, I successfully established a connec ...

Unable to access the following element using jQuery's next() method

Can someone help me understand how the "next" function works in jQuery? I'm trying to reveal a hidden textarea when a span element is clicked. The hidden textarea is supposed to be the immediate next sibling of the span, but it's not working as e ...

My header has a video background that appears aesthetically pleasing on a desktop screen. However, when viewed on a mobile device, it is off-center and doesn't span the full

I am having an issue with a header that has a video background. It looks great on desktop, but on mobile devices, it is only taking up about 2/3 of the screen width and is aligned to the left side instead of filling the entire width. I have tried several ...

Transfer the choices from a dropdown list to a different JSP page

In my current JSP code, I have a select element that looks like the following: <select name="withoutAccess" size="5"> <c:foreach var="item" items="${obj.withoutAccess}"> <option>${item}</option> </c:foreach> </sel ...

The JavaScript program is occasionally receiving unconventional input

I'm really struggling with this one. As part of a programming exercise, I am developing a JavaScript calculator. You can access the functioning calculator here on Codepen. At the bottom left corner of the calculator interface, you will notice a "+-" ...

A mock or spy must be used for the jest function

I'm having an issue with the last expectation not being called in a test I'm writing to test the actions within my application. const pushData = jest.fn(() => Promise.resolve()); test('anotherAsyncCall is fired to get more info', ( ...

Utilizing Locale to Rewrite URLs in Next.js Version 13

I've been attempting to rewrite the URL based on the locale extracted from my middleware.js, but for some reason, the URL isn't being rewritten and leads to a page-not-found error 404. Strangely though, if I manually navigate to "localhost:3000/e ...

increasing the size of the input thumb compared to the others

Hello fellow React developers, I'm currently learning by coding and I have a question about a slider. I'm trying to make the thumb of the slider bigger, but when I increase its size it doesn't fully display within the input area (as you can ...

Tips for ensuring an image remains consistently positioned alongside another element

I have a request - In my code, I have multiple h2 headings that I want to display with an image of the Orioles Logo on both sides. There are about 100 of them, and now I need to change all of them to show the Ravens Logo instead. Doing this manually would ...

Applying CSS blur filter to container without affecting its content

I'm trying to create a hover effect where an image within a parent div transitions into a blurred state. However, I've run into an issue where if the image is set to 100% width/height of the parent div, there is a visible bezel of the parent&apos ...

Integrate properties into a React component using an object as the representation

Can props be added to a component represented by an object? I am looking to add these props just once, within the map function, if possible. children: [ { id: '1', isActive: false, label: 'Home', component: & ...