一、static定位

HTML 元素的默认值,即没有定位,遵循正常的文档流对象。

静态定位的元素不会受到 top, bottom, left, right影响。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
} .c1 {
width: 100px;
height: 100px;
background: red;
position: static;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div class="c1"></div>
</body>
</html>

  

二、relative定位

相对定位元素的定位是相对其左上顶点的正常位置进行定位,定位后元素还会占据原来的空间。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
} .c1,.c2,.c3 {
width: 100px;
height: 100px;
} .c1 {
background: red;
position: relative;
left: 100px;
} .c2 {
background: green;
} .c3 {
background: blue;
}
</style>
</head>
<body>
<div class="c1"></div>
<div class="c2"></div>
<div class="c3"></div>
</body>
</html>

 

三、absolute定位

定位后,元素脱离文档流,不再占据原来的空间。

绝对定位的元素的位置相对于最近的已定位祖先元素,如果元素没有已定位的祖先元素,那么它的位置相对于<html>

1.父元素设置为relative定位,子元素设置absolute定位,相对其父元素进行定位。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
      * {
        margin: 0;
        padding: 0;
       }       .c1 {
        width: 300px;
        height: 300px;
        background: red;
       }       .c2 {
        width: 200px;
        height: 200px;
        background: green;
        position: relative;
       }       .c3 {
        width: 100px;
        height: 100px;
        background: blue;
        position: absolute;
        bottom: 0;
        right: 0;
       }
    </style>
  </head>
  <body>
    <div class="c1">
      <div class="c2">
        <div class="c3"></div>
      </div>
    </div>
  </body>
</html>

  

2.祖先元素设置为relative定位,后代元素设置absolute定位,相对其祖先元素进行定位。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
} .c1 {
width: 300px;
height: 300px;
background: red;
position: relative;
} .c2 {
width: 200px;
height: 200px;
background: green;
} .c3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1">
<div class="c2">
<div class="c3"></div>
</div>
</div>
</body>
</html>

  

3.元素没有已定位的祖先元素,它的位置相对于<html>。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
/*这里不清除body的外边距,是为了能够区分html和body元素*/
     html,body {
       width:100%;
       height:100%;
     } html {
border: 3px solid yellow;
} body {
border: 3px solid purple;
} .c1 {
width: 300px;
height: 300px;
background: red;
} .c2 {
width: 200px;
height: 200px;
background: green;
} .c3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1">
<div class="c2">
<div class="c3"></div>
</div>
</div>
</body>
</html>

  

四、fixed定位

元素的位置相对于浏览器窗口是固定位置,即使窗口是滚动的它也不会移动。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.c1 {
width: 100px;
height: 100px;
background: red;
position: fixed;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1"></div>
  <!--这里用br元素产生滚动条-->
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</body>
</html>

CSS的四种定位的参照物的更多相关文章

  1. CSS的4种定位方式比较

    CSS有四种定位(Positioning)方法:Static,Relative, Absolute和Fixed 元素flow, overlap 相对参照物: 能否用offset( top, left, ...

  2. CSS四种定位及应用

    定位(position) 如果,说浮动, 关键在一个 “浮” 字上面, 那么 我们的定位,关键在于一个 “位” 上. PS: 定位是我们CSS算是数一数二难点的了,但是,你务必要学好它,我们CSS离不 ...

  3. 页面中CSS的四种引入方式的介绍与比较

    转自:https://blog.csdn.net/qq_38689666/article/details/79039392 一:行内式 <p style="color:red" ...

  4. 《网页设计基础——CSS的四种引入方式详解》

    网页设计基础--CSS的四种引入方式详解     一.行内式:   规则: 1. 行内式是所有样式方法中最为直接的一种,它直接对HTML的标记使用style属性,然后将CSS代码直接写在其中.   格 ...

  5. CSS的四种引入方式

    1.使用link标签引入css文件: <head> <link rel="stylesheet" type="text/css" href=& ...

  6. CSS的五种定位方式

    CSS中一共有五种定位: position:static:默认值 position:absolute:绝对定位 position:relative:相对对定位 position:fixed:固定定位 ...

  7. HTML中引入CSS的四种常用方法及各自的缺点

    在HTML中引入CSS的方法主要有四种,它们分别是行内式.内嵌式.链接式和导入式. 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用.格式如下: ...

  8. sess文件编译输出css的四种方式以及使用

    sess文件输出css有下面四种方式: :nested(嵌套) :compact(紧凑) :expanded(展开) :compressed(压缩) 如何使用: sass --watch style. ...

  9. 有关于css的四种布局

    四种布局 (1).左右两侧,左侧固定宽度200px, 右侧自适应占满. (2).左中右三列,左右个200px固定,中间自适应占满. (3).上中下三行,头部200px高,底部200px高,中间自适应占 ...

随机推荐

  1. 【Spark机器学习速成宝典】模型篇03线性回归【LR】(Python版)

    目录 线性回归原理 线性回归代码(Spark Python) 线性回归原理 详见博文:http://www.cnblogs.com/itmorn/p/7873083.html 返回目录 线性回归代码( ...

  2. java导入导出下载Excel,xls文件(带下拉框)

    /** * 导入excel文件 * 2014-7-23 * @return */ @RequiresPermissions("plug:product:caiwu:upload") ...

  3. Vs code工具汉化

    官网为:https://code.visualstudio.com/ 看到中间有一些提示的命令 选择第一条,即Ctrl+shift+P,弹出命令行,选择"Configure Display ...

  4. Pycharm 常用快捷键-Windows版

    常用快捷键: Ctrl + / 行注释(可选中多行) Ctrl + Alt + L 代码格式化 Tab / Shift + Tab 缩进.不缩进当前行(可选中多行) Ctrl + D 复制选定的区域 ...

  5. Spliterator介绍

    一.Spliterator Spliterator<T> trySplit();trySplit接口就相当于我们之前写的AccumulatorRecursiveTask类里面的分割任务,如 ...

  6. dcef3 基本使用经验总结

    dcef3 基本使用经验总结 https://www.cnblogs.com/h2285409/p/10517483.html 加载命令启动参数    --enable-system-flash 会加 ...

  7. IDEA/Git 提交/commit 忽略 文件夹

    commit的时候.idea文件夹被默认选中了,如果忘记点掉就会被提交上去,想要默认忽略其实很简单. 找到项目根目录处的.gitignore文件(如果是用git版本控制的话) 双击打开之后  我们在最 ...

  8. 微信小程序的配置详解

    1.配置详解: 使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 1>pages 接受一个数组,每一项都是字符串,来指定小 ...

  9. Private Variable

    Any variable defined inside a function is considered private since it is inaccessable outside that f ...

  10. pycryptodom的源码安装

    1.去网站https://pypi.python.org/pypi/pycryptodome/#downloads下载 2.python setup.py build -> python set ...