Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

use css to adjust div container size to hug content

Posted on 2011-02-28
15
Medium Priority
?
1,631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 84

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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 84

Accepted Solution

by:
Dave Baldwin earned 400 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 400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

636 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