Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div width adjusting to 100% if another div is hidden

How do I set the width of #leftdiv to 100% when #rightdiv is hidden, and when the button is clicked, both <div>s should be next to each other.

I already got both <div>s next to each other on the button click, but I wanted to expand #leftdiv to 100% when #rightdiv is hidden.

function toggleSideBar() {		
    var div = document.getElementById('rightdiv');
    if (div.style.display !== 'none') {
        div.style.display = 'none';
    } else {
        div.style.display = 'block';
    }		
};
#leftdiv
{
   border: solid medium thick;
   float: left;
   display: inline-block;
   background-color: #ffc;
   /*border: 1px solid red;*/
}

#rightdiv
{
   width: 50%;
   border: solid medium thick;
   background-color: #ffa;
   display: none;
   float:right;
}
<input type="button" id="btn" value="toggle" onclick="toggleSideBar()" />
    <div id="main-content">
        <div id="leftdiv">selectable</div>
        <div id="rightdiv">right panel</div>
    </div>`
like image 217
Devi Avatar asked May 15 '15 00:05

Devi


People also ask

How do I make div width auto adjust?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

Is a div 100% width by default?

So, by nature, it doesn't matter how much content the element contains because its width is always 100%, that is, until we alter it. Think of elements like <p> , <article> , <main> , <div> , and so many more.

How do I force a div to full width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do you make a div not occupy full width?

Answer: Use the CSS display Property You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).


1 Answers

I would just use flex box for this:

HTML

<input type="button" id="btn" value="toggle" onclick="toggleSideBar()" />
<div id="main-content">
  <div id="leftdiv">selectable</div>
  <div id="rightdiv">right panel</div>
</div>

CSS

#main-content {
  display: flex;
  flex-flow: row nowrap;
}

#leftdiv {
  background-color: #ffc;
  flex: 1 1 auto;
  border: 1px solid red;
}

#rightdiv {
  flex: 1 1 auto;
  background-color: #ffa;
  display: none;
  border: 1px solid green;
}

JS

function toggleSideBar() {  
    var div = document.getElementById('rightdiv');

    if (div.style.display !== 'none') {
        div.style.display = 'none';
    } else {
        div.style.display = 'block';
    }
};

The main reason your example did not work as you wanted is the use of float. Floating causes an element to only occupy the space needed by its content. Using float, you have to use width:100% for the left container to fix this. To make it switch dynamically from 50% to 100% width it would also be necessary to switch the style between those two in your toggle function.

With flexbox all of this is no longer necessary as its children get ordered automatically by the direction you want, in this case as a row. Setting flex: 1 1 auto; for both containers makes them grow and shrink equally to occupy all the space they can get. (flex: 1 0 auto; would also work here, as the shrinking behavior does not make any difference in this example.)

like image 168
Artur Noetzel Avatar answered Sep 24 '22 20:09

Artur Noetzel