Guest

CSS: Inline element stretch to fill available horizontal space of container

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 2.54 KB  |  hits: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <div style="display:table;width:200px">
  2.     <div style="display:table-row">
  3.         <button style="display:table-cell">1</button>
  4.         <button style="display:table-cell">2</button>
  5.         <button style="display:table-cell">3</button>
  6.     </div>
  7. </div>
  8.        
  9. <!--[if ie lt 8]>
  10. <script><!--pseudo-code, not real js-->
  11. for (el in getElementsByTagName('button')) {
  12.     if el.style.find('display:table-cell') {
  13.         el.innerHTML = '<td><button>'+el.innerHTML+'</button></td>'
  14.     }
  15. }
  16. </script>
  17. <![endif]-->
  18.        
  19. <div style="display:table;width:500px;">
  20.     <div style="display:table-row">
  21.         <div style="display:table-cell"> <button style="width:100%">1938274</button> </div>
  22.         <div style="display:table-cell"> <button style="width:100%">2</button> </div>
  23.         <div style="display:table-cell"> <button style="width:100%">3</button> </div>
  24.     </div>
  25. </div>
  26.        
  27. <style>table button {width:100%}</style>
  28.  
  29. <table style="width:500px;">
  30.     <tr> <td><button>1938274</button> <td> <button>2</button> <td> <button>3</button> </tr>
  31. </table>
  32.        
  33. <div id="container">
  34.     <button id="one">One</button><button id="two">Two</button><button id="three">Three</button>
  35. </div>
  36.        
  37. div#container {
  38.     border: solid 1px;
  39.     width: 200px;
  40. }
  41.  
  42. div#container button {
  43.     width: 33%;
  44. }
  45.  
  46. div#container button:last-child {
  47.     width: 34%;
  48. }
  49.        
  50. div#container {
  51.     border: solid 1px;
  52.     width: 50%; /* resize your browser window to see results */
  53.  
  54.     position: relative;
  55. }
  56.  
  57. div#container button {
  58.     position: absolute;
  59.     width: 50px;
  60. }
  61.  
  62. button#one {
  63.     top: 0;
  64.     left: 0;
  65. }
  66.  
  67. button#two {
  68.     top: 0;
  69.     left: 55px;
  70. }
  71.  
  72. button#three {
  73.     width: auto !important; /* get rid of the 50px width defined earlier */
  74.     top: 0;
  75.     left: 110px;
  76.     right: 0px;
  77. }
  78.        
  79. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  80.   "http://www.w3.org/TR/html4/strict.dtd">
  81.  
  82. <html>
  83. <head>
  84. <title>test css button stretch</title>
  85. <style>
  86.  
  87. #btn_container
  88. {
  89.    width: 200px;
  90. }
  91. #btn_container button
  92. {
  93.    width: 20%;
  94. }
  95. #btn_container button.stretch
  96. {
  97.    width: 58%;
  98. }
  99. </style>
  100. </head>
  101. <body>
  102.  
  103. <div id="btn_container">
  104. <p>last button stretch...</p>
  105. <button type="button">eat</button>
  106. <button type="button">drink</button>
  107. <button class="stretch" type="button">sleep</button>
  108. <br>
  109. <p>first button stretch...</p>
  110. <button class="stretch" type="button">eat</button>
  111. <button type="button">drink</button>
  112. <button type="button">sleep</button>
  113. <br>
  114. <p>middle button stretch...</p>
  115. <button type="button">eat</button>
  116. <button class="stretch" type="button">drink</button>
  117. <button type="button">sleep</button>
  118. </div>
  119. </body>
  120. </html>