定位

定位

position属性

static:默认值,没有定位

relative:相对定位

absolute:绝对定位

fixed:固定定位

标准文档流

标准文档流:是指页面上从上到下,从左到右,网页元素一个挨一个的简单的正常的布局方式。

一般在HTML元素分为两种:块级元素和行内元素。像div,p这些的元素属于块级元素,块级元素是从上到下一行一行的排列;默认一个块级元素会占用一行,而跟在后面的元素会另起一行排列;

行内元素是在一行中水平布置,从左到右的排列;span,strong等属于行内元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div style="background-color:dodgerblue">我是块级元素,我单独占一行 我单独占一行 我单独占一行</div>
<div style="background-color:yellow">我是块级元素,我一行一行的排列,我一行一行的排列,我一行一行的排列</div>
<span style="background-color:green">我的行内元素,我水平的排列,我水平的排,我水平的排,我水平的排列,我水平的排列</span>
<span style="background-color:gray">我是行内元素,没有那么霸道,没有那么霸道,没有那么霸道,没有那么霸道,没有那么霸道</span>
</body>
</html>
</body>
</html>

static定位

position:static

元素没有定位,以标准流方式显示

<!DOCTYPE html >
<head>
<meta charset="UTF-8>
<title>position属性</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style>
div {
    width: 300px;
    margin:10px;
    padding:5px;
    font-size:12px;
    line-height:25px;
}
#father {
    width: 500px;
    margin: 50px auto;
    border:1px #666 solid;
    padding:10px;
}
#first {
    background-color:#FC9;
    border:1px #B55A00 dashed;
}
#second {
    background-color:#CCF;
    border:1px #0000A8 dashed;
}
#third {
    background-color:#C5DECC;
    border:1px #395E4F dashed;
}
</style>
</head>
<body>
<div id="father">
  <div id="first">第一个盒子</div>
  <div id="second">第二个盒子</div>
  <div id="third">第三个盒子</div>
</div>
</body>
</html>

效果图


相对定位

relative属性值

相对自身原来位置进行偏移

偏移设置:top、left、right、bottom
可以用left来描述盒子向右移动;
可以用right来描述盒子向左的移动;
可以用top来描述盒子向下的移动;
可以用bottom来描述盒子的向上的移动;
如果是负数就是相反的方向。

相对定位的盒子,不脱离标准流,老家保留位置,其后的元素不能占用其原有位置。

相对定位的主要用途是作为其内部元素绝对定位时的参照标准,有相对于我之义。

<!DOCTYPE html >
<head>
<meta charset="UTF-8>
<title>position属性</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style>
div {
    width: 300px;
    margin:10px;
    padding:5px;
    font-size:12px;
    line-height:25px;
}
#father {
    width: 500px;
    margin: 50px auto;
    border:1px #666 solid;
    padding:10px;
}
#first {
    background-color:#FC9;
    border:1px #B55A00 dashed;
    position:relative;
    top:10px;
    left:50px;
}
#second {
    background-color:#CCF;
    border:1px #0000A8 dashed;
}
#third {
    background-color:#C5DECC;
    border:1px #395E4F dashed;
}
</style>
</head>
<body>
<div id="father">
  <div id="first">第一个盒子</div>
  <div id="second">第二个盒子</div>
  <div id="third">第三个盒子</div>
</div>
</body>
</html>
11.jpg

**绝对定位
absolute属性值
偏移设置: left、right、top、bottom

使用了绝对定位的元素以它最近的一个****“已经定位”“祖先元素” 为基准进行偏移。如果没有已经定位的祖先元素,那么会以浏览器窗口为基准进行定位。绝对定位的元素从标准文档流中脱离,其后的元素会占据其原有的位置。

<!DOCTYPE html >
<head>
    <meta charset="UTF-8">
    <title>position属性</title>
    <link rel=" stylesheet" href="resert.css">
    <style>
        div {
            width: 300px;
            margin:10px;
            padding:5px;
            font-size:12px;
            line-height:25px;
        }
        #father {
            width: 500px;
            margin: 50px auto;
            border:1px #666 solid;
            padding:10px;
            position: relative;
        }
        #first {
            background-color:#FC9;
            border:1px #B55A00 dashed;
            position: absolute;
            top:10px;
            right: 10px;
        }
        #second {
            background-color:#CCF;
            border:1px #0000A8 dashed;
        }
        #third {
            background-color:#C5DECC;
            border:1px #395E4F dashed;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>
</div>
</body>
</html>

效果图


22.jpg

练习 微信消息提示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>weixin</title>
    <style>
        .father
        {
            width: 200px;
            height: 200px;
            border: 1px solid green;
            position: relative;
        }
        .circle
        {
            width: 30px;
            height: 30px;
            background-color: red;
            border-radius: 50%;
            line-height: 30px;
            text-align: center;
            position: absolute;
            right: -15px;
            top:-15px;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="circle">2</div>
</div>
</body>
</html>

border-radius: 50%; 确实宽和高的正方形可以用这种写法变成圆
z-index属性

调整元素定位时重叠层的上下位置

z-index属性值:整数,默认值为0
设置了positon属性时,z-index属性可以设置各元素之间的重叠高低关系
z-index值大的层位于其值小的层上方


网页元素透明度

CSS设置元素透明度
opacity:x
x值为0~1,值越小越透明
opacity:0.4;
在IE浏览器的书写方式:
filter:alpha(opacity=x)
x值为0~100,值越小越透明
filter:alpha(opacity=40);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,687评论 1 92
  • 一、文档流的概念指什么?有哪种方式可以让元素脱离文档流? 1、文档流指的是元素在排列布局中所占用的位置,具体的说是...
    鸿鹄飞天阅读 4,181评论 0 0
  • relative:生成相对定位的元素,通过top,bottom,left,right的位置相对于其正常位置进行定位...
    zx9426阅读 4,567评论 0 2
  • CSS 定位 CSS有三种基本的定位机制:普通流,浮动,绝对定位(absolute, fixed):普通流是默认定...
    _空空阅读 11,087评论 0 15
  • 1.文档流的概念指什么?有哪种方式可以让元素脱离文档流? 文档流也叫常规流,其实就是文档的读取和输出顺序,也就是我...
    墨月千楼阅读 4,227评论 0 0

友情链接更多精彩内容