473,770 Members | 4,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Div auto height

Consider the following HTML:

<div class="links">
<img src="linkimages/logo.gif">
<h2>title</h2>
<span><a target="_blank" href="http://">http://</a></span>
<p>descriptio n of link</p>
</div>

I have the following CSS:
div.links {
width:auto;
margin:8px;
margin-bottom:13px;
border-bottom:1px solid;
}
div.links h2 {font-size:110%;text-align:left;}
div.links span a{text-align:left;font-weight:bold;}
div.links p{text-align:left;font-size:80%;}
div.links img {float:left;pad ding:0px 10px 0px 0px;}

Now in my HTML I have plenty of <div class="links"th e one under another.

If the image is big and the description short then the image extends
outside the div. How can I overcome this behavior and have a div that
extends at 100% of the necessary height? (i have already tried
height:auto with no luck)

thanks
Dec 29 '06 #1
4 41711
On 2006-12-29, Harris Kosmidhs <hk******@remov e.me.softnet.tu c.grwrote:
Consider the following HTML:

<div class="links">
<img src="linkimages/logo.gif">
<h2>title</h2>
<span><a target="_blank" href="http://">http://</a></span>
<p>descriptio n of link</p>
</div>

I have the following CSS:
div.links {
width:auto;
margin:8px;
margin-bottom:13px;
border-bottom:1px solid;
}
div.links h2 {font-size:110%;text-align:left;}
div.links span a{text-align:left;font-weight:bold;}
div.links p{text-align:left;font-size:80%;}
div.links img {float:left;pad ding:0px 10px 0px 0px;}

Now in my HTML I have plenty of <div class="links"th e one under another.

If the image is big and the description short then the image extends
outside the div. How can I overcome this behavior and have a div that
extends at 100% of the necessary height? (i have already tried
height:auto with no luck)
height: auto is what they are already, and divs don't grow in height to
contain floats that overflow them[1]

We want the bottom border to clear the floats. We can achieve this by
making the bottom border an element in itself, and setting clear on it.

So if you change your margin on .links to

margin:8px 8px 0 8px;

i.e. all 8px except bottom (unless I got the order wrong there).

and then create a new selector:

div.separator
{
border-bottom: 1px solid;
margin-bottom:13px;
clear: left;
}

Then after each <p>descriptio n</padd

<div class="separato r"></div>

in the content.

[1] divs _do_ grow to contain the divs inside them if they are
themselves the "block formatting context boxes" for the floats, which
they are if they are floated or positioned themselves.

So we can fix this another way without moving the borders to extra
separating divs by adding this to div.links:

float: left;
clear: left;
width: 100%;
Dec 29 '06 #2
Ben C wrote:
On 2006-12-29, Harris Kosmidhs <hk******@remov e.me.softnet.tu c.grwrote:
>Consider the following HTML:

<div class="links">
<img src="linkimages/logo.gif">
<h2>title</h2>
<span><a target="_blank" href="http://">http://</a></span>
<p>descriptio n of link</p>
</div>

I have the following CSS:
div.links {
width:auto;
margin:8px;
margin-bottom:13px;
border-bottom:1px solid;
}
div.links h2 {font-size:110%;text-align:left;}
div.links span a{text-align:left;font-weight:bold;}
div.links p{text-align:left;font-size:80%;}
div.links img {float:left;pad ding:0px 10px 0px 0px;}

Now in my HTML I have plenty of <div class="links"th e one under another.

If the image is big and the description short then the image extends
outside the div. How can I overcome this behavior and have a div that
extends at 100% of the necessary height? (i have already tried
height:auto with no luck)
[1] divs _do_ grow to contain the divs inside them if they are
themselves the "block formatting context boxes" for the floats, which
they are if they are floated or positioned themselves.

So we can fix this another way without moving the borders to extra
separating divs by adding this to div.links:

float: left;
clear: left;
width: 100%;
yes it works thanks.
But I would like to clear this out a bit. How float affects div's
height? I thought float is used to position -maybe not the right word
here, forgive my english- the element to its parent element.
Dec 29 '06 #3
On 2006-12-29, Harris Kosmidhs <hk******@remov e.me.softnet.tu c.grwrote:
Ben C wrote:
[snip]
>[1] divs _do_ grow to contain the divs inside them if they are
themselves the "block formatting context boxes" for the floats, which
they are if they are floated or positioned themselves.

So we can fix this another way without moving the borders to extra
separating divs by adding this to div.links:

float: left;
clear: left;
width: 100%;

