1

I need to build a HTML/CSS layout with the following layout

|Static Div|Content Div0|Content Div1|...|Content DivN|Static Div|

a scaled down version of my code is like follows:

<div id="container">
  <div id="static_div_1"></div>
    <div id="content">
      <div id="item1"></div>
      <div id="item2"></div>
       ....
      <div id="itemN"></div>
    </div>
  <div id="static_div_2"></div>
</div>

Requirements:

  1. All DIVs need to be stacked next to each other.
  2. I will need to add DIV's in the content DIV dynamically using javascript
  3. The static DIV's need to maintain their position at the beginning and end of the content.
  4. Content DIV should scroll horizontaly

I am struggling with the following issues:

  1. After a point the content div starts wrapping the inserted DIV and another row of DIVs start to be rendered.

Edit: Its not something tabular in nature like plain data. Assume each div is like a form in itself. Something looking like a W2. I just need scrolling in the content DIV. To make the matters worse I am not really a GUI guy.. :(

Any help will be greatly appreciated.

5
  • 1
    Any specific reasons why you are not using tables? Commented Dec 14, 2010 at 22:31
  • Why are you not expecting this behavior? Once the divs collectively reach a certain width you will experience a wrapping. Are you suggesting the wrap is coming too early or at an unexpected point? Commented Dec 14, 2010 at 22:38
  • 1
    I agree with @AniDev - Stacking Divs side by side is an extremely complicated and tricky maneuver. I struggled with it myself for quite a while not too long ago, and eventually gave up. Your example sounds more like a data display, which is what tables are traditionally used for - even in a CSS-driven page. Commented Dec 14, 2010 at 22:38
  • Updated the question and open to more clarification if necessary. Thanks guys! Commented Dec 14, 2010 at 22:49
  • Its a case of "Business users thinking everything can be like Excel" grrr Commented Dec 14, 2010 at 22:51

1 Answer 1

3

The CSS code you need is this:

#static_div_1, #static_div_2 {
    float: left;
    width: 200px;
  }
  #content {
    float: left;
    overflow-x: scroll;
    white-space: nowrap;
    width: 600px;
  }
  #content .item {
    display: inline-block;
    *display: inline;  /* Target IE6/7 only */
    width: 150px;
    zoom: 1;
  }

Note I have added a class "item" to each of the items inside the #content div to style them. The keys in this layout are:

  1. Make "float: left" the three parts, #static_div_1, #static_div_2 and the #content divs (and optionally set a fixed width to them).
  2. The "overflow-x: scroll" style on the #content div.
  3. The "white-space: nowrap" to the #content div.
  4. Set "display: inline-block" to each item inside the #content div.

You'll notice there is a space between each of the items. This is the newline space, so to avoid it, there must be no newline or space between those item divs.

I hope this results helpful to you. Best regards.

UPDATE: It now works on IE6/7. For correct setting of "display: inline-block" on these browsers notice the addition of the lines:

...
#content .item {
    ...
    *display: inline;  /* Target IE6/7 only */
    ...
    zoom: 1;
  }
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for the example - but does this work in ie? I am not sure how will ie handle display: inline-block and overflow-x?
@skajfes I added a hack to fix "display: inline-block" on IE6/7 (IE8 works fine without it).
@Perpetualcoder: Was this the correct answer? Can you tick the answer as correct?
In IE I am still getting the wrapping behavior for the DIV once it starts overflowing...I am mainly targetting IE7
Eventhough this is not the perfect answer but got me almost close before IE7 started messing with me...thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.