in between floated elemnts if we give position relative to any element will it effect to flow of document? - css

in between lots of floated elements if we give position:relative to any element to use position:absolution inside, will it effect to flow of document?
FLOAT 1 | FLOAT 2 | FLOAT 3 + position relative | Float 4
if i only want to give pixel perfect position to an element inside Float 3 box then what should i do? I need compatibility in all browser including IE6

No, it won't. Position relative normally does not affect the flow of the document (siblings) at all unless it is a browser bug. Of course it is meant to affect how a position:absolute child is positioned.
position: relative is the correct way to allow for an absolutely positioned child element.

No, position: relative won't affect that element's flow, it will just allow you to give child elements absolutely positioned to it, and a few extra tricks (like using z-index to raise it's z stacking index).

Related

CSS position:absolute destroys layout after element

I got a DIV in my HTML-Markup, which is positioned relative. The DIVs inside this container are absolute.
this destroys the complete layout, because the following HTML-Elements are now positioned wrong, the (above) DIV which is relative, overflows them...
but why? How can I fix it?
Once you position a container with :relative, everything inside it will be positioned in relation to that. The position:absolute pulls the child elements out of the flow of the page an positions them at the top of the container with position:relative.
Thus, once you have elements with position:relative, you will want to also add rules to move and position the child elements. Consider using left: and top: or right: and bottom to position your :absolute child elements.
Alternatively, you might try to position the child elements with :relative as well, if you don't want them pulled out of the flow of the page.
Do you have code to look at, or a jsfiddle? Otherwise it's difficult to know what's going on and to suggest a fix.
If your relatively-positioned element only contains absolutely-positioned elements, you need to give it a height setting, otherwise it will have zero height (the absolutely-positioned elements won't be counted for auto height), and therefore probably cause some overlapping with subsequent elements.

When can you float?

Can you float elements that are positioned in any way? Or do floated elements have to be static? For example, is it possible to float elements which are positioned relatively?
Float and position are two different things, although they do influence each other. Floats have zero impact on elements which are positioned absolutely or fixed. Floats only have an impact on elements which are positioned statically (Default) or relatively.
Much of this is dictated by a single subsection within the spec. In particular if you're only concerned about how the position property interacts with the float property, then that subsection says that
you cannot float an element that is absolutely positioned (either position: absolute or position: fixed), but
you can float an element that is either relatively positioned or static (i.e. non-positioned).
In both cases, the position property will take effect as normal, but the float property will not have any effect on an absolutely positioned element. This means that an absolutely positioned element will remain in its absolute position, and a relatively positioned element can still be shifted relative to where it would originally have been located, taking the float property into account (as well as acting as a containing block for other positioned elements).

CSS absolute positioned elements and margins

Am a right to conclude that a CSS margin (e.g. margin-left) influences the final position of an absolute postioned element? It seems a negative margin-left pulls it to the left (of it's absolute postion), a positive value to the right (of it's absolute postion).
Can someone explain me more about the combination absolute positioned elements and margins?
Thanks.
Correct. Margins influence where the edges of an absolutely positioned element begin.
Lets understand it this way:
When you have an statically-positioned element, the element is part of the normal flow of the document. Hence, any margins applied on it are considered 'with respect to its surrounding elements'.
When you have an relatively-positioned element, the element is still part of the normal flow of the document. Hence, any margins applied on it are still considered 'with respect to its surrounding elements'.
BUT,
When you have an absolutely-positioned element, the element is taken out of the normal flow of the document. This element's positioning is now dictated by the first parent container that is not statically positioned (or the top level body element as a fallback). Hence, when you apply margin, the parent container is taken as the 'surrounding element' and the margin in calculated with 'respect to it'.
Hope this helps.

Absolutely positioned elements have no height. What does it mean?

Hy,
I read on the web that absolutely positioned elements have no height. Can someone explain this in a simple way and possible illustrate it with an example.
I think you're a bit confused. You probably heard that elements which contain only absolutely positioned elements have no height. That makes more sense.
An absolutely positioned element, like any element, would have the height of it's content, unless specified otherwise with height or min-height.
An element containing only absolutely (or floated) positioned elements have no height, because their content (a.k.a. the positioned elements) are taken out of the document's flow. Which means, they aren't rendered along the render tree of the document, where inline elements stack horizontally, and block elements are stacked vertically, but are rendered on an absolute position, relative to the nearest positioned ancestor. Because of that, their size doesn't count towards the parent's height.

what is the containing block of an absolutely positioned element?

what is the containing block of an absolutely positioned element? it seems the rule can be a bit complicated...
the spec should be here:
http://www.w3.org/TR/CSS21/visudet.html#containing-block-details
i want to verify if the following is true:
for simplicity, assume the containing block is a block element (not inline element)...
1) if the absolute positioned element has a closest ancestor that is positioned "non static" (relative, fixed, or absolute), then that ancestor is the containing block. the absolute positioned element is relative to it.
2) if there is no such ancestor, then the viewport is the containing block, and so the absolute positioned element is relative to the viewport.
no matter what the containing block is above, the width:100% or n% and height:100% or n% are both relative to the containing block.
that's why a
<div style="position:absolute;width:100%;height:100%;background:green"></div>
right under <body> will cover up the whole viewport exactly -- no more, no less.
we could also use position: fixed, except IE 6 doesn't support it... and so the poor programmer need to use position: absolute instead... (well, not a big deal)
Is that an accurate description of an absolute positioned element? If so, i think IE 6 and above, FF, Safari, Chrome all follow this behavior accurately?
You are correct. The containing block is the last positioned element. so if you want to explicitly set the container then give it position:relative. Most browsers get this right. CSS doesn't really have a 'viewport', I think the top is the HTML element though don't hold me to that. IE prior to 7 had an unnamed element above HTML though.
Summary:
position: relative
Does nothing except set the positioning context for all the elements contained within it. You can then position: absolute any element it contains by setting (typically one or two) values from the possible top, right, bottom or left.
If you give an element with position: relative a top, right, bottom or left value, it will shift position accordingly but leave a blank space where it would be by default. In other words, it remains within the document flow but offset.
position: absolute
To position something absolutely, you need to ask 'absolutely, but relative to which containing element'? It will either be the entire body (the default) or some other element on the page that is already positioned (usually with relative or absolute - fixed is also useful and supported in IE 7 but see this bug). It is then taken out of the document flow - other elements might appear beneath it, but won't flow around it. If it appears behind another element, you need to set the z-index property to move it in front.
A common solution is to have a centred container (margin: 0 auto) with position: relative within which other items are placed with position: absolute.
Finally, I like this little interactive CSS positioning demo.

Resources