Tips for expanding a flex-grow element to fill the area of a concealed flex item

I am looking to create a window that can be divided into two or three areas based on the state of a checkbox. When the box is checked, I want the second area to be hidden and the first area to expand to fill the additional space.

My layout works perfectly for larger screens (>640px): checked box large screens and unchecked box large screens

However, I am facing issues with smaller screens where the second area is positioned between areas 1 and 3: checked box small screens

When the box is unchecked, area 1 does not expand into the available space left by the hidden area 2. Instead, area 3 moves up and centers itself in the now vacant space (grey background): unchecked box small screens

I believe I understand the reason behind this behavior but have yet to find a solution.

Initially, I tried changing the CSS properties using `flex-direction: row`, but this caused the elements to align horizontally instead of vertically. Even attempting to use `flex-wrap: wrap` did not yield the desired result.

The extra grey space above and below area 3 seems to be related to the height setting of the hidden area 2. Although setting it to `display:none` should remove the height from consideration, this was not effective. To address this, I attempted using JavaScript to set the height of the hidden area to 0% when the box is unchecked and the screen size is below a certain threshold. However, even after running the script, the appearance remained unchanged. Additionally, adjusting the height directly through CSS did not resolve the issue as the grey wrapper area persisted.

Below is my attempt at creating a reproducible example, though I am unsure if it provides sufficient detail:

function visCheck (){
if (document.getElementById("hint").style.display=="block"){ document.getElementById("hint").style.display="none";
}
else if (document.getElementById("hint").style.display=="none"){
document.getElementById("hint").style.display="block";
}
else{   document.getElementById("hint").style.display="block";
}
}
*{
  box-sizing: border-box;
  }
body{
    margin: 0;
    height: 100vh;
    width: 100%;
}
main{
    height: 100%;
    box-sizing: border-box;
}
.maingrid-wrapper{
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    position: relative;
    display: flex;
    width: 100%;
    height: 100%;
}
.maingrid-content{
/*   border: red solid; */
}
.image{
    flex:2 1 60%;
    flex-grow:2;
    height: 90%;
    background-color: lightblue;
}
.sidehint{
    background-color: lightgreen;
    flex:1 1 40%;
    height: 90%;
}

.footer-wrapper{
    background-color: lightpink;
    height:10%;
    width: 100%;
    display: flex;
    flex-direction: row;
}

@media all and (max-width: 640px){
    .maingrid-wrapper{
        box-sizing: border-box;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        position: relative;
        display: flex;
        width: 100%;
        height: 100%;
      background-color: lightgrey;
    }
    .maingrid-content{
/*         border: red solid; */
        box-sizing: border-box;
    }
    .image{
      height: 70%;
        flex:1 1 100%;
        flex-grow: 2;
        background-color:lightblue;
    }
    .sidehint{
        height: 20%;
        background-color: lightgreen;
    }
    
    .footer-wrapper{
        height:10%;
        width: 100%;
        display: flex;
        background-color: lightpink;
    }
}
<body>
  <main>
    <div class="maingrid-wrapper">
      <div class="image maingrid-content">A</div>
      <div class="sidehint maingrid-content"
           id="hint" style="display:none">B</div>
      <div class="footer-wrapper 
                  maingrid-content">
        <input class="bulb-input" id="hint-toggle"
               type="checkbox"
               onchange="visCheck()">C</div>
    </div>
  </main>
</body>

Answer №1

After optimizing your CSS, I removed redundant code, default values, and unnecessary declarations.

Your CSS would greatly benefit from adopting a "mobile first" approach, utilizing media queries to enhance styling as the viewport expands rather than using them to override styles for smaller viewports.

function visCheck() {

  if (document.getElementById("hint").style.display=="block") {
    document.getElementById("hint").style.display="none";
  }
  else if (document.getElementById("hint").style.display=="none") {
    document.getElementById("hint").style.display="block";
  }
  else {
    document.getElementById("hint").style.display="block";
  }
}
* {
  box-sizing: border-box;
  }
  
body {
  margin: 0;
  height: 100vh;
  }
  
main{
  height: 100%;
  }

.maingrid-wrapper{
  display: flex;
  flex-wrap: wrap;
  height: 100%;
  }

.maingrid-content{
  /* border: red solid; */
  }

.image{
  flex: 2;
  height: 90%;
  background-color: lightblue;
  }

.sidehint{
  flex: 1;
  background-color: lightgreen;
  }

.footer-wrapper{
  background-color: lightpink;
  height: 10%;
  width: 100%;
  }

@media all and (max-width: 640px){

  .maingrid-wrapper{
    flex-direction: column;
    background-color: lightgrey;
    }
    
}
<body>
  <main>
    <div class="maingrid-wrapper">
      <div class="image maingrid-content">A</div>
      <div class="sidehint maingrid-content" id="hint" style="display:none">B</div>
      <div class="footer-wrapper maingrid-content">
        <input class="bulb-input" id="hint-toggle" type="checkbox" onchange="visCheck()">C</div>
    </div>
  </main>
</body>

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

Attempting to insert visuals into a product catalog within a table

