views:

89

answers:

2

Its in all in the title.

I'm trying to get a div on the left of the page to a static width 170px; this works fine.

What I'm having issues with is getting a div next to it, that scales to fit the remaining width.

Is there a simple approach I can use here?

+4  A: 

On the right-hand div, just set a margin:

style="margin-left: 170px;"

Here's an example page, works here:

<html>
  <head>
    <title>Jquery Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
      $(function()  {
        var right = $("#right");
        $("#toggle").click(function() {
          $("#left").animate({"width": "toggle"}, {
            duration: 250,
            step: function() {
                right.css("margin-left", $(this).width());
            }
          }, function() { right.css("margin-left", $("#left").width()); });             
        });
      });
    </script>
    <style type="text/css">
      #left { width: 170px; float: left; border: solid 1px red; }
      #right { margin-left: 170px; border: solid 1px green; }
    </style>
  </head>
  <body>
    <div id="left">Test</div>
    <div id="right">Test Right</div>
    <input id="toggle" value="Open/Close" type="button" />    
  </body>
</html>
Nick Craver
what if you want to put content there?
Robert Greiner
I tried this, and it added 170px of scrolling to the right.
Nate Bross
@Robert Greiner: There is already content...the left-hand div
Nick Craver
@Nate Bross: I added an example page, they combine to form 100% width, no horizontal scrolling...is this not what you're after?
Nick Craver
+1 that worked for me.
Robert Greiner
Now, assume that I want to expand and collapse the div on the left, I would simply adjust it's "width" property and the "margin-left" of the rght div at te same time via my JS "Collapse" method?
Nate Bross
@Nate Bross: I added to the example page showing how to toggle the thing open/closed with a button. Let me know if this isn't what you're after. Using jQuery's step callback you can ensure the divs are in sync while they animate.
Nick Craver
Wow thanks. Above and beyond. I was simply asking if that was the right approach. Thanks!
Nate Bross
+1  A: 

You can define your css classes like this:

#left-div {
    margin-left: 0px;
    width: 170px;
    float: left;
}
#right-div {
    margin-left: 170px;
    margin-right: 0px;
}

This will keep the left margin of the right div next to the left div, and the right margin at the right edge of the window.

Aaron
Will this allow me to put a table inside and set its width to 100% and fill all the space of #right-div?
Nate Bross
Yes, you will be able to put a 100% width table. Set the id of the divs to "left-div" and "right-div" respectively.
Aaron