Utilize the image's width inheritance in CSS

I am working on creating a screen layout where 80% of it will be occupied by an image while the remaining 20% will be taken up by a separate div. The image is set to scale its width proportionally to its height, filling the 80% section. However, I want the bottom 20% div to automatically inherit the scaled width of the image as well. Below is my CSS code:

<style type="text/css">
    #background {
width: 100%; 
height: 100%; 
position: relative; 
overflow:hidden;
left: 0px; 
top: 0px; 
z-index: -1; /* Ensure div tag stays behind content; -999 might work, too. */
}

.stretch {
max-width:100%;
height:80%;   
}
</style>

Here is the corresponding HTML:

<div id="background" >
  <img id="status_bg" src="online_pend_off.jpg" class="stretch">
</div>
<div id="twenty_per" style="background-color:blue">
</div>

If you have any suggestions on how I can make the second div (twenty_per) inherit the same width as the status_bg image, please share them. Thank you!

Answer №1

Enclose the HTML content within another div that does not have a set width (to match the width of #background), and ensure that #twenty_per has a width of 100%. This will allow #twenty_per to automatically inherit the same width as #background.

CSS

.wrapper {
    display: inline-block;
}

#twenty_per {
    width: 100%;
}

HTML

<div class="wrapper">
    <div id="background" >
        <img id="status_bg" src="online_pend_off.jpg" class="stretch">
    </div>
    <div id="twenty_per" style="background-color:blue"></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

Preventing the Rendering of Inline-Block Whitespace in Web Browsers

Essentially, in my attempt to create a four-column layout, I have the following HTML code: <div> <div>A column</div> <div>A column</div> <div>A column</div> <div>A column</div> </div ...

How can I display only a horizontal scrollbar on the body content while avoiding an excessively long image header?

Currently, I have a 4000px width image as the header of my website. To hide the horizontal browser scrollbar, I am using the following CSS: html { overflow-x: hidden; } However, this method results in the horizontal scrollbar never appearing. I ac ...

Create a visual representation of an item within a framework using an Angular directive

I am interested in using a directive to draw a triangle above a series of div elements. In my scenario, I have four squares and two values: charge and normal. The value of charge determines the color of the squares, while normal is used for drawing the t ...

After a full day has passed, remove nodes from the Firebase database

Upon initializing my server, I populate Firebase with seed data. In the 24 hours that follow, users contribute additional data, but after this time period elapses, I aim to purge all user data while retaining the original seed information. What is the opt ...

Is it possible to generate assets or files in Angular 4 based on different environments?

Is it possible to generate assets/files on build in our Angular app using environment variables? We are looking to create an 'auth/settings.js' file in the assets folder with client id's and apiUrl's unique to each environment. These v ...

Is the background image unable to fill up the entire screen?

I'm currently facing an issue with my webpage's background not covering the entire vertical space. Despite trying different solutions and referring to previous posts, I haven't been able to resolve it yet. Here is the relevant code: <div ...

Adding and Removing Attributes from Elements in AngularJS: A Step-by-Step Guide

<input name="name" type="text" ng-model="numbers" mandatory> Is there a way to dynamically remove and add the "mandatory" class in Angular JS? Please note that "mandatory" is a custom class that I have implemented. Thank you. ...

Update the DOM by setting the innerHTML with a template tag

I'm currently working on a Vue project and I'm facing an issue with setting the innerHTML of an HTML element using a template tag. Here's an example of what I've tried: let divElem = document.getElementById('divElem') let inne ...

Video constraints in WebRTC are failing to function properly

I'm attempting to achieve a lower resolution from the webcam navigator.getUserMedia({audio: true, video: { "mandatory" : {maxWidth : 320}, "optional" : [] } }, function(stream) { videoElement.src = URL.createObjectURL(str ...

"Easily toggle the visibility of values in checkboxes and organize them into groups with the power of JavaScript

Hey there! I am currently working on a project that involves grouping checkboxes and hiding/unhiding their content based on user interaction. Essentially, when the page first loads, the "All CARS" checkbox will be checked by default. If I then check the "C ...

Is it possible to create an image or logo that moves in sync with the user's scrolling

Having trouble articulating this question, but I'm trying to replicate the logo in the top right corner of this website: I want the logo to move along with the user as they scroll down, remaining visible at all times. Any ideas on how to achieve this ...

Why is the service worker not functioning properly for .tsx/.jsx files in React Vite?

console.log("service worker"); const OFFLINE_ASSETS = [ "/", "/src/main.tsx", "/vite.svg", `https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmCy16nhIbV3pI1qLYHMJKwbH2458oiC9EmA&s`, ]; const addRes ...

It appears that the React MUI Grid is causing the page or its container to experience overflow issues

I'm utilizing Grid to maintain the organization of the entire page, but I keep encountering an overflow issue when adding multiple Grid items. This results in the scrollbar appearing even though the container size remains the same. Dashboard.tsx is pa ...

Is there a button that allows updating the text in a search field by entering multiple values one after the other?

Is it possible to insert multiple values sequentially into the search field using the same button? For example: value 1, value 2, value 3... Check out a demo here <form id="form1" name="form1" method="post"> <p> <input type=" ...

The table spills over the container and disappears underneath the navbar

After downloading a template table from the internet and customizing it to my liking, I encountered an issue where it does not adhere to the assigned settings (as shown in the image), but that's not the only problem. The dropdown navbar on my website ...

Guide to implementing 'active' state in Bootstrap 4 using JavaScript on a basic PHP website

A simple website consisting of three pages, designed using Bootstrap 4 framework. I have utilized includes to incorporate header.php and footer.php into the respective PHP pages below. My challenge lies in adding the 'active' class from Bootstrap ...

Angularjs app unable to retrieve data after deployment

Currently, I am working on a local server developing an AngularJS app on localhost. I have shared the project link with my colleagues using my current IP address within the network to receive feedback. However, when they visit the page, no data is displaye ...

The React engine is triggering an error stating "Module not found."

Utilizing react-engine to enable the server with react component access. Through react-engine, you can provide an express react by directing a URL and utilizing res.render. The documentation specifies that you need to supply a path through req.url. app.use ...

Struggling to accurately convert the string into a date object

I have an array of objects structured like this: const days = [ { _id: 12312323, date : '30/12/2021', dateStatus : 'presence' }, ... ] I am looking to convert the date property from a string to a Date object using the follo ...

How to extract the value of an object from an array using jQuery

I have a set of four input fields: <input type="text" class="kal" name="data[tex1]"> <input type="text" class="kal" name="data[tex2]"> <input type="text" class="kal" name="data[tex3]"> <input type="text" class="kal" name="data[tex4]"& ...