What are some ways to make the parent div grow based on the width, padding, margin, or box of the child div

Is it possible to expand a parent div with a child div and how can I achieve this?

I have created a JSFiddle with some code included.

CSS

body {
    background: gray;
}
div.page {
    color: white;
    background: black;
    max-width: 72em;
    margin: auto;
    padding: 1em;
}
div.one {
    background-color: red;
    width: 40%;
}
div.two {
    background-color: green;
    width: 120%;
}

HTML

<body>
    <div class="page">
        <div class="one">One</div>
        <div class="two">Two</div>
    </div>
</body>

Answer №1

If you're looking for a solution to your issue, consider utilizing the CSS property display:inline-block;.

HTML

body {
    background: gray;
}
div.page {
    color: white;
    background: black;
    margin: auto;
    padding: 1em;
    display:inline-block;
}
div.one {
    background-color: red;
    width: 10em;
    display:inline-block;
}
div.two {
    background-color: green;
    width: 40em;
    display:inline-block;
}
<div class="page">
  <div class="one">One</div>
  <div class="two">Two</div>
</div>

Answer №2

If you try to use % for sizing and expect the box to overflow, it will never end. 100% turns into 120%, then 120% of 120%, and so on. This idea simply cannot work.

Your request for CSS is unclear and inconsistent.

In order for an element to expand beyond the window width, one of its parent elements must be able to handle this behavior. Typically, content overflow needs to remain visible within the html/body or parent element.

As far as I am aware, only the following display values:

display:
  1. table
  2. inline-table
  3. table-row
  4. table-cell

Allow the container to grow to match the content size.

Answer №3

The issue lies in the following code snippet:

div.two {
        background-color: green;
        width: 120%;
}

By setting the width of the child element to be 120% of its parent's width, you are essentially making it wider than the parent itself. Adjusting the width to 100% should resolve the unexpected outcome.

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 use a button to hide specific divs upon clicking?

Is there a way to use a button onclick event to hide specific divs within a parent div? I've tried using .toggleClass('.AddCSSClassHere') but I'm not sure how to apply it to multiple divs. The jQuery snippet provided only allows me to h ...

creating a div element with a height that matches its content dimensions

I'm having trouble creating a navigation bar because the outer div isn't matching the height of the unordered list inside it. I attempted using display:inline-block as well, but it didn't solve the issue. Here is the HTML: http://jsfiddle ...

creating movement in a display of data points

(I'm just starting to learn about html5, so please keep it simple) I want to make a scatterplot of some data where the points move around over time. Right now, I am using context.arc() to create the initial frame of the animation with specific coord ...

Is there a way to sort a table using a dropdown menu selection as a filter?

I am currently working on a project that involves a table with three columns, which are typically populated from a SQL database. My goal is to filter the third column based on the value selected from a dropdown menu. I came across a helpful tutorial on W3 ...

Arranging elements in HTML for Manipulating with JavaScript

I haven't started coding yet, but I'm contemplating the overall strategy. My front end is primarily composed of HTML tables, giving it an Excel-like appearance. I am considering generating default pages and then using JavaScript to dynamically m ...

Input field in a tableview

I have a ListView that features a dropdown, 4 textboxes and buttons. My goal is to only show the fourth textbox (enclosed in a span) when the dropdown value in each row of the ListView is set to 2. For instance, if there are 5 records being displayed in t ...

Implementing a dropdown menu within a Bootstrap navbar

Currently, I am working on developing a website and decided to kickstart the project by using the dashboard example provided on the Bootstrap website. However, I encountered an issue when trying to integrate a dropdown into the navbar. It seems that the dr ...

What is the process of turning a picture from a menu file into a clickable link?

I have created a menu with some images included, and I want them to be clickable links. However, currently only the text on top of the images acts as a link when hovered over. I would like the entire image to be clickable, not just the text. I believe I ...

InnerHTML failing to append HTML content

Almost at the finish line with this code! I already have a footer in place, but when the button is clicked, it smoothly slides down along with its contents. Once it's done sliding, I utilize JavaScript to swap out that footer and its contents with a n ...

Replacing a div with another div can be achieved in a single swap

My goal is to swap one div with another div upon clicking using the provided code. It functions properly for a single instance. However, the issue arises when the class appears multiple times, causing all instances to change due to sharing the same class n ...

Bootstrap container with added page border

Allow me to explain the issue to the best of my ability: I have a design that needs to be implemented using bootstrap. I prefer to use only CSS and avoid JS/jquery. The content is contained within a bootstrap container that has two columns. The left gre ...

Issue with dropshadows in Chrome: Instead of applying the shadow effect to the graphic itself, it is mistakenly being added to the container

Having trouble adding a Gaussian drop shadow to an SVG path. The shadow is not applying correctly in Chrome - instead of just on the graphic, it's being added to the container. Works fine in ff though. Any suggestions on how to make this work properly ...

Tips on removing previously selected files from a Bootstrap 4 input file

I am currently facing an issue with a form that includes an input file implemented using Bootstrap 4. The functionality of this input file depends on Javascript: when the user clicks on the "browse files" button, a window opens for file selection, and upon ...

Designing a webpage using CSS

the design I'm aiming for I am attempting to create a header layout as depicted in the image below using HTML and CSS. I have managed to place the logo in the center, but I am facing difficulties in aligning the business list to the corner. I have tri ...

(Monetate) Resolving the issue of delayed transition between the <a> element and CSS arrow in the breadcrumb menu utilizing HTML/CSS/jQuery

Recently, I've been working on implementing a breadcrumb-style checkout progress indicator on my website using a code generator created by an incredible developer named Jonas Ohlsson. This feature utilizes HTML, CSS, and jQuery and is integrated into ...

Having some trouble getting the text to float alongside an image properly in HTML and CSS

I'm having trouble positioning my text next to an image. I've tried floating it to the right and even using a flexbox, but nothing seems to be working well. Here is the code I have... <div class="tab-content"> <div clas ...

What is the method for altering the date format of a published article?

I am looking to modify the date format of a published post in WordPress. Currently, the date format is <?php the_time('m.d.y'); ?></div>, which appears as "1.20.2018". My goal is to change it to "January 20, 2018". Can anyone guide ...

Connect my php search outcomes to a different webpage

Hello, I am new to the world of php and I have written a search result function that allows users to click on items. However, I am facing an issue with making a specific search item clickable to open in another page with full details. Below is the code I h ...

I'm having an issue with Internet Explorer where the link doesn't function properly when I enclose the content within an <a> tag. Could anyone

I understand that this might not be the preferred approach, but a customer has requested live text wrapped in just one HTML tag.... Here's the challenge I'm facing: I currently have some code structured like this: <a href="google.com"> ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...