1

I have a problem with footer positioning. It doesn't go to the bottom/last.

So, I have a container div which has 3 divs - float:right , float:left and the center one (which has position:absolute) that comes between the two floated divs.

The center one must have fixed width and height because it's an image.

In that center div I have another div with a lot of content.

The problem is, because the center div has fixed width and height, it doesn't take the childs div height.

So my problem is how to put the footer that it comes last (after the container)?

Note - with JQuery I put the width of the floated divs because they take 100%-980px width.

This is how it looks like.

I tried putting to the center div overflow:auto,overflow:overlay,margin-left:auto;margin-right:auto;.

Vucko
  • 20,555
  • 10
  • 56
  • 107

4 Answers4

2

Try this for the parent. overflow:auto;

Also refer to this stack overflow post: Expanding a parent <div> to the height of its children

Community
  • 1
  • 1
Karan Verma
  • 1,721
  • 1
  • 15
  • 24
  • It's puts a scrollbar and that isn't the option. All content must be **visible**. And I tried all the solution from your link earlier, but nothing worked. – Vucko Feb 06 '13 at 23:23
2

After reading your question again an again i come to conclusion and create the below fiddle using your code and embed a sample image for you desired size.

Please let me know if i am wrong while understanding your question. So i can work around according your needs.

fiddle: http://jsfiddle.net/ah3nr/6

Demo: http://jsfiddle.net/ah3nr/6/embedded/result/

My approach:

I have remove the position:absolute from center div and added new div for image and relate them both using css layer techniques.

Updated css:

.sectionDownContainer {
    width: 980px;
    /*height:270px;*/
    border:1px solid red;
    /*position: absolute;*/
    position:relative;
    top: -32px;
    z-index: 1;
}
/*.sectionDownMenu {
    margin-left: 50px;
    margin-top: 1px;
    display: block;
}
*/

#image_container {
    position:relative;
    width:980px;
    height: 270px;
    margin-top:-2px;
    z-index:2;
}

.sectionDownContent {
    width: 640px;
    margin-top: -190px;
    margin-left: 50px;
    position: relative;
    z-index:5;
    color:#000;
    font-weight:bold;
}

Screenshot: enter image description here

w3uiguru
  • 5,864
  • 2
  • 21
  • 25
  • You got ir right. I come to something similar also with **margin-top:-xpx** later. Is it possible ( via **CSS**) to expand the image so the expanded area isn't the picture repeating , that it's only white space? And why is there space between the **footer** and **sectionDown**? – Vucko Feb 09 '13 at 14:29
  • Found out why there is space between **footer** and **sectionDown**. – Vucko Feb 09 '13 at 16:18
  • @vucko, i am understanding exactly what you want from the image? Using css we can stretch image by applying cs on img tag using ID or class and set the width in percentage. but if stretch more than its exact width its pixels get bursted and image bill look blurry. you tell me your exact desired layout i can easily accomplish that. I have question why you use floated div if you do not have text, image, advertisement in the floated div? If you are using just for the sake of filling that particular area using green color than it can be done via css no need to floated div and jquery calculation. – w3uiguru Feb 10 '13 at 03:21
  • @vucko, By mistake i missed to add NOT in previous comment. i am not understanding exactly what you want from the image? – w3uiguru Feb 10 '13 at 03:28
  • Solutions that implement code like this: `margin-top: -190px; margin-left: 50px;` are always bad design. You shouldn't use any absolute values for your layout, except for a header, footer, and navigator (they can be all absolute or fixed). Anyhow, good solutions mostly implement divs like this code: `margin 0px; border:0px; padding:10px`, while padding is used to control the containing

    -tags. @Singh: As I said in my answer, if you use float, you just can't use any absolute positioning, this works very good.

    – Marcus Feb 10 '13 at 08:43
  • @HappySingh, is it possible to do [this](http://jsfiddle.net/njTaL/1/) but with all height and [not expanding the image](http://jsfiddle.net/njTaL/2/)? – Vucko Feb 10 '13 at 11:01
  • @vucko in you commented fiddle you have used cloud image as background that is the correct way to get the image as background and that will repeat vertically according to height of text contained div. If you use img tag and then you want to expand up to height od text contained div that will be very tedious and odd too because if stretch the height of image more that actual image height, the image pixels look stretched and blurry. I asked you in previous comments what is desired layout? If you clearly explain me i can create that very easily. – w3uiguru Feb 10 '13 at 13:47
2

You need to set this property of the center-div: height:auto (you could also add a minimum height: min-height:400)

About your second question with the footer, this is much more complicated. You must do this:

<div id="content">
    <div id="content_left">
    </div>
    <div id="content_center">
    </div>
    <div id="content_right">
    </div>
    <div id="footer">
    </div>
</div>

I'll give you now the full CSS (because it's not so easy):

.content {position:relative; overflow:hidden;} //hidden overflow just a hack for common issues...
.content_left {height:auto; float:left} //set height to auto (very important)
.content_center {height:300; float:left} //a fixed height also works!
.content_right {height:auto; float:right}
.content_footer {width:100%; height:auto; float:right} //for tests you can also set a fixed height

This solution is also according to other threads on Stackoverflow: Align DIV's to bottom or baseline, How to align content of a div to the bottom?

But, if you experience problems with that, you may do this (my preferred solution):

<div id="content">
    <div id="content_left">
    </div>
    <div id="content_center">
    </div>
    <div id="content_right">
    </div>
</div>
<div id="footer">
</div>

And its CSS:

