Home > Web Front-end > CSS Tutorial > css positioning

css positioning

高洛峰
Release: 2016-10-09 15:22:08
Original
1322 people have browsed it

There are three types of positioning, namely relative positioning: position:relative;, absolute positioning: position:absolute;, fixed positioning: fixed;

Relative positioning

Relative positioning is to fine-tune the position of the element, so that the element is relative to its original position. Make position adjustments. In other words, if a box wants to adjust its position, then it must use relative positioning

css positioning


without going off the mark. The original position is still occupied and separated

relative positioning without going off the mark. The real position is In its original position, it's just that the shadow has gone out and can float everywhere.

css positioning

Usage of relative positioning

There are pitfalls in relative positioning, so it is generally not used for the "capping" effect. Page, the effect is minimal. Just two functions:

1) Fine-tuning elements

2) As a reference for absolute positioning, you can use left and right to describe the movement of the right and left of the box; you can use top and bottom to describe the movement of the box Movement down and up.

css positioningHow to implement the above picture:

方法1:
position:relative;
top:100px;
left:200px;

方法2:
position:relative;
bottom:-100px;
right:-200px;

方法3:
position:relative;
top:100px;
right:-200px;

方法4:
position:relative;
bottom:-100px;
left:200px;
Copy after login

Absolute positioning

Absolute positioning is more flexible than relative positioning

css positioningAbsolute positioning is out of standard

Absolutely positioned boxes are out of standard document flow. Therefore, all the properties of standard document flows are no longer followed after absolute positioning. After absolute positioning, the label does not distinguish between so-called inline elements and block-level elements. You can set the width and height without display:block;.

Reference point

The reference point of absolute positioning. If it is described by top, then the positioning reference point is the upper left corner of the page, not the upper left corner of the browser:

css positioningIf it is described by bottom, then it is the top left corner of the browser. Screen window size, corresponding to the lower left corner of the page:

css positioning Taking the box as the reference point

An absolutely positioned element. If there is an element that is also positioned in the parent element, then the parent element will be used as the reference point.

css positioningIf you want to listen to the nearest ancestor element that has been positioned, it may not be the father, it may be the grandfather:

<div class="box1">      →  相对定位
        <div class="box2">  →  没有定位
            <p></p>         → 绝对定位,将以box1为参考,因为box2没有定位,box1就是最近的父辈元素
        </div>
    </div>

    <div class="box1">      →  相对定位
        <div class="box2">  → 相对定位
            <p></p>         → 绝对定位,将以box2为参考,因为box2是自己最近的父辈元素
        </div>
    </div>
Copy after login

Not necessarily relative positioning, any positioning can be used as a reference point

<div>  → 绝对定位        <p></p>  → 绝对定位,将以div作为参考点。因为父亲定位了。</div>
Copy after login

子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。

<div class=”box1”>          → 绝对定位
    <div class=”box2”>      → 相对定位
        <div class=”box3”>  → 没有定位
            <p></p>         → 绝对定位,以box2为参考定位。
        </div>
    </div>
</div>
Copy after login

绝对定位的儿子,无视参考的那个盒子的padding。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
            border: 10px solid red;
            padding: 100px;
            padding-top: 150px;
            position: relative;
            margin: 100px;
        }
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            top: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div>
        字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字
        <p></p>
    </div>
</body>
</html>
Copy after login

css positioning

绝对定位的盒子居中

绝对定位之后,所有标准流的规则,都不适用了。所以 margin:0 auto; 失效。绝对定位的盒子居中,只需 left:50%; margin-left: 负的宽度的一半。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 400px;
            height: 60px;
            background-color: green;
            position: absolute;
            left: 50%;
            margin-left: -200px;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>
Copy after login

固定定位

固定定位,就是相对浏览器窗口定位。页面如何滚动,这个盒子显示的位置不变,固定定位脱标。

css positioning

z-index

z-index值表示谁压着谁。数值大的压盖住数值小的。

1)只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。

2) z-index值没有单位,就是一个正整数。默认的z-index值是0。

3) 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。

4)父元素的z-index小了,子元素设置的z-index再大也没用。

css positioning

css positioning

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template