以下这一段程序码会长出不同大小及颜色的四块积木
...
<div class="container">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
<div class="d">D</div>
</div>
...
div{
font-size: 50px;
text-align: center;
}
.a{
width: 300px;
height: 100px;
background: wheat;
}
.b{
width: 200px;
height: 100px;
background: red;
}
.c{
width: 100px;
height: 100px;
background: blue;
}
.d{
width: 50px;
height: 200px;
background: blueviolet;
}
成这样的四块肌积木,我想把他全部塞在一起,
也就是让他们由相同的地方长出来
这时後就要用到一种定位(position)的技巧,绝对定位
绝对定位这名词可能会让人误以为:使用了绝对定位,就会定在浏览器上面的某一个地方,
到死元素都会定在那个地方不会动
但并不是如此,
绝对定位只是一种定位方式,要搭配相对定位点才会起作用
可以想像有一只英雄角色,他造出了一个分身,
这只分身会固定在本尊的右侧三公尺旁
当本尊往前移动一段距离时,分身也会同时跟着往前移动一段距离
那我们可以说这个分身 使用的就是绝对定位 (就是跟屁虫啦)
而本尊就是相对定位点
召唤分身 -- 跟屁虫之术
position: absolute;
设定相对定位点 (定位本尊的位置)
position: relative;
我们把父层容器设定成relative
、并把所有类别都挂上一个absolute
来看看会有什麽变化
.container{
position: relative;
}
.container{
position: relative;
}
.a{
width: 300px;
height: 100px;
background: wheat;
position: absolute;
}
.b{
width: 200px;
height: 100px;
background: red;
position: absolute;
}
.c{
width: 100px;
height: 100px;
background: blue;
position: absolute;
}
.d{
width: 50px;
height: 200px;
background: blueviolet;
position: absolute;
}
哇,这次全部的积木果然就从原点(相对定位点)的地方开始长!
那如果只放absolute
在A积木身上,并拿掉其他三个积木的absolute
,
会发生什麽事情
(由於积木B会被A盖住,我稍微调整一下B的尺寸)
.a{
width: 300px;
height: 100px;
background: wheat;
position: absolute;
}
.b{
width: 700px;
height: 300px;
background: red;
}
这次A方块从原点(最左上方)开始长、B方块也从原点(最左上方)开始长
看的出来,
原本的积木就长原本自己的、而积木A(绝对定位)就长分身的
原本的就长原本的,意思是static
就长static
自己的,而不受absolute
影响
静态定位(static)是网页预设的定位方式。
position: static;
透过rgba设定透明度的方式,让图层彼此间多少能看的见
再同时让积木A与积木B挂上absolute
.a{
width: 300px;
height: 100px;
background: rgba(245, 222, 179, 0.5);
position: absolute;
}
.b{
width: 700px;
height: 300px;
background: rgba(255, 0, 0, 0.1);
position: absolute;
}
.c{
width: 100px;
height: 100px;
background: rgba(0, 0, 255, 0.8);
}
.d{
width: 50px;
height: 200px;
background: rgba(138, 43, 226, 1);
position: static;
}
积木A、B都透过分身的方式,从原点开始长
积木C则是用原本预设static
方式,从原点开始依序排、再来才排到积木D
z-index
数字越大盖在越上层z-index
可以为负数z-index
预设值为0拿刚刚的绝对定位来展示z-index
效果说明
.container{
position: relative;
}
.a{
width: 300px;
height: 100px;
background: wheat;
position: absolute;
z-index: 1;
}
.b{
width: 200px;
height: 100px;
background: red;
position: absolute;
}
.c{
width: 100px;
height: 100px;
background: blue;
position: absolute;
}
.d{
width: 50px;
height: 200px;
background: blueviolet;
position: absolute;
}
在 display:flex
或者 display:grid
的情况下,
可以透过CSS中的order
属性来改变同层元素间DOM元素显示的先後顺序
改变元素的排列顺序,可以增加排版上的使用弹性
适合用在RWD画面缩小时、变换成手机版元件排版的顺序
(order
仅对视觉呈现的顺序产生作用,不会影响元素排列逻辑)
order
数字最小的会先显示出来、数字越大排在越後面order
可以为负数order
预设值为0
order
值相同时,会按原先DOM顺序补上...
<div class="container">
<p id="p1"> 1 lorem10(按tab)</p>
<p id="p2"> 2 lorem20(按tab)</p>
<p id="p3"> 3 lorem50(按tab)</p>
<p id="p4"> 4 lorem100(按tab)</p>
</div>
...
.container{
display: flex;
}
p:first-letter{
font-size: 50px;
}
#p1{
}
#p2{
order: 0;
}
#p3{
order: 5;
}
#p4{
order: -1;
}
<<: Day15 Sideproject(作品集) from 0 to 1 -刻画面
Azure Cosmos DB API for MongoDB- 找个地方放资料 MongoDB是一...
今天要认识的模式是Chain of Responsibility,属於Behavioral De...
全文同步於个人 Docusaurus Blog 建立完成特定频道後,再来就是在指定频道中发出个人讯...
今天要接续昨天的继续介绍基础资料型别,如下 Enum 型别 Null 型别 Undefined 型别...
各位夥伴这个热血的30天挑战又来了 不免俗的第一天开赛就是来分享一下这心路历程XDD 8月本来想说今...