模型是CSS布局的基礎,理解它的每個組成部分對于創建整潔、響應式的網頁至關重要。本文將深入探討盒模型的四個主要組成部分:邊距(Margin)、邊框(Border)、填充(Padding)和內容(Content),并解釋它們如何共同工作來創建網頁布局。
在CSS中,盒模型是一種用于設計和布局的概念模型,它將HTML元素視為一個盒子。這個盒子包括了元素的內容、內邊距、邊框和外邊距。理解盒模型對于控制元素的大小和在頁面上的位置至關重要。
+-------------------------------+
| Margin |
| +-------------------------+ |
| | Border | |
| | +-------------------+ | |
| | | Padding | | |
| | | +-------------+ | | |
| | | | Content | | | |
| | | +-------------+ | | |
| | +-------------------+ | |
| +-------------------------+ |
+-------------------------------+
每個盒子從里到外包括:
邊距是盒子外部的空間,它決定了元素之間的間隔。邊距是透明的,不可見,不會被背景顏色或背景圖片覆蓋。
/* 單邊邊距設置 */
.element {
margin-top: 10px; /* 上邊距 */
margin-right: 15px; /* 右邊距 */
margin-bottom: 10px; /* 下邊距 */
margin-left: 15px; /* 左邊距 */
}
/* 簡寫形式 */
.element {
margin: 10px 15px; /* 上下邊距 | 左右邊距 */
}
邊距可以用來創建元素之間的空間,或者將元素與頁面邊緣分開。當兩個元素的垂直邊距相遇時,它們會合并成一個邊距,這個現象稱為邊距折疊。
邊框是盒子的一個可視化組件,圍繞著內邊距和內容。邊框的樣式、寬度和顏色都可以自定義。
.element {
border-style: solid; /* 邊框樣式 */
border-width: 2px; /* 邊框寬度 */
border-color: black; /* 邊框顏色 */
}
/* 簡寫形式 */
.element {
border: 2px solid black;
}
邊框對于突出顯示元素或分隔內容非常有用。你還可以只為邊框的一邊或幾邊設置樣式。
填充是圍繞內容內部的空間,它可以增加內容和邊框之間的距離。與邊距不同,填充區域會被背景顏色或背景圖片覆蓋。
.element {
padding-top: 5px; /* 上填充 */
padding-right: 10px; /* 右填充 */
padding-bottom: 5px; /* 下填充 */
padding-left: 10px; /* 左填充 */
}
/* 簡寫形式 */
.element {
padding: 5px 10px; /* 上下填充 | 左右填充 */
}
填充對于控制元素內部的空白區域非常有用,它可以幫助改善內容的可讀性。
內容是盒子中的文字、圖片或其他媒體。內容的大小可以通過設置width和height屬性來控制,但實際可見區域的大小還會受到內邊距和邊框的影響。
.element {
width: 200px;
height: 150px;
}
內容區域是設計和布局的核心,所有的文本和媒體都在這里顯示。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Margin, Border, Padding Example</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.container {
max-width: 800px;
margin: auto;
background-color: white;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.header {
background-color: #007bff;
color: white;
padding: 20px;
text-align: center;
}
.content {
padding: 20px;
border: 1px solid #ddd;
margin: 20px;
}
.box {
background-color: #007bff;
color: white;
padding: 10px;
margin: 10px;
border: 3px solid #0056b3;
text-align: center;
}
.footer {
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Welcome to My Page</h1>
</div>
<div class="content">
<h2>Understanding CSS Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of margins, borders, padding, and the actual content. This model allows us to create space between elements and style them effectively.</p>
<div class="box">Content Box</div>
</div>
<div class="footer">
Footer Content
</div>
</div>
</body>
</html>
理解盒模型是前端開發的基礎,它允許我們精確控制元素的布局和間距。通過恰當地使用邊距、邊框、填充和內容,我們可以創建出既美觀又功能強大的網頁設計。隨著響應式設計的興起,現代CSS框架已經將盒模型的概念整合進其核心,使得跨設備布局變得更加一致和簡單。
在日常開發中,經常使用開發者工具來檢查和調試盒模型的各個部分,確保我們的樣式表現按照預期工作。掌握盒模型,你將能夠更加自信地處理網頁布局的挑戰。
SS常用屬性:
字體屬性:(font)
大小 font-size: x-large;(特大) xx-small;(極小) 一般中文用不到,只要用數值就可以,單位:PX、PD
樣式 font-style: oblique;(偏斜體) italic;(斜體) normal;(正常)
行高 line-height: normal;(正常) 單位:PX、PD、EM
粗細 font-weight: bold;(粗體) lighter;(細體) normal;(正常)
變體 font-variant: small-caps;(小型大寫字母) normal;(正常)
大小寫 text-transform: capitalize;(首字母大寫) uppercase;(大寫) lowercase;(小寫) none;(無)
修飾 text-decoration: underline;(下劃線) overline;(上劃線) line-through;(刪除線) blink;(閃爍)
常用字體: (font-family)
"Courier New", Courier, monospace, "Times New Roman", Times, serif, Arial, Helvetica, sans-serif, Verdana
背景屬性: (background)
色彩background-color: #FFFFFF;
圖片background-image: url();
重復background-repeat: no-repeat;
滾動background-attachment: fixed;(固定) scroll;(滾動)
位置background-position: left(水平) top(垂直);
簡寫方法 background:#000 url(..) repeat fixed left top;
區塊屬性: (Block)
字間距letter-spacing: normal; 數值
對劉text-align: justify;(兩端對齊) left;(左對齊) right;(右對齊) center;(居中)
縮進text-indent: 數值px;
垂直對齊vertical-align: baseline;(基線) sub;(下標) super;(下標) top; text-top; middle; bottom; text-bottom;
詞間距word-spacing: normal; 數值
空格white-space: pre;(保留) nowrap;(不換行)
顯示display:block;(塊) inline;(內嵌) list-item;(列表項) run-in;(追加部分) compact;(緊湊) marker;(標記) table; inline-table; table-raw-group; table-header-group; table-footer-group; table-raw; table-column-group; table-column; table-cell; table-caption;(表格標題)
方框屬性: (Box)
width:; height:; float:; clear:both; margin:; padding:; 順序:上右下左
邊框屬性: (Border)
border-style: dotted;(點線) dashed;(虛線) solid; double;(雙線) groove;(槽線) ridge;(脊狀) inset;(凹陷) outset;
border-width:; 邊框寬度
border-color:#;
簡寫方法border:width style color;
列表屬性: (List-style)
類型list-style-type: disc;(圓點) circle;(圓圈) square;(方塊) decimal;(數字) lower-roman;(小羅碼數字) upper-roman; lower-alpha; upper-alpha;
位置list-style-position: outside;(外) inside;
圖像list-style-image: url(..);
定位屬性: (Position)
Position: absolute; relative; static;
visibility: inherit; visible; hidden;
overflow: visible; hidden; scroll; auto;
clip: rect(12px,auto,12px,auto) (裁切)
css屬性代碼大全
一 CSS文字屬性:
color : #999999; /*文字顏色*/
font-family : 宋體,sans-serif; /*文字字體*/
font-size : 9pt; /*文字大小*/
font-style:itelic; /*文字斜體*/
font-variant:small-caps; /*小字體*/
letter-spacing : 1pt; /*字間距離*/
line-height : 200%; /*設置行高*/
font-weight:bold; /*文字粗體*/
vertical-align:sub; /*下標字*/
vertical-align:super; /*上標字*/
text-decoration:line-through; /*加刪除線*/
text-decoration: overline; /*加頂線*/
text-decoration:underline; /*加下劃線*/
text-decoration:none; /*刪除鏈接下劃線*/
text-transform : capitalize; /*首字大寫*/
text-transform : uppercase; /*英文大寫*/
text-transform : lowercase; /*英文小寫*/
text-align:right; /*文字右對齊*/
text-align:left; /*文字左對齊*/
text-align:center; /*文字居中對齊*/
text-align:justify; /*文字分散對齊*/
vertical-align屬性
vertical-align:top; /*垂直向上對齊*/
vertical-align:bottom; /*垂直向下對齊*/
vertical-align:middle; /*垂直居中對齊*/
vertical-align:text-top; /*文字垂直向上對齊*/
vertical-align:text-bottom; /*文字垂直向下對齊*/
二、CSS邊框空白
padding-top:10px; /*上邊框留空白*/
padding-right:10px; /*右邊框留空白*/
padding-bottom:10px; /*下邊框留空白*/
padding-left:10px; /*左邊框留空白
三、CSS符號屬性:
list-style-type:none; /*不編號*/
list-style-type:decimal; /*阿拉伯數字*/
list-style-type:lower-roman; /*小寫羅馬數字*/
list-style-type:upper-roman; /*大寫羅馬數字*/
list-style-type:lower-alpha; /*小寫英文字母*/
list-style-type:upper-alpha; /*大寫英文字母*/
list-style-type:disc; /*實心圓形符號*/
list-style-type:circle; /*空心圓形符號*/
list-style-type:square; /*實心方形符號*/
list-style-image:url(/dot.gif); /*圖片式符號*/
list-style-position: outside; /*凸排*/
list-style-position:inside; /*縮進*/
四、CSS背景樣式:
background-color:#F5E2EC; /*背景顏色*/
background:transparent; /*透視背景*/
background-image : url(/image/bg.gif); /*背景圖片*/
background-attachment : fixed; /*浮水印固定背景*/
background-repeat : repeat; /*重復排列-網頁默認*/
background-repeat : no-repeat; /*不重復排列*/
background-repeat : repeat-x; /*在x軸重復排列*/
background-repeat : repeat-y; /*在y軸重復排列*/
指定背景位置
background-position : 90% 90%; /*背景圖片x與y軸的位置*/
background-position : top; /*向上對齊*/
background-position : buttom; /*向下對齊*/
background-position : left; /*向左對齊*/
background-position : right; /*向右對齊*/
background-position : center; /*居中對齊*/
五、CSS連接屬性:
a /*所有超鏈接*/
a:link /*超鏈接文字格式*/
a:visited /*瀏覽過的鏈接文字格式*/
a:active /*按下鏈接的格式*/
a:hover /*鼠標轉到鏈接*/
鼠標光標樣式:
鏈接手指 CURSOR: hand
十字體 cursor:crosshair
箭頭朝下 cursor:s-resize
十字箭頭 cursor:move
箭頭朝右 cursor:move
加一問號 cursor:help
箭頭朝左 cursor:w-resize
箭頭朝上 cursor:n-resize
箭頭朝右上 cursor:ne-resize
箭頭朝左上 cursor:nw-resize
文字I型 cursor:text
箭頭斜右下 cursor:se-resize
箭頭斜左下 cursor:sw-resize
漏斗 cursor:wait
光標圖案(IE6) p {cursor:url("光標文件名.cur"),text;}
六、CSS框線一覽表:
border-top : 1px solid #6699cc; /*上框線*/
border-bottom : 1px solid #6699cc; /*下框線*/
border-left : 1px solid #6699cc; /*左框線*/
border-right : 1px solid #6699cc; /*右框線*/
以上是建議書寫方式,但也可以使用常規的方式 如下:
border-top-color : #369 /*設置上框線top顏色*/
border-top-width :1px /*設置上框線top寬度*/
border-top-style : solid/*設置上框線top樣式*/
其他框線樣式
solid /*實線框*/
dotted /*虛線框*/
double /*雙線框*/
groove /*立體內凸框*/
ridge /*立體浮雕框*/
inset /*凹框*/
outset /*凸框*/
七、CSS表單運用:
文字方塊
按鈕
復選框
選擇鈕
多行文字方塊
下拉式菜單 選項1選項2
八、CSS邊界樣式:
margin-top:10px; /*上邊界*/
margin-right:10px; /*右邊界值*/
margin-bottom:10px; /*下邊界值*/
margin-left:10px; /*左邊界值*/
CSS 屬性: 字體樣式(Font Style)
序號 中文說明 標記語法
1 字體樣式 {font:font-style font-variant font-weight font-size font-family}
2 字體類型 {font-family:"字體1","字體2","字體3",...}
3 字體大小 {font-size:數值|inherit| medium| large| larger| x-large| xx-large| small| smaller| x-small| xx-small}
4 字體風格 {font-style:inherit|italic|normal|oblique}
5 字體粗細 {font-weight:100-900|bold|bolder|lighter|normal;}
6 字體顏色 {color:數值;}
7 陰影顏色 {text-shadow:16位色值}
8 字體行高 {line-height:數值|inherit|normal;}
9 字 間 距 {letter-spacing:數值|inherit|normal}
10 單詞間距 {word-spacing:數值|inherit|normal}
11 字體變形 {font-variant:inherit|normal|small-cps }
12 英文轉換 {text-transform:inherit|none|capitalize|uppercase|lowercase}
13 字體變形 {font-size-adjust:inherit|none}
14 字體 {font-stretch:condensed|expanded|extra-condensed|extra-expanded|inherit|narrower|normal| semi-condensed|semi-expanded|ultra-condensed|ultra-expanded|wider}
文本樣式(Text Style)
序號 中文說明 標記語法
1 行 間 距 {line-height:數值|inherit|normal;}
2 文本修飾 {text-decoration:inherit|none|underline|overline|line-through|blink}
3 段首空格 {text-indent:數值|inherit}
4 水平對齊 {text-align:left|right|center|justify}
5 垂直對齊 {vertical-align:inherit|top|bottom|text-top|text-bottom|baseline|middle|sub|super}
6 書寫方式 {writing-mode:lr-tb|tb-rl}
背景樣式
序號 中文說明 標記語法
1 背景顏色 {background-color:數值}
2 背景圖片 {background-image: url(URL)|none}
3 背景重復 {background-repeat:inherit|no-repeat|repeat|repeat-x|repeat-y}
4 背景固定 {background-attachment:fixed|scroll}
5 背景定位 {background-position:數值|top|bottom|left|right|center}
6 背影樣式 {background:背景顏色|背景圖象|背景重復|背景附件|背景位置}
框架樣式(Box Style)
序號 中文說明 標記語法
1 邊界留白 {margin:margin-top margin-right margin-bottom margin-left}
2 補 白 {padding:padding-top padding-right padding-bottom padding-left}
3 邊框寬度 {border-width:border-top-width border-right-width border-bottom-width border-left-width}
寬度值: thin|medium|thick|數值
4 邊框顏色 {border-color:數值 數值 數值 數值} 數值:分別代表top、right、bottom、left顏色值
5 邊框風格 {border-style:none|hidden|inherit|dashed|solid|double|inset|outset|ridge|groove}
6 邊 框 {border:border-width border-style color}
上 邊 框 {border-top:border-top-width border-style color}
右 邊 框 {border-right:border-right-width border-style color}
下 邊 框 {border-bottom:border-bottom-width border-style color}
左 邊 框 {border-left:border-left-width border-style color}
7 寬 度 {width:長度|百分比| auto}
8 高 度 {height:數值|auto}
9 漂 浮 {float:left|right|none}
10 清 除 {clear:none|left|right|both}
分類列表
序號 中文說明 標記語法
1 控制顯示 {display:none|block|inline|list-item}
2 控制空白 {white-space:normal|pre|nowarp}
3 符號列表 {list-style-type:disc|circle|square|decimal|lower-roman|upper-roman|lower-alpha|upper-alpha|none}
4 圖形列表 {list-style-image:URL}
5 位置列表 {list-style-position:inside|outside}
6 目錄列表 {list-style:目錄樣式類型|目錄樣式位置|url}
7 鼠標形狀 {cursor:hand|crosshair|text|wait|move|help|e-resize|nw-resize|w-resize|s-resize|se-resize|sw-resize}
*請認真填寫需求信息,我們會在24小時內與您取得聯系。