/    Sign up×
Community /Pin to ProfileBookmark

Make div auto-fit contents with inside left & right alignment

Hi all,
check the following code:
<style>div {border:1px solid black; margin:3px;white-space:nowrap;}</style>
<div style=”float:left”>
<div>1<div style=”float:left;margin:0px;border:none”>2</div></div>
<div>Text that the outer div should adjust to</div>
</div>

It does not display as I want it to. What I want is:
– the outer div should auto-size to exactly fit the div with the long text
– the div with the long text should also size-down to exactly fit the text
– the “1” should be displayed in the upper box aligned to the left, the “2” should be displayed in the upper box aligned to the right

I cannot use and fixed widths because the texts are dynamic.
Can anybody help??

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@Frank62Aug 27.2011 — This works in IE8+ (remove the compatability tag) and the standards-compliant browsers, but not in IE6/7:

[code=html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<style type="text/css">
div {
border: 1px solid black;
margin: 3px;
white-space: nowrap;
}
#container {
float: left; /* floats also make divs shrink-wrap */
}
p.left {
float: left;
margin: 0;
}
p.right {
float: right;
margin: 0;
}
.clearingDiv {
clear: both;
margin: 0;
border: 0;
}
</style>
</head>
<body>
<div id="container">
<div id="upperDiv">
<p class="left">1</p>
<p class="right">2</p>
<div class="clearingDiv"></div>
</div>
<div id="lowerDiv">Text that the outer div should adjust to</div>
</div>
</body>
</html>
[/code]


I tried getting it to work in IE7 as well, but didn't succeed. What does work in all browsers is a simple table:

[code=html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table, td {
border: 1px solid black;
}
p.left {
float: left;
margin: 0;
}
p.right {
float: right;
margin: 0;
}
.clearingDiv {
clear: both;
}
</style>
</head>
<body>
<table cellpadding="0" cellspacing="3" summary="cute table">
<tr>
<td>
<p class="left">1</p>
<p class="right">2</p>
<div class="clearingDiv"></div>
</td>
</tr>
<tr>
<td>Text that the outer div should adjust to</td>
</tr>
</table>
</body>
</html>
[/code]


Validates just as well, and since the data are dynamically generated and can therefore be re-used with another make-up very easily, there is no reason not to use a table.
Copy linkTweet thisAlerts:
@baddaauthorAug 28.2011 — Hi Frank,

thanks for your answer :-)

I think Ill stick to tables in this case ... ?
×

Success!

Help @badda spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...