As I work on my final school project, a functioning webshop, I've been making good progress. However, I've hit a roadblock when it comes to adding images to a table in PHP. In my setup, I have two PHP files that are interconnected: products.php h ...

Is there a way to prevent HTML code from being executed when sharing it as text? I want to be able to share code without it being activated as HTML on the page

Seems like I have a straightforward issue related to HTML tags. I am looking to post some code snippets on my blog, specifically the <h1> heading</h1>. I want visitors to view and not just the bolded text of heading. Do I need to incorporate J ...

Create concise information in a single line with Twig

So, I basically need to display the following information inline on my website: Just like this: Family: Open Sans - Variant: Regular Italic Size: 15px - Line Height: 25px - Paragraph: 15px Color: grey_3 This is the code snippet: {% include "../compone ...

What is the best way to maintain a div centered when the browser window shrinks smaller than the div's dimensions?

Is there a way to keep a div centered and create white space around it when the browser window is resized? I want this effect to happen whether the browser window gets larger or smaller. Currently, when the window size decreases, the left side of the div l ...

Modify the td attributes while retaining the extracted data values from the website

I'm currently utilizing this code to extract data from another website: $searchURL = "http://www.anotherwebsite.com"; $html = file_get_contents($searchURL); $patternform = '/(<tbody.*<\/tbody>)/sm'; preg_match_all($patternfor ...

JavaScript not redirecting to HTML page as expected

I have developed a basic login page that makes an Ajax request to the backend. However, I am experiencing difficulties with redirecting the page upon successful login. The redirect function only seems to work 1 in 15 times, and I'm unsure of the reaso ...

What is the best way to show an image on the screen once a submit button is clicked?

I have a hidden loader-bar gif that I want to display when the user submits a form. Here is the code: <p class="loadingImg" style="display:none;"><img src="/design/styles/_shared/classic-loader.gif" alt="" /></p> Is there a way to ...

Attempting to align two blocks side by side

As I work on developing my website, I encountered an issue with positioning div tags. I set up a side-navigation div and a body div within a site that is 1500px wide and 1000px tall, with the side-navigation at 300px and the body at 1200px in width. To my ...

Can someone please help me figure out why the "setInterval" function in my script isn't functioning as expected?

I've been experimenting with controlling the refresh rate of drawn objects in a canvas using JavaScript. Even after going through the tutorials and examples on w3.school, I'm still unsure why the "setInterval" function is not executing the "gener ...

What is the best way to align a button with the edge of an image?

How can I use CSS to position a button on the edge of an image? https://i.stack.imgur.com/FEFDC.png Here is my code: <div> <button class="" aria-label="Eat cake">Btn</button> <img class="pull-left" src="style.png" style="widt ...

Achieve hidden and visible elements using CSS without redundancy

Is there a way to hide a div on specific devices and show it on others without resorting to duplicating the code or using CSS hacks? This is my current approach: <div class="container"> <div class="row"> <div class="col-md-8 d-none d- ...

Node/Angular application facing a breach in content security

After working on a website built with node/express/angular/javascript, I returned to the project months later only to find that I couldn't display any page of the application when running it locally on node js. The error message "cannot get "{webpage} ...

When using Material UI TextField with input type "date", the event.target.value does not seem to be accessible

I am currently working with Material UI and React Grid from DevExtreme to build a table that includes an input field of type date. However, when I attempt to enter the date, it does not register the change in value until I reach the year field, at which po ...

Designing an image transformation page by segmenting the image into fragments

Does anyone have insight into the creation process of websites like this one? Are there any plugins or tools that can assist in building something similar? ...

Place background image in the center with a background color overlay

After browsing through this post on Stack Overflow, I managed to stretch my background image using CSS with the background-size: cover; property. However, I realized that this solution doesn't completely meet my needs. I'm dealing with an image ...

Reacting to the dynamic removal of spaces

Is there a way to dynamically remove the space between removed chips and older chips in Material-UI when deleting one of them? <div className="row contactsContainer"> {contacts.map((contac ...

Using HTML and CSS to stack a DIV on top of another using z-index

I have 3 main layers on my website: 1) The main view with elements inside (#views in jsbin) - BOTTOM LAYER 2) An overlay (with a white background opacity of .8 #overlay in jsbin) - MIDDLE LAYER 3) A context menu (#contextmenu in jsbin) - TOP LAYER Wh ...

css readonly input textbox not changing background color when set

I've come across an old Classic ASP form that requires additional functionality, and I'm currently using IE11. To address this, I decided to include a doctype like so (even though I'm unsure of its necessity): <!DOCTYPE html> In my C ...

React Native ScrollView with a custom footer that adjusts its size based on the content inside (

I've implemented the code below to ensure that the blue area in image 1 remains non-scrollable when there is sufficient space, but becomes scrollable when space is limited. However, I'm facing an issue where the ScrollView component is adding ext ...

Should data be stored in HTML5 using data-* attributes?

I have encountered a scenario like this: The screen contains numerous 'Rocks', each with attributes such as weight, points, and velocity. When a rock is clicked, its attributes are displayed. Currently, I have stored all the rocks' attribu ...