Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic width container div with fixed height and horizontal scroll?

I'm trying to get a container div that will scroll horizontally beyond the viewport. Sort of like a fullscreen filmstrip with #a at the beginning and #c at the end. Both #a and #c are fixed width divs, and #b has dynamic width image content. The problem I'm having is getting #container to change its width dynamically to accommodate #b. Setting #container width to auto or 100% just uses the viewport width.

What I'm after:

[-  viewport width  -]
--#container--------------------------------------- 
|   --------------------------------------------  | 
|   | #a  |      #b...                    | #c |  |  
|   --------------------------------------------  |
--------------------------------------------------- 

My markup:

#container {
    height:400px;
    }
#a {
    float:left;
    width:200px;
    height:400px;
    }
#b {
    float:left;
    width:auto;
    height:400px;
    }
#c {
    float:left;
    width:200px;
    height:400px;
    }


<div id="container">
    <div id="a">fixed width content</div>
    <div id="b">dynamic width content</div>
    <div id="c">fixed width content</div>
</div>

Edit: I can do this with a table, but would like to avoid that if possible.

Edit 2: Here's a working version using tables:

#container {
  height:400px;
  background:#fff;
  }
#a {
  width:200px;
  height:400px;
  background:#ccc;
  }
#b {
  height:400px;
  background:yellow;
  white-space: nowrap;
  }
#c {
  width:200px;
  height:400px;
  background:#ccc;
  }

<table cellpadding="0" cellspacing="0">
  <tr>
    <td id="a">
      a
    </td>
    <td id="b">
      <img src="..." />
      <img src="..." />
      <img src="..." />                     
    </td>
    <td id="c">
      b
    </td>       
  </tr>
</table>
like image 333
Jeff Avatar asked Oct 27 '11 17:10

Jeff


1 Answers

<div id="container">
  <div id="a">fixed width content</div>
  <div id="b">
    <img src="http://karenrothart.com/yahoo_site_admin/assets/images/Landscape_Panorama.13130817_large.jpg" />dynamic width content dynamic width content dynamic width content dynamic width content
  </div>
  <div id="c">fixed width content</div>
</div>

Here is good css:

div {
  height: 400px;
}

#container {
  position: relative; /* needed for absolutely positioning #a and #c */
  padding: 0 200px; /* will offset for width of #a and #c; and center #b */
  border: green 3px dotted; /* just for the show */
  float: left; /* To dynamicaly change width according to children */
}

#a, #b {
  position: absolute; /* causes #a and #b to not respect padding of #container and also gives it its place */
  top: 0;
  width:200px;
  left: 0;
}

#c {
  right: 0;
}

Nice and shiny example: http://jsfiddle.net/KefJ2/

If you need more than one image than add this:

#b {
  white-space: nowrap; /* causes all direct child elements to be in one line */
}

Example with more images: http://jsfiddle.net/KefJ2/1/ Obviously you will have to play around with text and images layout in #b but that should be easy enough :)

like image 184
Ivan Ivanic Avatar answered Oct 21 '22 16:10

Ivan Ivanic