当前位置: 首页 > news >正文

网站免费制作平台百度手游排行榜

网站免费制作平台,百度手游排行榜,企业网站备案要钱吗,郑州网站建设的软件文章目录 定位概述偏移量(offset)坐标及方向相对定位——position绝对定位——absolute固定定位 ——fixed粘滞定位——sticky绝对定位对布局的影响水平布局:垂直方向 定位概述 定位(position) 定位是一种更加高级的布局手段 通过定位可以将…

文章目录

  • 定位概述
  • 偏移量(offset)
  • 坐标及方向
  • 相对定位——position
  • 绝对定位——absolute
  • 固定定位 ——fixed
  • 粘滞定位——sticky
  • 绝对定位对布局的影响
    • 水平布局:
    • 垂直方向

定位概述

  • 定位(position)
    定位是一种更加高级的布局手段
    通过定位可以将元素摆放到页面的任意位置,而且不会对其他元素产生影响。
  • 使用position属性来设置定位
    可选值:
    static 默认值,元素是静止的没有开启定位
    relative 开启元素的相对定位
    absolute 开启元素的绝对定位
    fixed 开启元素的固定定位
    sticky开启元素的粘滞定位

偏移量(offset)

当元素开启了定位以后,可以通过偏移量来设置元素的位置

  • top:定位元素和定位位置上边的距离
  • bottom:定位元素和定位位置下边的距离
    定位元素垂直方向的位置由top和bottom两个属性来控制,通常情况下我们只会使用其中一个
    top值越大,定位元素越向下移动
    bottom值越大,定位元素越向上移动
  • left:定位元素和定位位置的左侧距离
  • right:定位元素和定位位置的右侧距离
    定位元素水平方向的位置由left和right两个属性控制
    通常情况下只会使用一个
    left越大元素越靠右
    right越大元素越靠左

坐标及方向

在页面中坐标的样式:
在这里插入图片描述

相对定位——position

相对定位:
当元素的position属性值设置为relative时则开启了元素的相对定位

  • 相对定位的特点:
    1.元素开启相对定位以后,如果不设置偏移量元素不会发生任何的变化
    2.相对定位是参照于元素在文档流中的位置进行定位的
    3.相对定位会提升元素的层级
    4.相对定位不会使元素脱离文档流
    5.相对定位不会改变元素的性质块还是块,行内还是行内

eg:元素开启相对定位以后,如果不设置偏移量元素不会发生任何的变化

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><style>.box1,.box2,.box3{width: 100px;height: 100px;}.box1{background-color: teal;}.box2{background-color: tomato;position: relative;}.box3{background-color:rgb(236, 233, 61);}</style></head>
<body><div class="box1"></div><div class="box2"></div><div class="box3"></div></body>
</html>

在这里插入图片描述
eg:相对定位是参照于元素自己在文档流中的位置进行定位的

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><style>.box1,.box2,.box3{width: 100px;height: 100px;}.box1{background-color: teal;}.box2{background-color: tomato;position: relative;left: 100px;top: -100px}.box3{background-color:rgb(236, 233, 61);}</style></head>
<body><div class="box1"></div><div class="box2"></div><div class="box3"></div></body>
</html>

在这里插入图片描述

eg:提升元素的层级

   .box2{background-color: tomato;position: relative;left: 50px;top: -100px}

在这里插入图片描述

绝对定位——absolute

  • 当元素的position属性值设置为absolute时,则开启了元素的绝对定位
  • 绝对定位的特点:
    1.开启绝对定位后,如果不设置偏移量元素的位置不会发生变化
    2.开启绝对定位后,元素会从文档流中脱离
    3.行内元素:行内元素脱离文档流以后会变成块元素,特点和块元素一样。
    4.块元素:块元素不再独占页面的一行;如果没有设置宽度和高度,脱离文档流以后,块元素的宽度和高度默认都被内容撑开;如果设置了宽度和高度,就按设置的来。
    5.绝对定位会使元素提升一个层级
    6.绝对定位元素是相对于其包含块进行定位的
    包含块( containing block ):
  • 正常情况下:
    包含块就是离当前元素最近的祖先块元素
    <div> <div></div> </div>div的包含块就是div
    <div><span><em>hello</em></span></div>em的包含块也是div,因为span是行内元素
  • 绝对定位的包含块:
    包含块就是离它最近的开启了定位(即position不是static即可)的祖先元素,
    如果所有的祖先元素都没有开启定位,则根元素(html)就是它的包含块
  • 初始包含块:html(根元素)
    eg:
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><style>body{font-size: 50px;}.box0{width: 100px;height: 100px;background-color: rgb(52, 78, 102);}.box1 {width: 400px;height: 400px;background-color: teal;}.box2 {width: 100px;height: 100px;background-color: tomato;position:absolute;left: 0px;top: 0px;}.box3 {width: 200px;height: 200px;background-color: rgb(236, 233, 61);}</style></head><body><div class="box0">0</div><div class="box1">1<div class="box3">3<div class="box2">2</div></div></div></body>
</html>

