Solved

use css to adjust div container size to hug content

Posted on 2011-02-28
15
1,574 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 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