An element (div) enclosed by "border"

Hi everyone,
My “container” div contains various changeable elements such as lists, forms and more within it.
That “container” has a “border” property to confine it’s content by a surrounding frame.
I wouldn’t like this “frame” to be static, meaning: it shouldn’t have a rigid width such as: “100px”.
I want its’ width to change in accordance with its’ content. If the content of that div is 10 px, border’s
width should be 10 px etc…
I tried: “width: auto; margin: auto” and recieved the whole display’s width as border’s width.
Can anyone give me a suggestion of how to create an “elastic” border attribute?
Thanks

Hi,

This may be wide of the mark as its hard to visualise what you want butiof you set the container to display:inline-block then it will only be as wide as its contents. Of course if contents are 100% wide then the parent gets stretched to 100% wide also.,


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.wrap {
	text-align:center;/*centers inline-block elements*/
}
.container {
	text-align:left;
	border:1px solid #000;
	padding:10px;
	display:inline-block;
 *display:inline;/* ie6 and 7 fix for inline-block - remove if not supported */
 *zoom:1.0;/* ie6 and 7 fix for inline-block - remove if not supported */
}
</style>
</head>

<body>
<div class="wrap">
		<div class="container">
				<p>The box will be centred with</p>
				<p>The box will be centred with</p>
				<p>The box will be centred with the longest item</p>
				<p>The box will be centred with</p>
				<p>The box will be centred with</p>
		</div>
</div>
</body>
</html>


Expounding on what Paul has said:

If an element is block-level ( DIV, P, LI, etc) width: auto; will automatically make it 100% the width of it’s container.
If an element is inline-level ( A, SPAN, etc) it will ALWAYS be the width of it’s content, as inline-level element do honor dimension declarations.

Inline-block (display:inline-block), is somewhat of a hybrid in that, by default, an inline-block element will be the width of it’s content but you can still give it explicit dimensions.

The one exception to this is if you FLOAT an element, then you MUST give it explicit dimensions or it will ‘shrink-wrap’ to the width of its content.

Again, as Paul said, IF the contents COINCIDENTALLY happen to be the same width or longer than the ape rent element then the child element will APPEAR to be 100%. IN those cases remember that you can also limit the width with min/max width

hope that helps

Thanks a lot Paul,
My code is scattered in many files and I hoped I would get an answer without showing it as I did! Your answer is exactly what I needed and I’m grateful to you!

Thanks a lot dresden_phoenix !
For a more insight enlightment about something I didn’t know at all.