zoukankan      html  css  js  c++  java
  • CSS定位

    通过使用 position 属性,我们可以选择 4 种不同类型的定位,这会影响元素框生成的方式。

    position 属性值的含义:

    static
        元素框正常生成。块级元素生成一个矩形框,作为文档流的一部分,行内元素则会创建一个或多个行框,置于其父元素中。
    relative
        元素框偏移某个距离。元素仍保持其未定位前的形状,它原本所占的空间仍保留。
    absolute
        元素框从文档流完全删除,并相对于其包含块定位。包含块可能是文档中的另一个元素或者是初始包含块。元素原先在正常文档流中所占的空间会关闭,就好像元素原来不存在一样。元素定位后生成一个块级框,而不论原来它在正常流中生成何种类型的框。
    fixed
        元素框的表现类似于将 position 设置为 absolute,不过其包含块是视窗本身。

    提示:相对定位实际上被看作普通流定位模型的一部分,因为元素的位置相对于它在普通流中的位置。



    1.设置为相对定位的元素框会偏移某个距离。元素仍然保持其未定位前的形状,它原本所占的空间仍保留。
    相对定位是一个非常容易掌握的概念。如果对一个元素进行相对定位,它将出现在它所在的位置上。然后,
    可以通过设置垂直或水平位置,让这个元素“相对于”它的起点进行移动。
    如果将 top 设置为 20px,那么框将在原位置顶部下面 20 像素的地方。如果 left 设置为 30 像素,那
    么会在元素左边创建 30 像素的空间,也就是将元素向右移动。
    注意,在使用相对定位时,无论是否进行移动,元素仍然占据原来的空间。因此,移动元素会导致它覆盖其它框。

    如果top和bottom一同存在的话,那么只有top生效。
    如果left和right一同存在的话,那么只有left生效。

    相对定位实例:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <div style="background: #00a0e9; 500px;height: 200px;">
        <div style="position:relative;top: 20px;left: 30px;background: #dddddd; 100px;height:100px;"></div>
    </div>
    <div style="background: #00ad36; 500px;height: 200px;">
        <div style="position:relative;bottom: 20px;right: -400px;background: #dddddd; 100px;height:100px;"></div>
    </div>
    <div style="background: #0000cc; 500px;height: 200px;">
        <div style="position:relative;top: 20px;left: 30px;bottom: 20px;right: -400px;background: #dddddd; 100px;height:100px;"></div>
    </div>
    </body>
    </html>



    2.设置为绝对定位的元素框从文档流完全删除,并相对于其包含块定位,包含块可能是文档中的另一个元素
    或者是初始包含块。元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样。元素定
    位后生成一个块级框,而不论原来它在正常流中生成何种类型的框。

    绝对定位使元素的位置与文档流无关,因此不占据空间。这一点与相对定位不同,相对定位实际上被看作
    普通流定位模型的一部分,因为元素的位置相对于它在普通流中的位置。

    普通流中其它元素的布局就像绝对定位的元素不存在一样

    绝对定位的元素的位置相对于最近的已定位祖先元素,如果元素没有已定位的祖先元素,那么它的位置相对于最初的包含块。

    对于定位的主要问题是要记住每种定位的意义。所以,现在让我们复习一下学过的知识吧:相对定位是
    “相对于”元素在文档中的初始位置,而绝对定位是“相对于”最近的已定位祖先元素,如果不存在已定位

    提示:因为绝对定位的框与文档流无关,所以它们可以覆盖页面上的其它元素。可以通过设置 z-index
    属性来控制这些框的堆放次序。的祖先元素,那么“相对于”最初的包含块。

    绝对定位实例:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <div style="position: relative;background: #00a0e9; 500px;height: 200px;">
        <div style="position:absolute;top: 50px;left: 30px;background: #dddddd; 100px;height:100px;"></div>
    </div>
    <div style="position: relative;background: #00ad36; 500px;height: 200px;">
        <div style="position:absolute;bottom: 20px;right: 20px;background: #dddddd; 100px;height:100px;"></div>
    </div>
    <div style="position: relative;background: #0000cc; 500px;height: 200px;">
        <div style="position:absolute;top: 50px;left: 30px;bottom: 20px;right: -400px;background: #dddddd; 100px;height:100px;"></div>
    </div>
    </body>
    </html>



    3.浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
    由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。

    浮动定位实例:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <div style="background: #00a0e9; 500px;height: 400px;">
        <div style="float: right;background: #dddddd; 100px;height:100px;">1</div>
        <div style="background: #00ff00; 100px;height:100px;">2</div>
        <div style="background: #3c763d; 100px;height:100px;">3</div>
    </div>
    <div style="background: #00AAAA; 500px;height: 400px;">
        <div style="float: left;background: #dddddd; 100px;height:50px;">1</div>
        <div style="background: #00ff00; 100px;height:100px;">2</div>
        <div style="background: #3c763d; 100px;height:100px;">3</div>
    </div>
    <div style="background: #1c3b7c; 500px;height: 400px;">
        <div style="float: left;background: #dddddd; 100px;height:100px;">1</div>
        <div style="float: left;background: #00ff00; 100px;height:100px;">2</div>
        <div style="float: left;background: #3c763d; 100px;height:100px;">3</div>
    </div>
    <div style="background: #00cc66; 250px;height: 400px;">
        <div style="float: left;background: #dddddd; 100px;height:100px;">1</div>
        <div style="float: left;background: #00ff00; 100px;height:100px;">2</div>
        <div style="float: left;background: #3c763d; 100px;height:100px;">3</div>
    </div>
    <div style="background: #1c3b7c; 250px;height: 400px;">
        <div style="float: left;background: #dddddd; 100px;height:150px;">1</div>
        <div style="float: left;background: #00ff00; 100px;height:100px;">2</div>
        <div style="float: left;background: #3c763d; 100px;height:100px;">3</div>
    </div>
    </body>
    </html>




    参考链接:http://www.w3school.com.cn/css/css_positioning.asp


  • 相关阅读:
    汉字词组换行
    C#中获取Excel文件的第一个表名
    SQL查找某一条记录的方法
    C#数据库连接字符大全
    整理的asp.net资料!(不得不收藏)
    母版页的优点,及母版页与内容页中相互访问方法
    13范式
    使用 Jackson 树连接线形状
    word2007,取消显示回车符
    三张表之间相互的多对多关系
  • 原文地址:https://www.cnblogs.com/xutongbao/p/9924979.html
Copyright © 2011-2022 走看看