CSS Position

In CSS, the Position attribute is often used, mainly absolute positioning and relative positioning. Simple use is no problem, especially when nested, it will be a bit confusing.

Position attribute: Specifies the positioning type of the element. That is, the elements are separated from the layout of the document flow and displayed anywhere on the page.

position attribute value:

absolute: Generate absolutely positioned elements, relative to the first element other than static positioning parent element for positioning. The position of the element is specified through the "left", "top", "right" and "bottom" attributes.

relative: Generates relatively positioned elements, positioned relative to their normal position. Therefore, "left:20" adds 20 pixels to the element's LEFT position.

fixed: Generate absolutely positioned elements, positioned relative to the browser window.

static: Default value. Without positioning, the element appears in normal flow (ignoring top, bottom, left, right or z-index declarations). The position of the element is specified through the "left", "top", "right" and "bottom" attributes.

inherit: Specifies that the value of the position attribute should be inherited from the parent element.

The two most commonly used methods are absolute and relative.

position auxiliary attribute:

##①left: Indicates how many pixels to insert to the left of the element , how many pixels to move the element to the right.

②right: Indicates how many pixels to insert to the right of the element and how many pixels to move the element to the left.

③top: Indicates how many pixels to insert above the element and how many pixels to move the element downward.

④bottom: Indicates how many pixels to insert below the element and how many pixels to move the element upward.

The value of the above attributes can be negative, unit: px.


##Absolute Absolute positioning
##Absolute positioning ;The layout breaks away from the document flow, and the remaining space is filled by subsequent elements. The starting position of positioning is the nearest parent element (postion is not static), otherwise it is the Body document itself.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>实例</title> 
  <style type="text/css">
        html body
        {
            margin: 0px;
            padding: 0px;
        }
        #parent
        {
            width: 200px;
            height: 200px;
            border: solid 5px black;
            padding: 0px;
            position: relative;
            background-color: green;
            top: 15px;
            left: 15px;
        }
        #sub1
        {
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            top: 15px;
            left: 15px;
        }
        #sub2
        {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id="parent">
        <div id="sub1">
        </div>
        <div id="sub2">
        </div>
    </div>
</body>
</html>

Note:We found that due to the absolute positioning of Sub1, the position of Sub1 was offset, while the same level Div Sub2 occupied The position of Sub1, and Sub1 blocks Sub2.

#Relative Relative positioning

Relative positioning; does not break away from the layout of the document flow, Only changes its own position, leaving blank space in the original position of the document flow. The starting position of positioning is the original position of this element in the document flow.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>实例</title> 
  <style type="text/css">
        html body
        {
            margin: 0px;
            padding: 0px;
        }
        #parent
        {
            width: 200px;
            height: 200px;
            border: solid 5px black;
            padding: 0px;
            position: relative;
            background-color: green;
            top: 15px;
            left: 15px;
        }
        #sub1
        {
             width: 100px;
             height: 100px;
             background-color: blue;
             position: relative;
             top: 15px;
             left: 15px;
        }
        #sub2
        {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id="parent">
        <div id="sub1">
        </div>
        <div id="sub2">
        </div>
    </div>
</body>
</html>

Note: We will find that Sub1 is offset, which does not affect the position of Sub2, and at the same time covers Sub2. Remember that the offset is not relative to the Div Parent, but Relative to the original position of Sub1.

Fixed Positioning

Fixed positioning; similar to absolute, but does not change position as the scroll bar moves.

<meta charset="utf-8"> 
<title>实例</title> 
  <style type="text/css">
        html body
        {
            margin: 0px;
            padding: 0px;
        }
        #parent
        {
            width: 200px;
            height: 200px;
            border: solid 5px black;
            padding: 0px;
            position: relative;
            background-color: green;
            top: 15px;
            left: 15px;
        }
        #sub1
{
  width: 100px;
  height: 100px;
  background-color: blue;
  position: absolute;
  top: 15px;
  left: 15px;
}
#sub2
{
  width: 100px;
  height: 100px;
  background-color: red;    
  position: fixed;
  top: 5px;
  left: 5px;              
}
    </style>
</head>
<body>
    <div id="parent">
        <div id="sub1">
        </div>
        <div id="sub2">
        </div>
    </div>
</body>
</html>

Note: You will find that Sub2 is always positioned with body.


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>实例</title> <style type="text/css"> div { height: 200px; width: 300px; border-color: Black; border-style: solid; border-width: 1px; } #a { position:absolute; left:900px; top:150px; } #b { position:relative; left:500px; top:100px; } #c { position:fixed; left:970px; top:400px; } #d { position:static; background-color:Window; } </style> </head> <body>   <div id="a" >     div-a<br />     position:absolute;<br />     绝对定位;脱离文档流,遗留空间由后续元素填充。   </div>   <div id="b" >     div-b<br />     position:relative;<br />     相对定位;不脱离文档流,只改变自身的位置,在文档流原先的位置遗留空白区域。   </div>   <div id="c" >     div-c<br />     position:fixed;<br />     固定定位;固定在页面中,不随浏览器的大小改变而改变位置。   </div>   <div id="d"></div>   <br><br><br><br><br><br><br><br><br><br> </body> </html>
submitReset Code