Home >Web Front-end >CSS Tutorial >How to use css position
We often use position for hierarchical absolute positioning, so how can we master the use of CSS to manipulate the position attribute? Today we are here to study it with you.
position syntax and structure
position syntax:
position : static absolute relative
position parameters:
static : There is no special positioning, the object follows HTML positioning rules
absolute: Drag the object out of the document flow and use left, right, top, bottom and other attributes for absolute positioning. And its cascading is defined through the css z-index attribute. At this time, the object does not have margins, but still has fillers and borders
relative: The object cannot be stacked, but will be offset in the normal document flow based on left, right, top, bottom and other attributes
position description:
Setting the positioning method of the object can make the layout layer easy to position absolutely and control the box object more accurately.
The actual use of position
Absolute positioning position is used to position box objects. Sometimes there are several small objects in a layout, and it is not easy to use css padding and css margin for relative positioning. At this time we can use absolute positioning to easily do it. Especially for the irregular layout of several small boxes in a box, it is very convenient for us to use position absolute positioning to lay out objects at this time.
Absolute positioning position demonstration is suitable for images and irregular layouts. You can use position:absolute;position:relative for absolute positioning.
Absolute positioning and float cannot be used at the same time, such as a large box. Some of them are absolute positioning, and some use css float floating positioning. In this way, the IE6 browser will not display these absolute positioning and relative positioning in the changed object. This can also be regarded as IE6 CSS HACK. Just be careful not to mix them.
Absolute positioning usage conditions
position:absolute;position:relativeThe use of absolute positioning is usually the parent defines position:relative positioning, the child defines the position:absolute absolute positioning attribute, and the child uses Left or right and top or bottom perform absolute positioning.
.div{position:relative} definition, usually it is best to define CSS width and CSS height
.div-a{position:absolute;left:10px;top:10px} defined here The distance from the left side of the parent is 10px, and the distance from the top of the parent is 10px
or
.div-a{position:absolute;right:10px;bottom:10px} defined here The distance to the right of the parent is 10px, and the distance to the bottom of the parent is 10px
corresponds to the HTML structure
<div class="div"> <div class="div-a"></div> </div>
This way, "div-a" is absolutely positioned in the parent "div" box Inside.
Note that left (left) and right (right) can only choose one definition in an object, and bottom (bottom) and top (top) can also only choose one definition in an object.
position application case
Here is an example of applying position absolute positioning. We set the css border of an outermost box to red, css width to 400px, css height to 200px, and the interior contains 2 In order to use absolute positioning for these two boxes, the CSS name of the first box is "div-a", its width is 100px, the background color is black, the height is 100px, the positioning distance is 10px from the parent, and the distance from the left is 10px; the second box CSS class is named "div-b", its width and height are 50px respectively, css background color is blue, the distance from the bottom of the parent is 13px, and the distance from the right of the parent is 15px.
1. The css code is as follows
<style> .div{ position:relative;width:400px;height:200px; border:1px solid #000} /* 定义父级position:relative 为就认为是绝对定位声明吧 */ .divc-a{ position:absolute;width:100px;height:100px; left:10px;top:10px;background:#000} /* 使用绝对定位position:absolute样式 并且使用left top进行定位位置 */ .div-b{ position:absolute;width:50px;height:50px; right:15px;bottom:13px;background:#00F} /* 使用绝对定位position:absolute样式 并且使用right bottom进行定位位置 */ </style>
html code snippet
<div class="div"> <div class="div-a"></div> <div class="div-b"></div> </div>
There are only so many ways to use the position attribute of CSS. Friends who need it can save it. Please continue. Follow this site for other updates.
Related reading:
The above is the detailed content of How to use css position. For more information, please follow other related articles on the PHP Chinese website!