Home  >  Article  >  Web Front-end  >  Positioning usage of position attribute in CSS

Positioning usage of position attribute in CSS

高洛峰
高洛峰Original
2017-02-27 09:25:451620browse

The positioning mode in CSS specifies where a box should be located in the overall layout and what impact it will have on the surrounding boxes. This mode includes regular document flow, floats, and several types of position positioned elements.

Among them, the CSS position property can take on 5 values:
position: absolute
position: relative
position: fixed
position: static
position: inherit
static is the default attribute value of position. Any element to which position:static is applied is in regular document flow. Where it is located and how it affects surrounding elements is determined by the box model.
A statically positioned element ignores all declared values ​​for the top, right, bottom, left and z-index attributes. In order for your element to use these five properties, you need to first apply one of these three values ​​​​to its position property: absolute, relative, fixed
position The element with the value inherit and all other properties inherit If the value is the same, the element will inherit the position value of the parent element.

In order to understand it better later, let me first draw the DOM sketch of this example:
Positioning usage of position attribute in CSS

I think everyone can easily understand the DOM diagram above. , let’s look at the use of position together.

Step one: position: static

The default value of the "position" attribute of all elements in CSS is "static" because there is no need to explicitly Set "position:static" for each element. At this point, everyone will ask, does this attribute value have no meaning? Actually no, it will also play a big role in CSS. Let's look at an example:

For example, if "p#p-1" exists in your two pages at the same time, then you need to absolutely position "p#p-1" in side A. ; And in page B, "p#p-1" does not need to be absolutely positioned.

Absolute positioning of "p#p-1" in page A:

#p-1 {   
    position: absolute;   
   }

At this time, if you don't want to perform absolute positioning in page B, then we must Explicitly reset the postion attribute of "#p-1" to "static" in the style

body.B #p-1 {   
    position: static;   
   }

Step 2: Relative positioning position: relative

relative is called relative positioning. If you specify the position value of an element as "relative", then you can set the element's position through "T-R-B-L" (that is, top, right, bottom, left) Positioning value.

There are several points to note when using relative:

When the element is set to relative, it is positioned relative to the position of the element itself;
After the element is set to relative, you can use "T-R-B-L" Change the current position of the element, but after the element is shifted, the same point will have the original physical space position;
After the element is set to relative, if no "T-R-B-L" setting is made, the element will not make any position changes.
The first and third points of the above three points are relatively easy to understand, so now for the second point, let’s look at the operation of an example:

Based on the above, we "p-1" moves down 20px; moves 40px to the left:

#p-1 {   
     position:relative;   
     top:20px;   
     left:-40px;   
    }

Let's take a look at the effect:
Positioning usage of position attribute in CSS

From the rendering This can once again confirm the second point mentioned above. The element "p-1" has moved 20px down and 40px to the left. Its position has changed, but the physical space originally occupied by the element still exists. In addition, the relative positioning of the element does not affect other adjacent elements.

Step 3: Absolute positioning position:absolute

Absolute is the third attribute value in position. If you specify absolute for the element, the entire element will Floats out of the document flow, and the element's own physical space disappears at the same time. Unlike "relative" which still has the original physical space.

Let’s look at an example of absolute positioning on the p-1a element:

#p-1a {   
    position:absolute;   
    top:0;   
    rightright:0;   
    width:200px;   
   }

Positioning usage of position attribute in CSS

##At this time the element "p- 1a" is not in the original document flow, and its positioning at this time is relative to html, so sometimes we don't need such an effect, even if our element p-1a still wants to be absolutely positioned at p-1 , what should we do? At this point, the "relative" effect of the second step above comes into play.

Step 4: Combination of relative and absolute

第二步中大家知道元素相对定位“relative”是相对于元素自身定位,而在第三步中大家知道元素绝对定位“absolute”是相对于html。但这种说法只有满足这样的条件才是正常的:“绝对定位元素的任何祖先元素没有进行任何的“relative”或者“absolute”设置,那么绝对定位的元素的参考物就是html”,这样一来,“relative”和“absolute”的结合就能起到很大的作用。

我们接下来看一个截图:
Positioning usage of position attribute in CSS

