tags:

views:

522

answers:

3

I was wondering How To make a Form and Div have a max width of child elements. Eg in this example both the form and the outerDiv stretch the full width of the page. I would like the form and outerDiv to have a width of 200px.

This becomes a problem when I have this page in an iframe, because the width of the page is larger than the iframe I get a horizontal scroll bar.

<body>
<form name="myForm" method="post" action="#"  >
 <div id="outerDiv" >
  <div style="width: 200px">
  Both the form and outer div stretch 100%. I am wondering how I
  would get them to wrap tightly around the inner div.
  </div>
 </div>
</form>
</body>

Thanks for your time and help.

+2  A: 

I believe the "proper" way is to make them inline elements:

style="display: inline;"

divs are block elements and fill their container, spans are inline elements and shrink to fit their contents.

You can either use spans as containers or just add the above style to your divs.

Good luck!

Michael La Voie
Just an additional note that under XHTML standards, a span containing other elements is invalid, so the CSS method is better, though I'm not sure why not just set the outer div and form both to 200px width as well.
Zurahn
As per Ole J. Helgesen, inline-block might be better, however support for that property is poor in certain older browsers.
Joshua Carmody
+3  A: 

To make a block display like an inline element you can use

display: inline-block;
Ole J. Helgesen
+1  A: 

To prevent scrollbars for #outerDiv you could set a max-width and overflow properties:

#outerDiv {
  max-width: 300px;
  overflow: hidden;
}

In this way, you assure no scroll bars, and I'm assuming the width of the iframe is never wider than 300 pixels. Also be sure to set frameborder="0" on the iframe and check the margins on the body of the included html.

artlung
Good answer! This helped me out a lot on a similar problem!
MikeN