Solved

use css to adjust div container size to hug content

Posted on 2011-02-28
15
1,565 Views
Last Modified: 2012-06-27
is there a way using css that can get the container div to be just the right size to contain all its content divs? I have one container div with 3 member divs laid out horizontally inside.
0
Comment
Question by:bhomass
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35002125
CSS only sets things, it doesn't read anything or adjust except for things like percentages which the browser calculates.  You can use javascript to read the style info for elements and calculate it yourself and change the width.
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 35002540
A DIV will expand to fit all of the content inside of it.  However, if the content inside is floated, you will need to put a "clearing DIV" at the end of your content... or,  you can use this CSS on your container DIV:

overflow: hidden;

The above CSS will work the way you want only if you OMIT width and/or height.  For example, this won't work like you want it because the height is fixed:

height: 250px;
overflow: hidden;


If you can provide a link or some HTML code, we can help you a little more.
0
 

Author Comment

by:bhomass
ID: 35003382
It would be really great if the css method works.

I tried just overflow:hidden, just clear:left, both, but the containing div always expands to the width of its container. I even removed the height setting.
clearingdiv.jpg
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 35004138
@bhomass: please share the exact requirement with me. your requirement mentioned in the question is not clear to me.
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 35004932
Please post the HTML and the CSS that generated that screenshot.  Otherwise... we can't help you.
0
 

Author Comment

by:bhomass
ID: 35014331
<BODY>
 <div style="overflow:hidden; border-style:solid; border-width:1; border-color:black">
<div style="float:left;border-style:solid; border-width:1; border-color:blue" >left</div>
<div style="float:left;border-style:solid; border-width:1; border-color:gray" >middel</div>
<div style="float:left;border-style:solid; border-width:1; border-color:red" >right</div>
<div style="clear:left"></div>
 </div>
 </BODY>

divcontainer.jpg
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 100 total points
ID: 35014557
Try this.  'float' elements need to be inside a 'positioned' element.  And to get the 'outer container to wrap, it needs to float inside that positioned element also.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Minimum DIV</title>
</head>
<body>
<BODY>
<h1>Minimum DIV</h1>
<div style="position:relative;">
<div style="float:left;border-style:solid; border-width:1; border-color:black;min-width:10px;">
<div style="float:left;border-style:solid; border-width:1; border-color:blue" >left</div>
<div style="float:left;border-style:solid; border-width:1; border-color:gray" >middel</div>
<div style="float:left;border-style:solid; border-width:1; border-color:red" >right</div>
<div style="clear:left"></div>
</div>
</div>
</BODY>
</body>
</html>

Open in new window

0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 35017846
So you want the green box to shrink down to the width the content inside?

Then whatever DIV has the green background, just add float:left to it.

Also, floats do not need to be inside of positioned elements as suggested by DaveBaldwin.  Try this code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Shrinking DIV</title>
<style type="text/css">
#wrapper {
	float: left;
	background-color: #090;
}
</style>
</head>
<body>
<div id="wrapper">
<form>
<input type="text" id="txtUsername" />
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>

Open in new window

0
 
LVL 14

Assisted Solution

by:Designbyonyx
Designbyonyx earned 100 total points
ID: 35017887
And using your floats example with overflow:hidden
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Shrinking DIV</title>
<style type="text/css">
#wrapper {
	float: left;
	background-color: #090;
	overflow: hidden;
	padding: .5em;
}

.float {
	float: left;
	min-width: 5em;
	border: 2px solid #000;
	margin-right: .5em;
}

#float3 {
	margin-right: 0;	
}
</style>
</head>
<body>
<div id="wrapper">
    <div class="float" id="float1">left</div>
    <div class="float" id="float2">middel</div>
    <div class="float" id="float3">right</div>
</div>
</body>
</html>

Open in new window

0
 

Author Comment

by:bhomass
ID: 35019599
what is float1 and float2? same as float?
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 35020226
those are just IDs I gave to them.  I was going to style them a little differently with something like:

#float1 {
  border-color: #F00;
}
#float2 {
  border-color: #FF0;
}
#float3 {
  border-color: #F0F;
}
0
 

Author Comment

by:bhomass
ID: 35020705
right, sorry, I misread them as class.
0
 

Author Comment

by:bhomass
ID: 35023481
they both worked! thanks
0
 

Author Comment

by:bhomass
ID: 35023488
added points
0
 

Author Closing Comment

by:bhomass
ID: 35023489
works as shown,
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question