iT邦幫忙

2023 iThome 鐵人賽

DAY 16
0
Modern Web

30天前端學習筆記心得系列 第 16

Day16-CSS位置浮動清除

  • 分享至 

  • xImage
  •  

位置(position)

指定元素在父元素定位方式

  • Static預設值

照原始位置顯示

.box {
    width: 250px;
    height: 250px;
}
.box1 {
    background-color: red;
    width: 100px;
    height: 100px;
    top: 60px;
    left: 40px;
    position: static;
}
.box2 {
    background-color: yellow;
    width: 100px;
    height: 100px;
    top: 50px;
    left: 50px;
}
.box3 {
    background-color: green;
    width: 100px;
    height: 100px;
    top: 70px;
    left: 30px;
}

https://ithelp.ithome.com.tw/upload/images/20231001/20163257CfVScdNSzm.png

  • fixed

將元素固定在瀏覽器視窗中,即使滾動條移動也會保持在同一位置

box {
  width: 250px;
  height: 250px;
}
.box1 {
  background-color: red;
  width: 100px;
  height: 100px;
  top: 60px;
  left: 40px;
  position: fixed;
}
.box2 {
  background-color: yellow;
  width: 100px;
  height: 100px;
  top: 50px;
  left: 50px;
}
.box3 {
  background-color: green;
  width: 100px;
  height: 100px;
  top: 70px;
  left: 30px;
}

https://ithelp.ithome.com.tw/upload/images/20231001/20163257P5Yl1nddLB.png

  • sticky

將元素固定在父元素中,滾動條移動到元素之前會固定在原始位置

.box {
  width: 250px;
  height: 250px;
}
.box1 {
  background-color: red;
  width: 100px;
  height: 100px;
  top: 60px;
  left: 40px;
}
.box2 {
  background-color: yellow;
  width: 100px;
  height: 100px;
  top: 50px;
  left: 50px;
  position: sticky;
}
.box3 {
  background-color: green;
  width: 100px;
  height: 100px;
  top: 70px;
  left: 30px;
}

https://ithelp.ithome.com.tw/upload/images/20231001/20163257ZJ5x2WUMXh.png

  • Absolute絕對位置

會相對於最近父元素或瀏覽器視窗進行定位

.box {
    width: 250px;
    height: 250px;
}
.box1 {
    background-color: red;
    width: 100px;
    height: 100px;
    top: 60px;
    left: 40px;
}
.box2 {
    background-color: yellow;
    width: 100px;
    height: 100px;
    top: 50px;
    left: 50px;
    position: absolute;
}
.box3 {
    background-color: green;
    width: 100px;
    height: 100px;
    top: 70px;
    left: 30px;
    position: absolute;
}

https://ithelp.ithome.com.tw/upload/images/20231001/20163257dwNW65Om90.png

  • Relative相對位置

會相對於原始位置進行定位

.box {
    width: 250px;
    height: 250px;
}
.box1 {
    background-color: red;
    width: 100px;
    height: 100px;
    top: 60px;
    left: 40px;
    position: relative;
}
.box2 {
    background-color: yellow;
    width: 100px;
    height: 100px;
    top: 50px;
    left: 50px;
}
.box3 {
    background-color: green;
    width: 100px;
    height: 100px;
    top: 70px;
    left: 30px;
    position: relative;
}

https://ithelp.ithome.com.tw/upload/images/20231001/20163257yv69LwKCfj.png

  • z-index

控制元素在Z軸上堆疊順序,屬性值越大,元素越靠近

.redbox {
  position: relative;
  z-index: 1;
  background-color: red;
  height: 5em;
}
.greenbox {
  position: absolute;
  z-index: 3; 
  background-color: green;
  width: 50%;
  left: 65%;
  top: 25px;
  height: 10em;
}

.yellowbox {
  position: absolute;
  z-index: 2; 
  background-color: yellow;
  width: 100%;
  left: 100px;
  top: 3em;
}

https://ithelp.ithome.com.tw/upload/images/20231001/2016325790ikMZcL5R.png

  • overflow

控制元素內容是否溢出元素邊框

  • visible(默認值):元素內容會顯示,即使溢出元素邊框
  • hidden:元素內容會被裁剪,適合元素邊框
  • scroll:元素出現一個滾軸,溢出的內容
    https://ithelp.ithome.com.tw/upload/images/20231001/20163257IDfuROX6Wk.png

浮動(float)

元素從標準中移除,懸浮在其他元素周圍

  • left(默認值):元素向左浮動
  • right:元素向右浮動
  • none:元素恢復標準中
.container {
    width: 200px;
    height: 200px;
  }
  .redbox {
    width: 50px;
    height: 50px;
    background-color: red;
    float: left;
   
  }
  .greenbox {
    width: 150px;
    height: 150px;
    background-color: green;
    float: none;
  }
  .yellowbox {
    width: 100px;
    height: 100px;
    background-color: yellow;
    float: right;
  }

https://ithelp.ithome.com.tw/upload/images/20231001/20163257JgxladEtYR.png

清除(clear)

清除浮動元素周圍元素

  • left:清除左側浮動元素
  • right:清除右側浮動元素
  • both:清除左右兩側浮動元素
  • none:不清除任何浮動元素
.container {
    width: 200px;
    height: 200px;
  } 
  .redbox {
    width: 50px;
    height: 50px;
    background-color: red;
    float: left; 
  }
  .greenbox {
    width: 150px;
    height: 150px;
    background-color: green;
    float: none;
    clear: both;
  }
  .yellowbox {
    width: 100px;
    height: 100px;
    background-color: yellow;
    float: right;
  }

https://ithelp.ithome.com.tw/upload/images/20231001/201632571U0jj6vWu5.png

  .yellowbox {
    width: 100px;
    height: 100px;
    background-color: yellow;
    float: right;
    clear: both;
  }

https://ithelp.ithome.com.tw/upload/images/20231001/20163257eBNZAsSrfk.png

資料來源:CSS語法
overflow
z-index


上一篇
Day15-CSS BOX MODEL
下一篇
Day17-CSS FLEXBOX
系列文
30天前端學習筆記心得34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言