yes it works thanks.
But I would like to clear this out a bit. How float affects div's
height? I thought float is used to position -maybe not the right word
here, forgive my english- the element to its parent element.
Yes it is. But a float also "establishe s a new block formatting
context", which means it grows to fit the floats inside it in.

In this case the float doesn't actually visibly float to the left itself
because we make it width: 100%. We make it float:left not to make it
float to the left, but just to get the sideffect of starting a new block
formatting context.

Normally floats overflow their containers. This is so you can write
content like this:

<ptext blah blah <img style="float: right;" src="foo.png"</p>
<pmore text blah blah blah </p>

In this example, if the first paragraph grew to fit the float in,
there'd be a vertical gap between the two paragraphs. This is less
desirable than the second paragraph starting in its proper place,
immediately below the first paragraph, but also flowing around the
float.

So that's why the float overflows its containing box. But it doesn't
overflow its block formatting context box.

CSS 2.1 9.4.1:

"Floats, absolutely positioned elements, inline-blocks, table-cells, and
elements with 'overflow' other than 'visible' establish new block
formatting contexts."

And then in CSS 2.1 10.6.7 ("'Auto' heights for block formatting context
roots"):

"In addition, if the element has any floating descendants whose bottom
margin edge is below the bottom, then the height is increased to include
those edges. Only floats that are children of the element itself or of
descendants in the normal flow are taken into account, e.g., floats
inside absolutely positioned descendants or other floats are not."

Why these extra rules?

One possible reason is to make the implementation easier. Floats are
already harder to implement than most things in CSS because text in
sibling, descendent, or descendent-of-sibling blocks has to be flowed
around them. This introduces extra complexity by breaking the assumption
that you can make almost everywhere else that the layout of an element
is only affected by its ancestors and descendents. If you can keep
floats inside their block formatting contexts however, you can limit
that complexity a bit.
Dec 29 '06 #4
Harris Kosmidhs wrote:
div.links img {float:left;pad ding:0px 10px 0px 0px;}

If the image is big and the description short then the image extends
outside the div. How can I overcome this behavior and have a div that
extends at 100% of the necessary height?
http://www.quirksmode.org/css/clearing.html

--
Berg
Dec 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13958
by: Glabbeek | last post by:
I'm changing the layout of my site. Instead of using tables, I will use DIVs. It's working fine, except for 1 thing: In IE6 some DIVs are not the correct width. Mozilla and Opera are showing the page the way I want. Does anybody know a solution for this? First of all, the code I am using: CSS ------- body {
3
14231
by: inkswamp | last post by:
I've been using CSS for quite a while now but I have yet to master its eccentricities. Here's something I recently ran across and if someone could explain why this happens, I would appreciate it. I've included code below. The code produces three divs, each with height set to "auto." Within each div is an image. If I float the image left or right, the height of the box does not accommodate the height of the image automatically. If I don't...
0
7694
by: Seguros Catatumbo | last post by:
Hi, i have this code on a test site: <html> <head> <style type="text/css"> #main{ border:1px solid #000063; border-width:1px; background:#FFF; width:400px; margin: 0 auto;
1
2985
by: yellowtek | last post by:
Hi, I display some information (actually a table of data) within a <div> which height is constrained, making display a vertical scrollbar: <div style='overflow: auto height:300px;'> I need to refresh the data within this <divwithout changing the current scroll position set by the user. ... but the scrollbar is always reset to the top of the document....
0
1773
by: nathbooth | last post by:
Hiya, I have this website that i am making, the main content below the menu navigation is loaded through php nath.timboothcsl.co.uk <?php error_reporting(E_ALL ^ E_NOTICE); $file = "home.php"; $file = $_GET; $filePath = "${file}.php";
1
2980
by: neridaj | last post by:
Hello, I've found a few postings of this problem but none of the answers seem to fix my problem. I have a content div wrapped around a left floated image and a right floated table. I want the table to dictate the height to the content div instead of popping out of the bottom of the content div. Seems pretty simple but too complicated for me I guess. If you know how to achieve this please share. Thanks, J /*begin code*/
1
5167
by: pravinnweb | last post by:
can anyone tell me how to set auto height to outer div that is in green box id "gray-background" it should increase relatively to inner div "smbox" here is the css and html code it should work in FF, IE6 and IE7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Employee Listing </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords"...
3
8951
by: gaya3 | last post by:
How do i set autoHeight dynamically in dojo grid 1.1? please do help... Thanks in Advance
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.