Solved

use css to adjust div container size to hug content

Posted on 2011-02-28
15
1,556 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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