输出:
在这里插入图片描述
如果开启2的定位:

      .box1 {position: relative;width: 400px;height: 400px;background-color: teal;}

输出:
在这里插入图片描述

如果开启3的定位:

.box3 {position: relative;width: 200px;height: 200px;background-color: rgb(236, 233, 61);}

在这里插入图片描述

固定定位 ——fixed

固定定位:

  • 将元素的position属性设置为fixed则开启了元素的固定定位
  • 固定定位也是一种绝对定位(absoult),所以固定定位的大部分特点都和绝对定位一样:
    1.开启固定定位后,如果不设置偏移量元素的位置不会发生变化
    2.开启固定定位后,元素会从文档流中脱离
    3.固定定位会改变元素的性质,行内变成块,块的宽高被内容撑开
    (也就是脱离文档流的特点)
    4.固定定位会使元素提升一个层级
    5.唯一不同的是固定定位永远参照于浏览器的视口(即窗口)进行定位。

即当滚动条滚动时,固定定位的元素的位置不会改变
eg:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><style>body{font-size: 50px;height: 1500px;}.box0{width: 100px;height: 100px;background-color: rgb(52, 78, 102);}.box1 {position: relative;width: 400px;height: 400px;background-color: teal;}.box2 {width: 100px;height: 100px;background-color: tomato;position:fixed;left: 0px;top: 0px;}.box3 {width: 200px;height: 200px;background-color: rgb(236, 233, 61);}</style></head><body><div class="box0">0</div><div class="box1">1<div class="box3">3<div class="box2">2</div></div></div></body>
</html>

在这里插入图片描述

粘滞定位——sticky

该属性,浏览器的支持度不高
粘滞定位

  • 当元素的position属性设置为sticky时则开启了元素的粘滞定位
  • 粘滞定位和相对定位的特点基本一致
    1.元素开启相对定位以后,如果不设置偏移量元素不会发生任何的变化
    2.相对定位会提升元素的层级
    3.相对定位不会使元素脱离文档流
    4.相对定位不会改变元素的性质块还是块,行内还是行内