上图做为一个实例来说明“relative”和“absolute”的关系,首先上图中共有三个p放在body内,而且他们三个p的关系是“p-1>p-2>p-3”,而且在p-3有这么一个绝对定位:

.p-3 {   
    position: absolute;   
    left:0;   
    top:0;   
   }

下面分几个情况来说明上图的意思:

1、p-1与p-2都没有设置“position:relative”,此时我们的p-3绝对定位后就漂到了上图中“p-3c”的位置上;

2、现在我们在p-2元素中加设置一个“position: relative”,此时我们的p-3绝对定位后就漂到了上图中的“p-3a”的位置;

3、接下来把相对定位的设置换到p-1元素上,此时p-3绝对定位后就到了p-3b的位置。

花这么多心思,我只想说明一点:如果一个元素绝对定位后,其参照物是以离自身最近元素是否设置了相对定位,如果有设置将以离自己最近元素定位,如果没有将往其祖先元素寻找相对定位元素,一直找到html为止。这句话说起起来好像有点拗口,不知道大家能否明白我说的是什么?如果不明白大家可以参考上图或者下面这个实例效果:

回到上面的实例中,如果我们在“p-1”加一个“relative”:

#p-1 {   
    position:relative;   
   }   
   #p-1a {   
    position:absolute;   
    top:0;   
    rightright:0;   
    width:200px;   
   }

现在我们相对点不在是第三步中的body了,而是“p-1”了,大家看看与第三步的变化:
Positioning usage of position attribute in CSS

第五步:relative和absolute实现布局效果

这一步只要想演示一下使用相对定位和绝对定位实现的两例布局。在前面的基础上,p-1进行相对定位,而p-1a和p-1b进行绝对定位,从而实现两列布局的效果:

#p-1 {   
    position:relative;   
   }   
   #p-1a {   
    position:absolute;   
    top:0;   
    rightright:0;   
    width:200px;   
   }   
   #p-1b {   
    position:absolute;   
    top:0;   
    left:0;   
    width:200px;   
   }

Positioning usage of position attribute in CSS

这样的制作只是用来说明absolute的作用,如果只能实现上面的效果,可能在实际制作中并不完美,为了让其更完美一些,在这个基础上我们在来看下面这一步。

第六步:设置固定高度

为了让布局更适用一些,可以在p-1元素上设置固定高度,如:

#p-1 {   
    position:relative;   
    height:250px;   
   }   
   #p-1a {   
    position:absolute;   
    top:0;   
    rightright:0;   
    width:200px;   
   }   
   #p-1b {   
    position:absolute;   
    top:0;   
    left:0;   
    width:200px;   
   }

Positioning usage of position attribute in CSS

相比之下好一点,但我们并不知道元素内容高度将会是多少,所以在此设置一个固定高度也是我们实际中的一个死穴,个人不建议这样使用。如果为了需要,我们可以通过别的办法来实现。

第七步:float

前两步,使用绝对定位都并不是很理想,那么我们可以考虑使用float来解决。我们可以在一个元素上使用float,让元素向左或向右,而且还可以使用文本围绕在这个元素的周边(这个作用在文本围绕图片特别有用)。下面来模拟一下:

#p-1a {   
    float:left;   
    width:200px;   
   }

Positioning usage of position attribute in CSS

第八步:多列浮动

上面展示的是一个列浮动,接下来看看多列的变化:

#p-1a {   
    float:left;   
    width:150px;   
   }   
   #p-1b {   
    float:left;   
    width:150px;   
   }

Positioning usage of position attribute in CSS

浮动与绝对定位来相比,现在解决了其高度自适应的问题,但也存在一个问题,浮动也破坏了元素当初的文档流,使其父元素塌陷了,那么为了解决这个问题,我们有必要对其进行清除浮动。

第九步:清除浮动

为了让浮动元素的父元素不在处于塌陷状态下,我们需要对浮动元素进行清除浮动:

#p-1a {   
    float:left;   
    width:190px;   
   }   
   #p-1b {   
    float:left;   
    width:190px;   
   }   
   #p-1c {   
    clear:both;   
   }


Positioning usage of position attribute in CSS

更多Positioning usage of position attribute in CSS相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn