Create a child element to match the width of its grandparent element

Would it be possible to make the innermost div as wide as the section in my nested structure?

Below is a snippet of the code:

body {
    margin: 0 auto;
    padding: 0;
    background-color: blue;
}

#content {
    background-color: orange;
    padding: 20px;
    width: 300px;
    height: 100px;
    margin: 0 auto;
}

#inner {
  background-color: pink;
}
<body>
  <section id="content">
    <div>
      <p>
        test
      </p>
      <div id="inner">
        <p>
          inner
        </p>
      </div>
    </div>
  </section>
</body>

Answer №1

To achieve the desired layout, you can either create a middle element with a fixed width or use negative margins along with fixed widths and paddings.

Check out the demo below which demonstrates the concept of using negative margins:

body {
    margin: 0 auto;
    padding: 0;
    background-color: blue;
}


#content {
    background-color: orange;
    padding: 20px;
    width: 300px;
    height: 100px;
    margin: 0 auto;
}

#inner {
  background-color: pink;
  margin: 0 -20px;
  padding: 0 20px;
}
<body>
  <section id="content">
    <div>
      <p>
        test
      </p>
      <div id="inner">
        <p>
          inner
        </p>
      </div>
    </div>
  </section>
</body>

Answer №2

To achieve this, you can take the following steps:

body {
    margin: 0 auto;
    padding: 0;
    background-color: blue;
}


#content {
    background-color: orange;
    padding: 20px;
    width: 300px;
    height: 100px;
    margin: 0 auto;
}
#content > div {
    width: inherit;
}


#inner {
  width: inherit;
  background-color: pink;
}

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

Show a hyperlink within dynamically retrieved HTML content

As I generate a list of strings dynamically, I am displaying it in Angular using ngFor. However, some of the strings contain hyperlinks which are not being distinguished properly when displayed - they appear as normal text. I want to differentiate these ...

Adjust the font size of the immediate child element that belongs to the fs-5 class

I have implemented a media query specifying the pixel value at which I want to apply a specific font size to all direct child elements (paragraphs) of the div. Within these paragraphs, there is one with the class=fs-5. However, when viewing it responsivel ...

Is there a way to prevent navbar links from wrapping when closed with CSS?

Upon closing my side navbar, I encountered an issue where the links warp to the size of the navbar. I am seeking a solution to keep the links (highlighted in pink in the image below) the same size without warping. Is there a CSS technique to achieve this? ...

Is there a way to create two proportional columns using HTML/CSS, with the left column sticking to the user's view as they scroll?

I am in need of a two-column layout where the columns are proportional in size and do not overlap. The left column should have minimal content at the top and scroll along with the page as the user scrolls down. On the other hand, the right column will cont ...

The presentation of the Google graph with dynamically changing data appears to be displaying inaccurately

I am looking to incorporate a graph displaying sales and purchase data on my webpage. Users should be able to select from categories like Purchase, Sales, or Production. I have separate tables for Purchase (AccPurchase) and Sales (AccSales), with productio ...

Right-align the text in the title of the material card

Why isn't the CSS aligning my title of matcard to the right? <mat-card [ngStyle]="{ 'margin':'5px','height':'130px'}"> <mat-card-header> <mat-card-title [ngStyle]="{ 'text-align': ...

Text that is not aligned in the middle and has a colored background

After setting up a flexbox container with some flex-items, I encountered an issue: When clicking on either "Overview" or "Alerts", the white background border is not displayed. To highlight the selected item, a class called .selected is triggered which ad ...

The jQuery scroll functionality is malfunctioning

Could you please assist me in resolving an issue with my scroll effects not working? I have been unable to pinpoint the problem. The code snippet I am using is based on Bootstrap 4. Below is the code snippet in question: //SCROLL EFFECT $(".nav-link" ...

Outdated ExpiresByType directive in .htaccess leads to website layout distortion. What is the best way to prompt the browser cache to refresh?

I recently updated my website and had the following configurations in the old .htaccess file: <IfModule mod_expires.c> ExpiresActive On # Images ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" Expi ...

Having trouble accurately obtaining the height of a DIV element using jQuery

After trying to troubleshoot on my own, I ran into a roadblock due to my specific requirements not being met by existing solutions. The issue at hand is as follows: I have a sticky DIV element positioned to the left, nested within another DIV Element wit ...

Effortlessly glide to a specific element on an HTML page

I am working with XML data that contains a lot of Test information. I am using XSLT to display this data as HTML in a web browser. Each test includes a table with the test steps, and every time a test is executed, the table updates to show whether each s ...

Import files from local directory to iframe in Electron application

I am currently working on an application using Electron that allows users to preview HTML5 banner ads stored locally on their computer. At this point, I have enabled the ability to choose a folder containing all the necessary files. Once a directory is s ...

Obtaining the node generated from a document fragment

Suppose I add a document fragment to the DOM in the following way: const ul = document.querySelector("ul"); const fruits = ["Apple", "Orange", "Banana", "Melon"]; const fragment = new DocumentFragment(); ...

The sticky navigation bar's background fails to change at the appropriate time following an image slider

I am currently facing an issue with my sticky navigation bar. I want the background color to change to white after the user scrolls past the image slider. The problem is that the white background appears above the image slider instead of in the orange logo ...

A guide to displaying the beginning content of an HTML input element programmatically in React, specifically focusing on cases where the text exceeds the width of the input

My input element initially contains the value 'abcdefghijklmnopqrstuvwxyz' I utilized ref to highlight the characters from 'n' to 'z' inputRef.current.setSelectionRange(18, 26); This code snippet results in the following: h ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

Remove vertical spacing from rows of thumbnail grid in bootstrap

I have been attempting to eliminate the vertical space between rows in a thumbnail grid, similar to how I did for horizontal spacing, but have not been successful. .gutter-0.row { margin-right: 0; margin-left: 0; /* not working */ margin-t ...

Implementing timeAgo with jQuery (or a similar tool) to display the elapsed time since a user (client) accesses or updates a webpage

https://i.sstatic.net/Y7bzO.png Currently, the timer displayed always shows the actual time instead of phrases like "about a minute ago" or "5 minutes ago". I have tried different solutions without success. //timeago by jQuery (function timeAgo(selector) ...

What is the best way to add content to a box once it has been hovered over?

Looking to create a sticky note-style design, but struggling with displaying the content inside the box only when hovered. In my code example below, can you help me achieve this effect? body { margin: 45px; } .box { display:inline-block; backgrou ...

What is the best way to remove headers and footers programmatically in print pages on Safari using JavaScript?

Struggling with eliminating the header and footer on print pages in Safari using JavaScript? While disabling the header and footer manually can be done through print settings in most browsers, my aim is to automate this process with code to ensure that use ...