Search code examples
htmlcssword-wrap

How to force inline divs to stay on same line?


I'm trying to make a three-column layout. I'd like the width of the left and right columns to be only as wide as their children content. I'd like the center column to expand to fill the remaining space.

I'm trying the following (overview, jsfiddle link included below):

#colLeft {
  display: inline;
  float: left;
}
#colCenter {
  float: left;
  display: inline;
  overflow: none;
  white-space: nowrap;
}
#colRight {
  display: inline;
  float: right;
}

<div id="parent" style="width:100%">
  <div id="colLeft">left</div>
  <div id="colCenter">Some really long text in the center. Some really long text in the center.</div>
  <div id="colRight">right</div>
</div>

fiddle: http://jsfiddle.net/5kszQ/

but the center column pushes the right column below it when its content is too long. I'd like all three columns to be inline, and have the center column shrink as necessary. This is what the above is giving me:

enter image description here

instead I would like:

enter image description here

Thanks for any help


Solution

  • If you are open for some HTML changes, then this should give you exactly what you want:

    <div id="parent" style="width:100%">  
      <div id="colLeft">left</div>
      <div id="colwrap">
          <div id="colRight">right</div>
          <div id="colCenter">Some really long text in the center. Some really long text in the center.</div>  
        </div>
    </div>
    

    and css to be:

    html, body {
      margin: 0px;
      padding: 0px;
    }
    #parent {
      background-color: #eee;
      height: 48px;
    }
    #colLeft {
      background-color: #ff8b8b;
      height: 48px;
      float: left;
    }
    #colwrap{
        overflow:hidden;
        background-color: orange;      
    }
    #colCenter {
      height: 48px;  
    }
    #colRight {
      background-color: #c3d0ff;
      height: 48px;
      float: right;
    }
    

    jsFiddle: http://jsfiddle.net/gajbhiye/ZX97K/ Hope that helps.