Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS fill remaining width

Tags:

css

I have this header bar.

<div id="header">         <div class="container">             <img src="img/logo.png"/>             <div id="searchBar">                 <input type="text" />             </div>             <div class="buttonsHolder">                 <div class="button orange inline" id="myAccount">                     My Account                 </div>                 <div class="button red inline" id="basket">                     Basket (2)                 </div>             </div>          </div>     </div> 

I need the searchBar to fill whatever the remaining gap is in the div. How would I do this?

Here's my CSS

#header {      background-color: #323C3E;     width:100%; }  .button {     padding:22px; }   .orange {     background-color: #FF5A0B; }  .red {     background-color: #FF0000; }  .inline {      display:inline; }  #searchBar {     background-color: #FFF2BC; } 
like image 255
TMH Avatar asked Sep 23 '13 14:09

TMH


People also ask

How do you occupy remaining width in CSS?

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 an element occupy full width?

The . header-square is missing width attribute. Therefore you should expand to 100% by doing width: calc(100% / 7); to achieve your desired result.

How do I make a div fill the whole page?

position:absolute You can also use position absolute as well as set all the viewport sides (top, right, bottom, left) to 0px will make the div take the full screen.


2 Answers

Use calc!

https://jsbin.com/wehixalome/edit?html,css,output

HTML:

<div class="left">   100 px wide!   </div><!-- Notice there isn't a space between the divs! *see edit for alternative* --><div class="right">     Fills width!   </div> 

CSS:

.left {   display: inline-block;   width: 100px;    background: red;   color: white; } .right {   display: inline-block;   width: calc(100% - 100px);    background: blue;   color: white; } 

Update: As an alternative to not having a space between the divs you can set font-size: 0 on the outer element.

like image 80
Isaiah Turner Avatar answered Oct 11 '22 01:10

Isaiah Turner


You can realize this layout using CSS table-cells.

Modify your HTML slightly as follows:

<div id="header">     <div class="container">         <div class="logoBar">             <img src="http://placehold.it/50x40" />         </div>         <div id="searchBar">             <input type="text" />         </div>         <div class="button orange" id="myAccount">My Account</div>         <div class="button red" id="basket">Basket (2)</div>     </div> </div> 

Just remove the wrapper element around the two .button elements.

Apply the following CSS:

#header {     background-color: #323C3E;     width:100%; } .container {     display: table;     width: 100%; } .logoBar, #searchBar, .button {     display: table-cell;     vertical-align: middle;     width: auto; } .logoBar img {     display: block; } #searchBar {     background-color: #FFF2BC;     width: 90%;     padding: 0 50px 0 10px; }  #searchBar input {     width: 100%; }  .button {     white-space: nowrap;     padding:22px; } 

Apply display: table to .container and give it 100% width.

For .logoBar, #searchBar, .button, apply display: table-cell.

For the #searchBar, set the width to 90%, which force all the other elements to compute a shrink-to-fit width and the search bar will expand to fill in the rest of the space.

Use text-align and vertical-align in the table cells as needed.

See demo at: http://jsfiddle.net/audetwebdesign/zWXQt/

like image 35
Marc Audet Avatar answered Oct 11 '22 02:10

Marc Audet