tags:

views:

74

answers:

5

I need to create a one-lined 3-column layout. The left and right columns should each display one word without truncation (they should expand and contract to fit the word). The center column should display a potentially lengthy string, truncated to fit between the two columns.

Here's a bit of HTML to convey the idea:

<div class="container">
  <div class="left">Left</div>
  <div class="center">Center center center center center center center</div>
  <div class="right">Right</div>
</div>

And some corresponding CSS:

.container {
  whitespace: nowrap;
}

.left {
  display: inline-block;
}

.center {
  display: inline-block;
  overflow: hidden;
}

.right {
  display: inline-block;
}

The next step is to somehow set the center element to automatically expand or contract to fill the space between the left and right elements.

Something like center.width = container.width - left.width - right.width

Any ideas? Thanks


EDIT: Solved with a few minor changes to ianhirschfeld's response.

HTML:

<div class="container">
  <div class="left">Left</div>  
  <div class="right">RightRightRight</div>  
  <div class="center">Center center center center center center center</div>
</div> 

CSS:

.container {
  white-space: nowrap;
  overflow: hidden;
}

.left {
  float: left;
}

.center {
  overflow: hidden;
}

.right {
  float: right;
}
A: 

I am sure some CSS guru knows how to do this without the use of javascript, but here is a sample page that should work.

http://jsbin.com/ivuyi

To show you how it will actually function with more data.

http://jsbin.com/ilari3/2

sberry2A
Was looking for a more dynamic, HTML/CSS solution, but thanks!
Eric Freese
A: 

Try doing a search for the "Holy Grail" layout. It is kind of a classic CSS problem. You should be able to modify existing solutions to truncate text using the overflow property. Here's one such link:

http://matthewjamestaylor.com/blog/ultimate-3-column-holy-grail-pixels.htm

Brad Gignac
I don't *think* that will work for the OP. The Holy Grail layout does not allow for resizing of the columns based on their content. It has hard coded values for width on the columns which seems to make it unsuitable for the problem.
sberry2A
Yep, the side columns need to fit their content
Eric Freese
+1  A: 

Depending on how you are exactly implementing this, here is something you could try:

  • Set the height on the container class
  • Set float:left on .left and float:right on .right
  • Place your .left and .right divs within your center div before its content

.container {
height: 30px;
overflow: hidden;
}
.left {
display: inline-block;
background: #b9ff67;
float: left;
}
.center {
width: 100%;
display: inline-block;
overfow: hidden;
background: #9ac0ff;
}
.right {
display: inline-block;
background: #ffc8c8;
float: right;
}

<div class="container">  
<div class="center">  
    <div class="left">Left</div>  
    <div class="right">RightRightRight</div>  
    Center center center center center center center  
</div>  

It's very, very close, but the text in the center div overwrites the text in the right div.
Eric Freese
A: 

One line, three columns, with adjusting width.........

Why not make it a table with three cells instead of using floated divs, then give the cells percentage width?

You should be able to get reasonably close to content fit with the side columns if you play around with the percentages.... for example 1% | 98% | 1% and going from there.

Tom
Tried that, but for my particular case, the length of the words in the left and right cells vary enough that the percentage width solution is inefficient in most cases.
Eric Freese
+1  A: 

If I understand correctly, what you are looking for is this line:

.center { overflow: hidden; }

This HTML is fine:

<div class="container">
  <div class="left">Left</div>  
  <div class="right">RightRightRight</div>  
  <div class="center">Center center center center center center center</div>
</div> 

With overflow: alt text

Without overflow: alt text

This resizes properly with the container and the window.

GeReV
Yes, apparently I missed that in ianhirschfeld's solution. Once I added it in, it successfully chopped off the extra text in the center element. Thanks!
Eric Freese
Also was able to remove the `position` and `z-index` statements.
Eric Freese