.content {position:relative; overflow:hidden;} //hidden overflow is just a hack
.content_left {height:auto; float:left} //set height to auto (very important)
.content_center {height:300; float:left} //a fixed height also works!
.content_right {height:auto; float:right}
.content_footer {width:100%; height:xxx; float:left} //you can use any height...

Note that all above solutions works only if you set all the "contents" to float, it doesn't work with absolute values! I found this here: http://wiki.answers.com/Q/How_can_a_parent_DIV_wrap_around_child_DIVs_which_are_floating_left_or_right

This is due to an issue with divs: It's not possible to "tell" a parent div the size! So childs like "content_center" or "content_right" won't tell the "content" how long they are and how long "content" must be. So it's impossible to tell the footer where to align, if you use absolute values for the childs.

So your second question, although it looks trivial, is a very important question, and not easy to solve.

IMPORTANT UPDATE:

I tried to find a solution with absolute now. The problem is, that absolute and fixed are taken out of the regular (text)flow, so their size can't influence the size/positioning of any other element anymore. But we also have to understand that an absolute element still controls all its childs, so we should rather set the childs as relative than the parent (here: "content")! So I finally found the solution, and it's quite weird, because it's almost the opposite thing I suggested above, but that solution was influenced by the posting of others, while following solution is "my own" one (I added a header for demonstration purpose):

<div id="header">
</div>
<div id="content">
    <div id="content_left">
    </div>
    <div id="content_center">
    </div>
    <div id="content_right">
    </div>
    <div id="footer">
    </div>
</div>

The CSS (the "header" clearly shows, that "content" inherites all positioning to its childs like "content_left", "content_right", aso.):

.header {position:absolute; left:0; top:0; height:100; width:100%}
.content {position:absolute; left:0; top:100; height:auto; min-width:700} //min-width is only voluntary, but quite useful
.content_left {position:relative; left:0; top:0; width:200; height:auto;} //height:auto is important to adapt the height from containing text!
.content_center {position:relative; left:200; top:0; right:200; width:auto; height:auto;} //in the middle element, also auto-width is important!
.content_right {position:fixed; right:0; top:0; width:200; height:1000;} //we set a fixed position, but that won't influence the footer anymore!
.content_footer {margin:0 0 60 0; position:relative; left:0; bottom:-60; width:100%; height:150;} //a fixed height is also okey...but relative position is needed!
//you still need to add margin:0; border:0; padding:0 or similar values for some elements to get a good layout

The important point here is, that you can decide which child element will be the longest one, and set this element's position:relative, while the other may have absolute or fixed. But if you don't know which element will be the longest, all child's positions need to be set as relative. Anyway, I suggest to set all childs to relative (beside fixed if needed), because their parent "content" will set their absolute height-position already correctly, so there's no need for any absolute at all.

I'm repeating myself: Above I wrote it's not possible to tell a parent div the size...actually it's possible, but not if the values absolute and fixed are used. Only if you use the browser standart value (static) or relative, the parent div will be informed about the size of its childs, an therefore the footer is set correctly at the bottom of the page.

Well, my solution works everywhere...even in IE (tested 6.0 and 8.0!) due to the hack margin:0 0 60 0 where the value 60 should be the positive value of bottom:-60. Now we finally got the non-floating crossbrower-solution. ;)

Community
  • 1
  • 1
Marcus
  • 1,222
  • 2
  • 13
  • 22
  • I prefer to avoid `float`. I only put it on the `content_left` and `content_right` because it didn't break when resizing ( I tried with `position:absolute` but it didn't do the trick). – Vucko Feb 10 '13 at 11:05
  • So there is the solution without float. Took me a while to find it. %) – Marcus Feb 11 '13 at 19:40
1

The problem you're experiencing is that certain CSS properties cause elements to be "removed from the flow" of the document (see the W3C Visual formatting model). Parent elements naturally grow to fit the height of children elements, however, floated and absolutely positioned elements are removed from the document flow. As mentioned in a few comments, setting overflow: auto; or overflow: hidden; on the parent element re-establishes a bounding box around floated elements. This means you can float elements within the parent container, then set overflow: hidden; on the parent element, and the parent element will contain the floats. However, this doesn't work for absolutely positioned elements: the absolutely positioned box is "removed from the normal flow entirely (it has no impact on later siblings)". The only exception is that the entire document will try and grow to display any positioned elements (give an element position: absolute; top: 3000em; and the page scrollbar will grow to allow you to scroll to that element). I don't know of any way to trigger this for elements other than the document.

Back to your intended effect… If you don't need IE7 support, you can use display: table; table-layout: fixed; to achieve a centered column with a fixed width and two columns of variable width on either side.

jsFiddle Demo

In the near future, this will also be possible using the CSS "flexbox" properties. Flexbox will allow for some nifty new features, including horizontal and vertical centering, changing the order of rendered elements, and setting "flex" values for how much of the remaining variable width an element should take. However, the standard is currently going through a period of flux, and the old standard (enjoying moderate support) is being replaced by a new standard (with little to no support). See "Old" Flexbox and "New" Flexbox and the accompanying demo. Considering the glacially slow progress of web standards implementation, I don't expect to see this in use for a few years unless a truly masterful polyfill is produced.

thirdender
  • 3,891
  • 2
  • 30
  • 33
  • +1 This is a very nice solution. But for some reason, it breaks when resizing the browser. Is that due to my desing or due the `table atributes`? – Vucko Feb 10 '13 at 11:04
  • What breaks? If the left and right sidebars are getting too small when resizing the browser down, just add an appropriately sized `min-width: ...px;` CSS property to your `BODY` tag. Anything else breaking? – thirdender Feb 10 '13 at 17:15