Adjust the size of the child div to fit the remaining space within the parent div

I'm dealing with a situation where there are two child divs inside a parent div. The first child div takes up 32% of the width, while the second child div takes up 68% of the width. If I were to set the display of the first child div to "none", how can I ensure that the second child div expands from its original 68% width to fill 100% of the available space? Any suggestions or tips would be greatly appreciated!

.parent {
    width: 400px;
    height: 200px;
    position: relative;
    float: left;
    display: inline-block;
}

.child1 {
    width: 32%;
    height: 100%;
    float: left;
    background-color: green;
}

.child2 {
    width: 68%;
    height: 100%;
    float: left;
    background-color: red;
}
<div class='parent'>
  <div class='child1'></div>
  <div class='child2'></div>
</div>
    

Answer №1

If I had to choose one tool to handle layout challenges, it would be the power of utilizing flexbox!

By setting flex: 0 0 32%; on the first child element, we can establish a width of 32%.

Assigning flex: 1; to the second child element ensures that it fills all available space. This means if the first child element is removed, the second child will expand to occupy the remaining space.

.parent {
  width: 400px;
  height: 200px;
  display: flex;
}

.child1 {
  flex: 0 0 32%;
  background-color: green;
}

.child2 {
  flex: 1;
  background-color: red;
}
<div class='parent'>
  <div class='child1'></div>
  <div class='child2'></div>
</div>

Answer №2

Using flex instead of float and applying display: none to one element will automatically adjust the other element for you:

document.querySelector('button').addEventListener('click', () => {
  document.querySelector('.child1').classList.toggle('hidden');
})
.parent {
    width: 400px;
    height: 200px;
    position: relative;
    display: flex;
}

.child1 {
    flex: 0 0 32%;
    background-color: green;
}

.child2 {
    flex: 1 1 auto;
    background-color: red;
}

.hidden {
  display: none;
}

button {
  margin-bottom: 1em;
}
<button>Toggle child1 visibility</button>

<div class='parent'>
  <div class='child1'></div>
  <div class='child2'></div>
</div>

Answer №3

Instead of utilizing the float property, consider using Flexbox for a more effective layout solution. For further insight, refer to this link.

You can implement this in Flexbox by incorporating the following code:

.parent {
    width: 400px;
    height: 200px;
    display: flex;
}

.child1 {
    height: 100%;
    background-color: green;
    flex:1;
}

.child2 {
   // display:none;
    height: 100%;
    flex:2;
    background-color: red;
}
<div class='parent'>
  <div class='child1'></div>
  <div class='child2'></div>
</div>
    

Answer №4

Here is a solution using the provided method.

  <div class="container">
  <div class="box1"></div>
  <div class="box2"></div>
</div>

.container {
    width: 400px;
    height: 200px;
    font-size: 20px;
    font-weight: bold;
    color: white;
}

.box1 {
    width: 32%;
    height: 100%;
    float: left;
    background-color: green;
}

.box2 {
    width: 100%;
    height: 100%;
    background-color: blue;
}

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

Inserting images into an HTML table using JavaScript in real-time

I am attempting to dynamically add an image into a table, but I am encountering difficulties. Whenever I try to insert an image into a cell, I receive the following error message: Cannot access the inner content because it is not literal. if ...

Semantic UI utilizes floating elements to position them within a

As a beginner with Semantic UI, I have been unable to find any information in the documentation on how to float a simple element without it needing to be nested within another element like a button or a segment. For example, at the bottom of a page, I hav ...

Issue with mix-blend-mode causing text to not show up on top of the gradient

I am struggling to find a way to make text easily readable over my gradient background. Despite trying to use MUI's Typography or a simple <p>, the contrast isn't great as the text appears in black. After some research, I discovered that I ...

What is the reason my bootstrap carousel isn't functioning properly?

My carousel is only displaying the first image and seems to be stuck. The navigation buttons are also not functioning. Here is the code for reference: <main> <div id="slider" class="carousel slide" data-mdb-ride="carousel"> <div cl ...

What is the best method for retrieving the entire row data based on the maximum value in a column?

