javascript - div width adjusting to 100% if another div is hidden -
how set width of #leftdiv 100% when #rightdiv hidden, , when button clicked, both div should next each other.
i got both div's next each other on on button click, wanted expand #leftdiv 100% when #rightdiv 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>`
it works without manipulating width via javascript if format them table-cell
s:
function togglesidebar() { var div = document.getelementbyid('rightdiv'); if (div.style.display !== 'none') { div.style.display = 'none'; } else { div.style.display = 'table-cell'; } };
#leftdiv { border: solid medium thick; width: auto; display: table-cell; background-color: #ffc; } #rightdiv { width: 50%; border: solid medium thick; background-color: #ffa; display: none; } #main-content { display:table; width: 100%; }
<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>`
Comments
Post a Comment