473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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>description 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;padding:0px 10px 0px 0px;}

Now in my HTML I have plenty of <div class="links"the 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 41693
On 2006-12-29, Harris Kosmidhs <hk******@remove.me.softnet.tuc.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>description 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;padding:0px 10px 0px 0px;}

Now in my HTML I have plenty of <div class="links"the 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>description</padd

<div class="separator"></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******@remove.me.softnet.tuc.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>description 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;padding:0px 10px 0px 0px;}

Now in my HTML I have plenty of <div class="links"the 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******@remove.me.softnet.tuc.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 "establishes 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;padding: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
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...
3
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. ...
0
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
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...
0
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 =...
1
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...
1
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...
3
by: gaya3 | last post by:
How do i set autoHeight dynamically in dojo grid 1.1? please do help... Thanks in Advance
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.