CSS: setting width of DIV with overflow:auto set

I have a DIV which contains a large table of data, too wide for most users' monitors. I had it styled like this:


That way, it fit within the screen design, and if its too wide, the data just scrolls inside the DIV. But now, the client wants the screen to stretch to use as much of the user's monitor width as possible. So I recoded the screen design so that the page template uses all of the width.

I tried changing my CSS on the DIV to this:


However, now it forces the DIV to as wide as it needs to go to include all of the table data. The entire screen scrolls left-to-right now, the DIV itself is wider than the monitor. I also tried this, but the same thing happens


What I want, is for the DIV to stretch as far left-to-right as it can go without making the entire screen scroll (DIV same width as the user's monitor), and then the contents within that DIV should scroll (overflow:auto;) if the table is too wide to fit within the DIV. Is this possible? I don't see how I can set a specific width because I don't know the size of the user's monitor, how their document window is positioned, etc. And 100% seems to just make it stretch as far as it needs to go.

Thank you!
Brad BansnerWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

How about
#membertable {
  position: absolute;
  left: 0;  /* or 5px or 10px give yourself some padding */
  right: 0; /* same here */
  overflow: auto;

Open in new window

This assumes that the div is inside a container that has been set to position: relative.  Otherwise, it will anchor itself to the first parent element whose position is not static (or fixed).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brad BansnerWeb DeveloperAuthor Commented:
That is getting me closer, I think. Here is a test version of my page:


Relevant code attached.

For some reason, the absolute-positioned DIV is breaking out of the main content area. I have membertablewrapper with a red border, it appears that membertable (green border) is not staying inside it for some reason, even though I have a "clear:both;" afterwards.

The page template is built with a table, but its width is not set. For some reason, the DIV still isn't forcing that wider.

Note that if I remove membertablewrapper altogether, the absolute DIV goes all the way to the top of the screen and it breaks out of the page template table (no white background), on top of everything else... BUT... the width is exactly the way I wanted, to the available left-to-right space on the screen.

So if I can get that to work, but keep it within the page template table, this will be solved.

I don't know if you have any other suggestions, otherwise I'll just need to keep muddling through it.

#membertablewrapper{position:relative;width:100%;border:1px solid #ff0000;}
#membertable{position:absolute;top:0px;left:0px;right:0px;overflow:auto;border:1px solid #00ff00;}

<div id="membertablewrapper">
<div id="membertable">

<table>data here</table>

<div class="clear"></div>

Open in new window

Brad BansnerWeb DeveloperAuthor Commented:
Thanks for your advice, I was able to cheat a little and get this to work.
Brad BansnerWeb DeveloperAuthor Commented:
And, I love Frank Zappa!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.