Make the width of the div equal to the width of its grandparent element

<div class = "box" style="width:100%;">
    <div class = "left-side" style="width:50%;float:left;">
        <div class = "inner-box" style="width:100%;">
        </div>
    </div>
    <div class = "right-side" style="width:50%;float:right;">
        <div class = "inner-box-right" style="width:100%;">
        </div>
    </div>
</div>

Is it possible to set the width of the "inner-box" or "inner-box-right" div to match the width of the "box" div using only CSS?

UPDATE
Here's the solution link(using the answer provided below)

Answer №1

To achieve this, you can set the parent element x to have a relative position and then set the z element to have an absolute position.

.x{
position: relative;
}
.z{
position: absolute ;
top: 0;
left: 0;
width: 100%;
}

Answer №2

To make a property inherit its value from its parent element, you can utilize the inherit keyword.

HTML:

<div class = "a">
   <div class = "b">
       <div class = "c">
       </div>
   </div>
</div>

CSS:

.a {
width: 200px; 
border: 2px dashed blue;
height: 150px;
}

.c {
width: inherit; 
border: 1px solid green;
height: 100px;
}

Check out this Fiddle link for further reference.

Answer №3

To set the width of .x, followed by giving .z a width of inherit is essential. If not, you will likely need to individually define the widths unless you plan on creating a box model where .x serves as the larger container for .y and .z.

For instance, assign a width of 100% to .x and then specify .y as 25px. Utilizing {display: -webkit-box;} and {-webkit-box-flex:1;} attributes can be helpful in this configuration. Subsequently, provide a max-width value for .x and adjust either y or z's width accordingly based on screen size variations.

In your HTML code, it may be necessary to designate the widths separately unless employing a box model approach.

I trust that this guidance proves beneficial to you.

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

Looking for a single solution to have my background image in CSS cover the entire screen on both my computer and cell phone

I used the following code in my demo .html file: body{ background-image: url('https://i0.wp.com/www.socialnews.xyz/wp-content/uploads/2020/10/26/5573c6f0bee7ca021f4a8aecbaf6cbeb.jpg?quality=80&zoom=1&ssl=1'); -webkit-backgroun ...

Creating a dynamic dropdown menu in your Python Flask application using HTML input tags is an excellent way to enhance user experience. By

How can I create a live search box with suggestions from YouTube for the input tag with the id="livebox"? I am looking to add a dropdown suggestions box to the input tag with suggestions from res in JavaScript. Any advice on how to achieve this? The outpu ...

Covering the entire screen, the Div element takes up the entirety of

Just like with other pages, the main div always takes up the entire screen visible to the user. As they scroll down, another div becomes visible: example1 , example2 Regardless of the screen size, the main div will always fill the space visible to the use ...

Overflow-y SweetAlert Icon

Struggling with a website modification issue where using Swal.fire(...) causes an overflow problem affecting the icon rendering. How can this be fixed? Here is the custom CSS code in question: * { margin: 0px; padding: 0px; font-family: &ap ...

Achieving Vertical Content Alignment in Bootstrap 4

I am struggling to achieve a responsive design where two columns stack vertically on top of each other when viewed on smaller mobile screens. Despite numerous attempts, I have been unable to figure it out. Ideally, I want the layout to look like this imag ...

Display the integer value in a div element using PHP

echo "<td>".$value["rating"]."<div class=ratingOutput data-average=".$rates." **data-id="$value["id"]"**></div>".$newline.$rateHere.$starOutput."</td>"; I need the value of data-id to be an integer, however, I encountered an error ...

Creating an MPG4 Video from a Three.js Scene: What Are the Steps?

I am working with a three.js scene that includes numerous animated objects. I am looking to export this animation as an mpg4 video. Here are my questions: What is the best method for exporting a scene to create an mpg4 video? Is it recommended to perfo ...

Seeking help with CSS styling

I ran into an issue while designing a web page using em units. I thought I had a good grasp of it, but when I tried to align two separate span tags with margin-left, they ended up overlapping in the upper-left corner of my header. Even though I used displa ...

Troubleshooting: WordPress background image not displaying

WordPress trouble: The background image I added to my style.css isn't showing up on the site. body{ background: url("/wp-content/themes/my_theme/images.png"); } Any advice would be greatly appreciated! ...

Automatically scraping Facebook API data with PHP

I'm completely new to php and I am looking for a way to automatically scrape metadata. I came across a solution, but I am unsure how to implement it in php. POST /?id={object-instance-id or object-url}&scrape=true Can anyone provide a sample php ...

custom dialog box appears using ajax after successful action

Recently, I created a custom dialog/modal box with the following code: <div id="customdialog" class="modal"> <div class="modal__overlay"></div> <div class="modal__content"> <h2><strong>Hello</strong&g ...

Arranging HTML components in Vue.js using v-for loop

Recently, I started using vue.js and I have a requirement where I need to display an HTML structure based on API data. The structure should look like this: <div class="row"> <div><p>text1</p><img src="{{imgurl1}}" /></di ...

Tips for resolving issues with the navigation menu not appearing on the website

For more information, please visit the webpage at I am facing an issue where the dropdown menu does not show up when hovering on the Services page. The dropdown menu was created in WordPress using wp_nav_menu. There are 5 pages, including a Services page ...

Creating a responsive table using Bootstrap is easy and efficient

Looking for assistance in making a responsive table. Everything looks fine at full size: https://i.sstatic.net/xKSLi.png but when the screen size is reduced I end up with something like this: https://i.sstatic.net/x0kwc.png The table is displayed on col- ...

"Enhance Your Website with a Stunning Background Image Using Twitter Boot

With a fixed navigation bar at the top and a container spanning span12 with full-width background, it is important for the content of the background image to be displayed fully regardless of window size. What would be the most effective way to create or c ...

What is the best way to establish a link between two containers?

Currently, my focus is on developing a unique component that poses a challenge with the connection between two containers. Specifically, I am working on crafting a sign-up form for users that comprises four distinctive sections. Each section triggers its o ...

It is crucial to refrain from making any CSS adjustments in Google Chrome

So I encountered an interesting issue on my website. There are two CSS files - main.css and bootstrap.css, with some overlapping styles. I made a change in the bootstrap.css file by adding !important next to the property. Surprisingly, in Firefox, the chan ...

Is it possible to use VueJS (JavaScript) to submit a form within a PHP file?

Recently, I've been working on updating an old PHP file that includes a form and a table. The code snippet below gives an overview: printf ("<form action=\"%s\" method=post>", $PHP_SELF); //this is the url printf (& ...

What is the best way to apply a className to a component using styled-components in React?

NavigationStyles.js import styled from 'styled-components'; export const Navigation = styled.navwidth: 100%; ; export const MobileNavMenu = styled.ul` height: 80px; .navbar_list_class { font-size: 2rem; background-co ...

After the image has loaded, Context.drawImage is failing to function

I am in the process of creating a freehand drawing application using the HTML canvas element. I am currently working on adding an 'Undo' feature, which involves saving snapshots of the canvas state each time the user draws something. When the use ...