Home >Web Front-end >HTML Tutorial >css position attribute (difference between absolute and fixed)_html/css_WEB-ITnose
In CSS3, the attribute values of position are: inherit, static, relative, absolute, fixed.
Inherit is to inherit the position attribute value of the parent element, and IE does not support it.
static Default value, the element appears in the normal flow, ignoring (TRBL) and z-index values. Please refer to the example below. Since the value of position of div1 is static, top and left have no effect.
<!Doctype html><meta charset="utf-8"><head><title>test position static</title> <style type="text/css"> .divPos{ position:static; left:100px; top:100px; } </style></head><body><div class="divPos">this id div 1(position is static)</div><div class="div2">this is div 2</div></body>
Difference between inherit and static: at runtime During the process, if the position is static, it will always remain unchanged, if it is inherit, it can change during the running process.
relative relative positioning, positioned relative to the normal position of the normal document flow. As in the example below, a div is offset by 100px relative to its normal position. Something to note here is that after positino is set to relative, the element still retains its shape before being positioned, and the space it occupies will be retained. That is to say, before positioning, div is a block-level element by default (div default attribute), and it is still a block-level element after positioning.
<!Doctype html><meta charset="utf-8"><head><title>test position static</title> <style type="text/css"> body{ background-color:#ffff33; } .divPos{ position:relative; left:100px; top:100px; background-color:#000fff; } </style></head><body><div class="divPos"> The position of this div is relative...</div></body>
absolute generates an absolutely positioned element, positioned relative to the first parent element other than static. As shown in the example below,
<!Doctype html><meta charset="utf-8"><head><title>test position static</title> <style type="text/css"> div{ background-color:#33ff33; } .divPos{ position:absolute; left:50px; top:50px; } .div3{ position:absolute; left:50px; top:50px; } </style></head><body><div class="divPos">div parent <div> <div class="div3">div child 3</div> </div></div></body>
fixed produces an absolutely positioned element, positioned relative to the browser window. As in the example below, the scroll bar on the right can be scrolled, and the div is always at the same position relative to the window. In fact, a more appropriate example is to make a shopping cart.
<!Doctype html><meta charset="utf-8"><head><title>test position static</title> <style type="text/css"> .divPos{ position:fixed; left:50px; top:50px; } .div1{ height:1000px; } </style></head><body><div class="divPos"> The position of this div is fixed.And this div will be always here.</div><div class="div1"></div></body>