日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一篇文章带你实操代码理解盒子模型

發(fā)布時間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一篇文章带你实操代码理解盒子模型 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關注!
歡迎關注微信公眾號:寶藏女孩的成長日記
如有轉載,請注明出處(如不注明,盜者必究)

目錄

    • 一、盒子模型的幾個關鍵詞
    • 二、盒子有尺寸,用CSS寫法為寬度(width)和高度(height)
    • 三、盒子有邊框,用CSS寫法為上下左右邊框(border)
      • 3.1邊框線型 border-style
      • 3.2邊框顏色
      • 3.3邊框的復合屬性
      • 3.4單獨控制上下左右邊框
        • 3.4.1上邊框 border-top
        • 3.4.2下邊框 border-bottom
        • 3.4.3左邊框border-left
        • 3.4.4右邊框border-right
    • 四、盒子有內邊距,用CSS寫法為上下左右內邊距(padding)
      • 4.1padding的復合屬性
      • 4.2單獨控制上下左右邊框
        • 4.2.1上邊內邊距 padding-top
        • 4.2.2底邊內邊距 padding-bottom
        • 4.2.3左邊內邊距 padding-left
        • 4.2.4右邊內邊距 padding-right
    • 五、盒子有外邊距,用CSS寫法為外邊距(margin)
      • 5.1margin的復合屬性
      • 5.2單獨控制上下左右邊框
        • 5.2.1上邊外邊距 margin-top
        • 5.2.2下邊外邊距 margin-bottom
        • 5.2.3左邊外邊距 margin-left
        • 5.2.4左邊外邊距 margin-left
    • 六、盒子模型實例
    • 七、如何隱藏一個元素

一、盒子模型的幾個關鍵詞

內容(content)、填充(padding通俗講就是內邊距)、邊框(border)、邊界(margin通俗講就是外邊距)。

用圖片來演示一下

簡單用畫圖工具演示一下就是

二、盒子有尺寸,用CSS寫法為寬度(width)和高度(height)

定義盒子的寬度和高度

<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 200px;height: 100px;background-color: blue;}</style></head><body><div id="box1">Hello, I am box1</div></body> </html>

運行結果

注意:只有塊元素才可以設置寬度和高度,行內元素無法設置寬度和高度。

三、盒子有邊框,用CSS寫法為上下左右邊框(border)

3.1邊框線型 border-style

參數(shù):
none : 無邊框 dotted : 點線邊框 dashed : 虛線邊框 solid : 實線邊框 double : 雙線邊框

3.2邊框顏色

檢索或設置對象的邊框顏色。
語法:

border-color : color

3.3邊框的復合屬性

語法:

border : border-style||border-width|| border-color

例如我要設置 p標簽的邊框為: 線型實線 , 寬度20px , 顏色為紅色

p { border: solid 20px red; }

3.4單獨控制上下左右邊框

3.4.1上邊框 border-top

檢索或設置對象的上邊框。這是一個復合屬性。
語法:

border-top: border-style||border-width||border-color

例如我要設置 p標簽的上邊框為: 線型實線 ,寬度20px , 顏色為紅色。

<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 200px;height: 100px;border-top: solid 20px red;}</style></head><body><div id="box1">Hello, I am box1</div></body> </html>

運行效果

3.4.2下邊框 border-bottom

檢索或設置對象的下邊框。這是一個復合屬性。
語法:

border-bottom :border-style||border-width||border-color

比如我要設置 p標簽的下邊框為: 寬度2px , 線型實線 , 顏色為紅色 。

<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 200px;height: 100px;border-bottom: 20px solid red;}</style></head><body><div id="box1">Hello, I am box1</div></body> </html>

運行效果

3.4.3左邊框border-left

3.4.4右邊框border-right

左邊框 border-left、右邊框 border-right同上

四、盒子有內邊距,用CSS寫法為上下左右內邊距(padding)

4.1padding的復合屬性

檢索或設置對象四邊的內邊距,換句話說,也就是邊框與內容之間的距離。
語法:

padding :length

例如我要邊框與內容之間的距離為50px

<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 200px;height: 100px;border: 20px solid red;padding: 50px;}</style></head><body><div id="box1">Hello, I am box1</div></body> </html>

運行效果

注意:
如果只提供一個,將用于全部的四條邊。
如果提供兩個,第一個用于上-下,第二個用于左-右。
如果提供三個,第一個用于上,第二個用于左-右,第三個用于下。
如果提供全部四個參數(shù)值,將按上-右-下-左的順序作用于四邊。

4.2單獨控制上下左右邊框

4.2.1上邊內邊距 padding-top

檢索或設置對象的上邊內邊距。
語法:

padding-top :length <!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 200px;height: 300px;border: solid 20px red;padding-top: 100px;}</style></head><body><div id="box1">Hello, my padding-top is 50px</div></body> </html>

運行結果

4.2.2底邊內邊距 padding-bottom