不同的是:

  1. 粘滞定位可以在元素到达某个位置时将其固定
    2.粘滞定位是参照于body进行定位的
    eg:
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><link rel="stylesheet" href="../reset.css" /><style>.nav{/* width: 1246px; */width: 1250px;height: 50px;background-color: rgb(231,231,226);margin: 50px auto;position: sticky;top:100px;}.nav li{height:50px;float: left;/* 设置height=line-height可以使元素垂直居中 */line-height: 48px;}.nav a{display: block;color: slategray;text-decoration: none;font-size: 18px;padding: 0 42px;}.nav li:last-child a{/* 这样写或者将nav的宽度修改为 width: 1246px; */padding: 0 44px 0 44px;}.nav a:hover{background-color: #3f3f3f;color: white;}.box1{width: 200px;height: 1000px;background-color: darksalmon;}body{height: 2000px;}</style></head><body><ul class="nav"><li><a href="#">HTML/CSS</a></li><li><a href="#">Browser Side</a></li><li><a href="#">Server Side</a></li><li><a href="#">Programming</a></li><li><a href="#">XML</a></li><li><a href="#">Web Building</a></li><li><a href="#">Reference</a></li></ul><div class="box1"></div></body>
</html>

在这里插入图片描述

绝对定位对布局的影响

也是相对于自己的包含快进行分居中等位置操作

水平布局:

当我们开启了绝对定位后,水平方向的布局等式就需要添加left和right两个值,此时规则和之前一样只是多添加了两个值:
left + margin-left + border-left + padding-left + width + padding-rigth + border-right + margin-right + right = 包含块的内容宽度

当发生过度约束:

  • 如果9个值中没有auto 则自动调整right值以使等式满足
    如果有auto,则自动调整auto的值以使等式满足
  • 可设置auto的值
    margin width left right
  • 因为left和right的值默认是auto,所以如果不指定left和right,则等式不满足时,会自动调整这两个值(着重调整right)
    eg:
<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><style>.box1{width: 500px;height: 500px;background-color: darksalmon;position: relative;}.box2{position: absolute;width: 100px;height: 100px;background-color: darkseagreen;/* 写不写都一样,因为默认值就是auto */left: auto;right: auto;}</style>
</head>
<body><div class="box1"><div class="box2"></div></div> 
</body>
</html>

在这里插入图片描述
如果:width,right,left都为auto时就不会显示了

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><style>body{background-color: darksalmon;}.box1{width: 500px;height: 500px;background-color:rgba(253, 253, 253, 0.288);position: relative;}.box2{position: absolute;/* 写不写都一样,因为默认值就是auto */width: auto;height: 100px;background-color: darkseagreen;/* 写不写都一样,因为默认值就是auto */left: auto;right: auto;}</style>
</head>
<body><div class="box1"><div class="box2"></div></div> 
</body>
</html>

输出:
在这里插入图片描述

水平居中方式:
要先指定 width,并且设置right = left = 0,水平居中才有效果

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><style>body{background-color: darksalmon;}.box1{width: 500px;height: 500px;background-color:rgba(253, 253, 253, 0.288);position: relative;}.box2{position: absolute; width: 100px;height: 100px;background-color: darkseagreen;left: 0;right: 0;margin-left: auto;margin-right: auto;}</style>
</head>
<body><div class="box1"><div class="box2"></div></div> 
</body>
</html>

输出:
在这里插入图片描述

垂直方向

垂直方向布局的等式的也必须要满足
top + margin-top/bottom + padding-top/bottom + border-top/border-bottom + height + bottom = 包含块的内容高度
所以可以利用这一特性,使元素垂直居中:

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><style>body{background-color: darksalmon;}.box1{width: 500px;height: 500px;background-color:rgba(253, 253, 253, 0.288);position: relative;}.box2{position: absolute; width: 100px;height: 100px;background-color: darkseagreen;top: 0;bottom: 0;margin-top: auto;margin-bottom: auto;}</style>
</head>
<body><div class="box1"><div class="box2"></div></div> 
</body>
</html>

在这里插入图片描述
所以利用绝对定位可以使元素垂直水平居中

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><style>body{background-color: darksalmon;}.box1{width: 500px;height: 500px;background-color:rgba(253, 253, 253, 0.288);position: relative;}.box2{position: absolute; width: 100px;height: 100px;background-color: darkseagreen;top: 0;bottom: 0;left: 0;right: 0;margin: auto;}</style>
</head>
<body><div class="box1"><div class="box2"></div></div> 
</body>
</html>

在这里插入图片描述

http://www.yayakq.cn/news/623324/

相关文章:

  • 傻瓜一键自助建站系统2021年不付费黄台软件下载
  • 如何利用路由建设网站简单网页制作
  • 销售网站平台怎么做的网页版视频怎么下载
  • 网站建设+荆州企业外包
  • 佛山专业的做网站python文章发布wordpress
  • 手机可以搭建网站吗上海百度首页优化
  • 天水网站建设博客网站介绍视频怎么做
  • 做网站视频网站韩国做网站网站违法不
  • 使用unity做网站网站建设都有哪些书
  • 科技让生活更美好500字六年级seo优化是怎么优化的
  • 网站是陕西省城乡建设综合服务中心网页版式设计欣赏
  • 做卡盟网站教程什么是营销网络
  • 达州 网站建设wordpress批量提交rss
  • ps做好切片后怎么做网站程序编程软件有哪些
  • 哪个网站做设计可以挣钱微网站的好处
  • 个人asp网站模板下载做网站的得花多少钱
  • 厦门企业做网站2345浏览器网址大全
  • 做网站怎么盈利泰安企业公司
  • 公交公司网站建设的意义展示型网站制作服务
  • 网站排名在哪里优化公司网站建设应包含哪几个板块
  • win8风格门户网站怎么查公司的邮箱
  • 设计营销型网站域名珠海城乡建设网站
  • 手机上自己做网站点商城域名注册多少钱
  • 企业网站如何建设和推广做h5网站制作
  • 青岛做网站推广公司哪家好企业网站制作设计公司
  • 太原做企业网站的wordpress更改登录地址
  • 东莞企业网站搭建制作取消网站备案号
  • 永久免费的建站系统有哪些碧桂园事件全过程
  • 两学一做考试网站保险网站建设公司
  • 济南做网站公司排名直通车官网