Solved

use css to adjust div container size to hug content

Posted on 2011-02-28
15
1,535 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 82

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
 
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 82

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now