javascript - Set width to widest element while still having floats -
so have sidebar container buttons, on first line there element floats left , element floats right. below them buttons various lengths, 1 button per line. in container. how can make buttons , container length of widest button? floating element right makes container wide sidebar, should max width, not default one. can accept answer in javascript css.
here i've done far: https://jsfiddle.net/ke2b7wq1/
css:
#sidebar{ width: 250px; height: 100%; border: 1px solid #000; } #left-float{ padding: 0 5px; background: #f00; float: left; } #right-float{ padding: 0 5px; background: #f00; float: right; } .button{ text-align: center; background: #bbb; clear: both; float: left; margin-top: 7px; width: 100%; } .pusher{ clear: both; height: 0; }
html:
<div id="sidebar"> <div id="container"> <div id="left-float">hello</div> <div id="right-float">0</div> <div class="button">lorem ipsum</div> <div class="button">dolor sit amet</div> <div class="button">foo bar foo bar</div> <div class="button">lorizzle ipizzle</div> <div class="button">final button</div> <div class="pusher"></div> </div> </div>
set #container
display:inline-block
or float:left
(don't forget clear floats).
floated
* { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } #sidebar { width: 250px; height: 100%; border: 1px solid #000; overflow: hidden; } #container { float: left; } #left-float { padding: 0 5px; background: #f00; float: left; } #right-float { padding: 0 5px; background: #f00; float: right; } .button { text-align: center; background: #bbb; clear: both; float: left; margin-top: 7px; width: 100%; padding: 4px; } .pusher { clear: both; height: 0; }
<div id="sidebar"> <div id="container"> <div id="left-float">hello</div> <div id="right-float">0</div> <div class="button">lorem ipsum</div> <div class="button">dolor sit amet</div> <div class="button">foo bar foo bar</div> <div class="button">lorizzle ipizzle</div> <div class="button">final button</div> <div class="pusher"></div> </div> </div>
inline-block
* { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } #sidebar{ width: 250px; height: 100%; border: 1px solid #000; } #container { display:inline-block; } #left-float{ padding: 0 5px; background: #f00; float: left; } #right-float{ padding: 0 5px; background: #f00; float: right; } .button{ text-align: center; background: #bbb; clear: both; float: left; margin-top: 7px; width: 100%; padding:4px; } .pusher{ clear: both; height: 0; }
<div id="sidebar"> <div id="container"> <div id="left-float">hello</div> <div id="right-float">0</div> <div class="button">lorem ipsum</div> <div class="button">dolor sit amet</div> <div class="button">foo bar foo bar</div> <div class="button">lorizzle ipizzle</div> <div class="button">final button</div> <div class="pusher"></div> </div> </div>
Comments
Post a Comment