function findMaxValue() { var highestValue = Math.max.apply(Math, $('.sira').map(function() { return $(this).text() })) alert(highestValue); } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"& ...

What is the best way to align text in the middle of a div using Foundation framework?

It seems like I may be either overanalyzing this or overlooking a small detail. The task at hand is to align text in the center within a div using Foundation. Here is the current code that I am working with: [1]<div class="row"> [2] <div class ...

Is it possible to integrate Processing JS on top of an HTML element?

I have currently integrated Processing JS with the HTML canvas element on a website. The goal is to have the Processing JS animation run smoothly over the existing HTML elements on the page. You can check out the site at [usandthings.com][1] to get an idea ...

Eliminate the see-through effect from a logo positioned in a fading container

I created a div with image fading functionality using JavaScript and placed a static SVG logo on top of it. The issue I'm facing is that the logo appears somewhat transparent, allowing the image in the div to show through. Despite adjusting the z-inde ...

Slide the menu off to the right

Check out these images to see my progress: Main Image. When you click on the menu, everything shifts right. I've managed to set up the push menu, toggle switch menu, and main divs. Now I need help with centering the 3 lines that are clicked within th ...

Tips for moving text within a Canvas using a button to trigger the function that displays the text in a Javascript environment

Within my HTML, there is a canvas element with the id="myCanvas". When a button is clicked, it triggers this particular function: function writeCanvas(){ var can = document.getElementById("myCanvas"); var ctx = can.getContext("2d"); va ...

When the script is placed at the end of the file, document.getElementById() is still returning null

I need assistance with running a JavaScript function that is located at the end of my .aspx file. This is the div tag in question: <div id="div_NRContainer" oninit="div_NRContainer_Init" class="panelContainer" runat="server"> Following this, with ...

The fullscreen API allows for the creation of a full-screen element containing internal elements, while also enabling the functionality

Is it possible to utilize the fullscreen API to make any element fullscreen, even if it contains multiple internal elements, while still maintaining the functionality of dropdowns and other custom elements that may be located in different areas of the page ...

Place an IconButton component next to each Material UI TableRow

I am trying to include an icon next to the material UI table row component. Similar to the hint icon shown in the screenshot below Here is my attempt so far, but it's not functioning as expected: Check out the code on CodeSandbox https://i.stack.i ...

html2canvas encountered a CORS error when trying to retrieve images from an external domain

I have been attempting to export a react component as a PDF file. Here are the steps I have followed: Converting the component into an image using html2canvas Creating a PDF document Attaching the image to the PDF The component contains images with URLs ...

Google App Engine displaying a blank screen error

I've been playing around with this concept where there's a table of 'events' on a '/search' page. When the 'GO' button of an event is clicked, it should increase the 'RSVP' count for that event and redirect ...

Preserve the location of a moveable div using jQuery

I have implemented draggable divs in my JSP page, allowing users to move them around freely. After dragging the divs, I would like to save their positions either in a cookie or database for future reference. Could you please advise on the best way to ach ...

JavaScript tutorial: Locate a specific word in a file and display the subsequent word

Seeking assistance with a task: I need to open a locally stored server file, search for a specific word, and then print the next word after finding the specified one. I have managed to write code that successfully opens the file, but it currently prints e ...

issue with JavaScript canvas

My task is to develop a Blackberry application, but I have limited knowledge in Java. Since the application requires drawing capabilities, I decided to use HTML5 and JavaScript instead. I started reading some JavaScript tutorials to prepare for this proj ...

Having trouble accessing the templateUrl for HTML in Angular version 1.6

I am currently working on a basic Angular 1.6 web application. Although I have created a state for the URL "/", it seems that Angular is unable to locate the file for some reason. The folder structure looks like this: https://i.stack.imgur.com/2p6bP.png ...

iPad screen not displaying overflow for x and y axis

I'm encountering an issue with displaying the scrollbar on a div while using an iPad device. Currently, I am utilizing: -webkit-overflow-scrolling: touch; Although this is functioning properly, my goal is to have the scrollbar visible without the ...