I have been asked this question several times in the previous interview, every time the answer is not satisfactory this good tidy down the record
First position provides properties that let us implement the positioning function, which allows you to define where the element box should appear relative to its normal position, or relative to the location of the parent element, another element, or even the browser window itself.
The value of the Position property,
Static: The default value element appears in a normal stream, the block-level element generates a rectangle, and the inline element generates one or more row boxes, placed in its parent element
Inherit: Specifies the value of the position that inherits the parent element from the parent element
Fixed: To generate an absolutely positioned element, relative to the browser to locate the window, this property is similar to the table absolute except that its containing block is the browser window itself, it is also out of the original document flow layout, relative to the location of the browser window to locate ( The expression is the position of this element relative to the position of the browser window, even if you move the scroll bar)
Relative: Generates a relatively positioned element, which is positioned relative to the normal position, and the element still retains its pre-positioned shape, that is, the block level is still the block level The original inline-level is still inline-level while its original position in the document is still retained, relative still belongs to the basic flow layout
Here is an example
In this example we have two paragraphs set on the second paragraph
{ position: relative; Left:20px; }
We will find that he moved to the right 20px relative to itself (i.e. the position of the 11111 paragraph) if we set the width of the paragraph by examining the element, the size of the element has not changed.
Absolute: Generates an absolutely positioned element, positioned relative to the first parent element other than static, if all its parent elements are not set to be positioned, then positioned relative to the outermost body, by setting the left top bottom Right sets the element to the corresponding position of the containing block, and the element that sets the absolute generates a block-level box that is then laid out, which is out of the original document flow
Detailed Position:absolute
We can control the position of the element relative to the containing element by setting the left right top bottom
1) When we do not set the left top right bottom performance
It is then positioned relative to its immediate parent element in top:0 left:0 (out of the original document flow)
Take a look at the following example
< div > < img src = "1.jpg" Alt = "" > < p > 1111111111111111111111111111</ p > </ div >
body,p{ margin:0; } {width:200px; Margin:20px; background: lightblue; }
In a div we include a picture and a paragraph p
Here I do not have a parent div set height of parent element div Height by img height and p (here p is block level element, can be set height when height is determined by line-height when for inline element of span etc.) and decision
When we set the Position:absolute for IMG, a familiar scene appeared.
Similar to the previously encountered float collapse scenario, but there are some different img out of the original document flow so this time the parent element's height is directly and P's height is determined and the IMG is positioned relative to the top left corner of the Div, which will overwrite the p after, That is, the width and height of the missing (IMG is still high, because it is out of the flow of the document to create this effect)
2) when the left top right bottom is set
A careful understanding of the positioning of elements other than the first static element of a parent element
There's a page structure like this
<id= "Content"> <id= "a" ></div> <ID= "B" ></div> </div>
Body{margin:0; }#content{width:400px;Height:400px;background:LightBlue;margin:30px; }#b{width:200px;Height:200px;background:Green; }#a{width:100px;Height:200px;background:Red; }
On the page above such a
When we set the corresponding Position:absolute and position values for the DIV with ID a
{ position: absolute; Left: 0; top:0; width:100px; height:200px; background: red; }
We found him positioned relative to the upper-left corner of the body. We set the location for the parent element Div (ID content).
{ position: relative; width:400px; height:400px; background: lightblue; margin:30px; }
He was found to be positioned relative to the upper-left corner of the parent element with the position:relative set.
For this we can use Relative+absolute combination to achieve the effect of float
Think about the effect we wanted to achieve in the past.
Make a hyperlink left a hyperlink right
<DivID= "Content"> <ahref= "Left"ID= "a">Left</a> <ahref= "Right"ID= "B">Right</a> </Div>
Body{margin:0; }#content{width:700px;Height:100px;background:LightBlue;margin:30px; }a{Color:Red; }#a{float: Left; }#b{float: Right; }
We can use the above method, but there are some problems with float. We're going to clear the float, and so on. We can use this relative+absolute way to lay out
Body{margin:0; }#content{position:relative;width:700px;Height:100px;background:LightBlue;margin:30px; }a{Color:Red; }#a{position:Absolute; Left:0;Top:0; }#b{position:Absolute; Right:0;Top:0; }
This also achieves the effect we want (and there is also the limitation that our elements are out of the flow of the document, such as when we add other content to the contents element, from the beginning of the content element)
Reference http://www.cnblogs.com/bokin/archive/2012/12/14/2816864.html
http://www.zhangxinxu.com/wordpress/2010/01/absolute%E7%BB%9D%E5%AF%B9%E5%AE%9A%E4%BD%8D%E7%9A%84%E9%9D%9E%E7% bb%9d%e5%af%b9%e5%ae%9a%e4%bd%8d%e7%94%a8%e6%b3%95/
Http://www.w3school.com.cn/css/css_positioning.asp
CSS Position Small note