檢索或設置對象的下邊內邊距。
語法:

padding-right:length <!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 200px;height: 200px;border: solid 20px red;padding-bottom: 100px;}</style></head><body><div id="box1">Hello, my padding-bottom is 100pxHello, my padding-bottom is 100pxHello, my padding-bottom is 100pxHello, my padding-bottom is 100pxHello, my padding-bottom is 100pxHello, my padding-bottom is 100px</div></body> </html>

運行結果

4.2.3左邊內邊距 padding-left

4.2.4右邊內邊距 padding-right

左邊內邊距padding-left、右邊內邊距padding-right同上

五、盒子有外邊距,用CSS寫法為外邊距(margin)

5.1margin的復合屬性

檢索或設置對象四邊的外邊距。
語法:

margin:length <!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 350px;height: 200px;border: solid 20px red;padding: 2px;margin: 50px;}</style></head><body><div id="box1">I didn't say anything. I just walked away.You're the light, you're the nightYou're the color of my bloodYou're the cure, you're the painYou're the only thing I wanna touchNever knew that it could mean so much, so muchYou're the fear, I don't care'Cause I've never been so highFollow me to the darkLet me take you past our satellites</div></body> </html>

運行結果

注意:
如果只提供一個,將用于全部的四條邊。
如果提供兩個,第一個用于上-下,第二個用于左-右。
如果提供三個,第一個用于上,第二個用于左-右,第三個用于下。
如果提供全部四個參數(shù)值,將按上-右-下-左的順序作用于四邊。

外邊距在不同瀏覽器的效果會有很大不同,建議盡量少用。

5.2單獨控制上下左右邊框

5.2.1上邊外邊距 margin-top

檢索或設置對象的上邊外邊距。
語法:

margin-top :length <!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 350px;height: 200px;border: solid 20px red;padding: 2px;margin-top: 50px;}</style></head><body><div id="box1">I didn't say anything. I just walked away.You're the light, you're the nightYou're the color of my bloodYou're the cure, you're the painYou're the only thing I wanna touchNever knew that it could mean so much, so muchYou're the fear, I don't care'Cause I've never been so highFollow me to the darkLet me take you past our satellites</div></body> </html>

運行結果

5.2.2下邊外邊距 margin-bottom

檢索或設置對象的下邊外邊距。
語法:

margin-bottom :length <!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 350px;height: 200px;border: solid 20px red;padding: 2px;margin-bottom: 50px;}#box2{width: 350px;height: 200px;background-color: blue;}</style></head><body><div id="box1">I didn't say anything. I just walked away.You're the light, you're the nightYou're the color of my bloodYou're the cure, you're the painYou're the only thing I wanna touchNever knew that it could mean so much, so muchYou're the fear, I don't care'Cause I've never been so highFollow me to the darkLet me take you past our satellites</div><div id="box2"></div></body> </html>

運行結果

5.2.3左邊外邊距 margin-left

5.2.4左邊外邊距 margin-left

左邊外邊距 margin-left、左邊外邊距 margin-left同上

六、盒子模型實例

要把頁面布局好,需要注意

  • 頁面由很多個盒子,從上往下堆積。外層的這些盒子是獨立的。
  • 我們布局的時候,外層的盒子最好不要設置浮動也不要設置為絕對定位
  • 盒子一般不設置高度,它的高度一般由內容來撐高。
  • 每個外層的盒子需要設置寬度(這個寬度叫版心),并且要將這些盒子在水平方向居中。
  • 把每個模塊外層的容器設置為相對定位。可以作為里面每個元素定位的參考。
  • 把外層容器設置為相對定位之后,它也不會脫離標準流,不會影響布局。
  • <!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin: 0;padding: 0;}.wrapper{background-color: green;width: 1200px;margin: 0 auto;margin-bottom: 4px;}#box1{width: 200px;height: 300px;border:solid 20px red;padding-left: 100px;}#box2{width:100px;height: 100px;background-color: seagreen;cursor: move;/* display: none; *//*元素隱藏后,不占位置*//*visibility: hidden;*/ /*這種隱藏也會占著位置*//*opacity: 0;*/ /*通過設置不透明度去隱藏一個元素,元素隱藏后,位置還占著*/}#box3{width:100px;height: 100px;background-color:darkgoldenrod;cursor: move;}</style></head><body><div class="wrapper">1</div><div class="wrapper">2</div><div class="wrapper">3</div><div class="wrapper">4</div><div class="wrapper">5</div><div class="wrapper">6</div><div id="box1"><div id="box2"></div><div id="box3"></div></div></body> </html>

    附加:如何隱藏一個元素?

    七、如何隱藏一個元素

    看上面的代碼和注釋就知道了!把注釋去掉,拿去運行運行!希望可以幫到你哈哈哈

    總結

    以上是生活随笔為你收集整理的一篇文章带你实操代码理解盒子模型的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。