Is it possible to have my div inherit its height and position from one div, while inheriting its width from another div

I am facing an issue with a div (div1) nested inside other divs. I want this div to span the full width of the display but maintain the same height and position as its parent div (div2). I have experimented with using position: absolute; on div1, but encountered difficulties in achieving the desired layout.

  • One approach involved setting the body to position: relative; to enable div1 to occupy 100% width while struggling to synchronize its height with that of div2.
  • Alternatively, setting the parent div (div2) to position: relative; allowed div1 to fill 100% height but presented challenges in matching its width with that of the body.

It would be beneficial if CSS offered functionality like:

.div1 {
height: 100%(.div2);
width: 100%(body);
position: 0(.div2);
}

Or something similar

JSFiddle showcasing the relevant details: https://jsfiddle.net/Hamleyburger/fqe5o46c/1/#&togetherjs=K02DaSO2nR

  • My goal is to have the div ".selectable" contain a sub-div (inside?) that appears on hover, aligning in height with ".selectable" (its parent) and spanning the entire width of the body.

Additional information that may be pertinent: I incorporate Bootstrap and (Jinja2) templating. The widths of the existing divs derive from a wrapper container (.main) in my base template which is intentionally set narrower than the body for responsive design. If I were to eliminate .main div, adjusting individual div widths becomes necessary. While this solution might work by making non-div1 divs narrower, it lacks DRYness. SASS is utilized in my workflow.

Answer №1

One way to make a div expand to the full width of the viewport is by using the unit vw. It may seem a bit unconventional, but it can be done like this:

body,
html {
    margin: 0;
    padding: 0;
}

.outer {
    width: 300px;
    height: 300px;
    background-color: #eeeeee;
    margin: 0 auto;
    position: relative
}

.inner {
    width: 100vw;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.1);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
<div class="outer">
    <div class="inner"></div>
</div>

If you prefer a more straightforward approach, you could make the outer div fill the entire width and specify a width for the inner div:

body,
html {
    margin: 0;
    padding: 0;
}

.outer {
    width: 100%;
    background-color: #eeeeee;
    position: relative
}

.inner {
    width: 300px;
    height: 300px;
    margin: 0 auto;
    background-color: rgba(0, 0, 0, 0.1);
}
<div class="outer">
    <div class="inner"></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

What is the best way to add a transition to a list element so that it expands in size without impacting its background image or the surrounding list items?

Lately, I've been working on revamping a flat design website and my focus has mainly been on the navbar. The original plan was to incorporate simple icons on a colored background that would darken when hovered over by the user's mouse. To achieve ...

Tips for incorporating scrollbars into a full screen web application's content area that spans the entire height of the screen with CSS

I am currently working on a full screen web app. I have applied CSS 100% height to the HTML, body, and parent elements. Within the app, there is a contents table that dynamically grows as items are added to it. My goal is to have a vertical scrollbar autom ...

Troubleshooting the problem with Bootstrap 4 dropdowns not opening

After updating bootstrap to v4 in my angular 5 application, I encountered an issue with the dropdown menu. When I try to open it for the first time, I need to click twice before it opens. However, after this initial hiccup, it functions properly. https:// ...

Is there a way to prevent errors from appearing when using PHPMailer, ensuring that the email is sent and received successfully without any error messages showing

Here is the code snippet I am using to send emails: <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; $receiving_email_address = '<a href="/cdn-cg ...

When integrating React with Tawk, the user's name and email are automatically encoded before being sent

My code within the componentDidMount() function initializes a widget popup when the page loads. It then sets the name and email using parameters received from the previous page. const fullName = this.state.data[0]; console.log(fullName); const e ...

Ionic2 - Ion-select malfunctioning on a particular page

I have encountered an issue with the ion-select component in my Ionic 2 app. Specifically, when navigating to a certain page, the ion-select does not display properly. Strangely enough, on other pages of the app, this component works perfectly fine. Below ...

Modifying drop down select list by adding or removing options entirely

How can I use JavaScript code to dynamically add or remove a dropdown list for selecting language and proficiency when a user clicks a button? I have tested out various codes from different sources, but none of them seem to be working! function addLang ...

Dynamic background scaling for Wordpress sections - is it responsive?

I am in the process of creating a WordPress website and I have set a section background with an image that has a width of 1920px. However, when viewing it on my 1366px screen, the image does not scale properly and gets cut off from the right due to the p ...

What are the ways to integrate JSON data into Angular?

I am facing some issues with integrating JSON in Angular. Within my project, I am generating components and deleting the spec.ts files. Subsequently, I create a new file called prod.data.json { "products" : [ { ...

Display the current date on a webpage using a Google calendar integration

I am struggling to display only today's events in my HTML output using this code. When I remove the line "if ($date = date("Y-m-d")){", the code shows a specific number of events. How can I modify the code to show all events that occur on a single day ...

Identify whether the file is suitable for downloading via UIWebview

I am currently working on a project where I need to detect audio files (mp3, mp4, m4a, and wav) when clicking a link within a UIWebview. I have implemented the delegate call -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)re ...

Looking for the location of a matching brace in a dataset?

As a newbie in the world of coding, I have embarked on learning about NodeJs file system module. Currently, my focus is on handling a large data file stored as a string. The challenge that I am facing is with locating the matching close brace and its pos ...

Exploring ways to tailor regex for tel input type in HTML5

Here is the current Regex pattern I am using: <input type="tel" name="phone" value="" placeholder="Phone Number" title="019XXXXXXX" required pattern="[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{4}"> Currently, this forces users to enter exactly 12 digits to pass ...

Encountering a display issue with Facebook page iFrame on a Bootstrap Template when integrating Laravel as the backend for embedding the iFrame

I am currently using a bootstrap template and I would like to showcase a Facebook page iFrame within the sidebar. The iFrame code I am utilizing is generated by the Facebook Page Plugin. Below is the code: <iframe src="https://www.facebook.com/plugins/ ...

Utilizing Bootstrap to emphasize the selected navbar item by adding the active class upon

I was attempting to emphasize the clicked navbar by adding an active class. This is the code I tested: <!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> < ...

Exploring the Interaction of Users with HTML Table Cell <td>

I am currently analyzing the code for a web page. Within this code, users have the ability to double-click on a cell in a table (<td> as shown below) and input a value. Is there a specific attribute or element within this HTML that indicates user in ...

Troubleshooting issue: Failure in proper functionality of Jquery's slideDown

My form is divided into 3 sections, with the latter two initially hidden using CSS. When users click the next button in the first section, the second section is supposed to slide down into view. However, I'm experiencing an issue where the animation s ...

Tips for identifying when the value of a div changes using Selenium

Currently, I am using selenium to gather data from a website. The structure consists of a parent div with 30 children divs. Periodically, one value is updated and stored in the first child while the last child is removed, maintaining a history of the las ...

Combine three sets of columns with three additional sets of columns using the Bootstrap framework, without considering their sizes

Currently utilizing Boostrap, I have set up 2 rows with 3 columns each. Displaying the layout in the image below: https://i.sstatic.net/Ye08T.jpg The issue lies in the bottom row where the 3 columns are aligned in a single line. I aim for the 3 columns ...

Is it necessary to update the HTML lang attribute when switching between languages dynamically?

I have a one-page website that loads an AngularJS application supporting multiple languages. All the pages on the site are generated in the browser using JavaScript after the initial HTML is fetched from the server. I'm aware of the importance of se ...