第27章 CSS傳統布局(下)

來源:互聯網
上載者:User

標籤:

第 27章 CSS傳統布局[下]
學習要點:
1.定位布局
2.box-sizing
3.resize


本章主要探討 HTML5中 CSS早期所使用的傳統布局,很多情況下,這些布局方式還
是非常有用的。
一.定位布局
在使用定位布局前,我們先瞭解一下定位屬性的用法。CSS2提供了position屬性來實現元素的絕對位置和相對定位。

屬性 說明
static 預設值,無定位。
absolute 絕對位置,使用 top、right、bottom、left進行位移。
relative 相對定位,使用 top、right、bottom、left進行位移。
fixed 以視窗參考定位,使用 top、right、bottom、left進行位移。


//絕對位置,脫離文檔流,以視窗文檔左上方 0,0為起點
header {
position: absolute;
top: 100px;
left: 100px;
}
所謂脫離文檔流的意思,就是本身這個元素在文檔流是佔位的。如果脫離了,就不佔有
文檔的位置,好像浮在了空中一般,有了層次感。
由於絕對位置脫離了文檔流,出現層次概念。那麼每個元素到底在那一層,會不會衝突
覆蓋。這時通過 z-index屬性來判定它們的層次關係。


屬性 說明
auto 預設層次
數字 設定層次,數字越大,層次越高

/設定在 100層上
header {
z-index: 100;
}
//以視窗參考定位,脫離文檔流,會隨著捲軸滾動而滾動
header {
position: fixed;
top: 100px;
left: 100px;
}
//相對定位,不脫離文檔流,佔位位移
header {
position: relative;
top: 100px;
left: 100px;
}
這三種分別都在各自的情況下使用,均比較常用。但還有一種情況,就是:1.既要脫
離文檔流(這樣元素之間不會相互衝突);2.以父元素,比如 body或其他父元素為參考點
(這樣可以實現地區性絕對位置);3.還必須是絕對位置。
//第一步,將需要設定參考點的父元素設定為相對,且不設定座標
body {
position: relative;
}
//第二步,如果父元素設定了參考點,子項目的絕對位置將以它為基準
header {
position: absolute;
top: 0px;
left: 0px;
}
1.固定式配置
//CSS部分
body {
width: 960px;
margin: 0 auto;
position: relative;
}
header {



width: 960px;
height: 120px;
background-color: olive;
position: absolute;
top: 0;
left: 0;
}
aside {
width: 200px;
height: 500px;
background-color: purple;
position: absolute;
top: 120px;
left: 0;
}
section {
width: 760px;
height: 500px;
background-color: maroon;
position: absolute;
top: 120px;
/*left: 200px;*/
right: 0;
}
footer {
width: 960px;
height: 120px;
background-color: gray;
position: absolute;
top: 620px;
}
在上面,基本都用了定位來進行固定式配置。但細心的可以發現,其實只有右側需要實行
絕對位置,其他就按照普通的擺放即可。對於設計成流體布局,只要將長度設定成百分比即
可。

/設定在 100層上
header {
z-index: 100;
}
//以視窗參考定位,脫離文檔流,會隨著捲軸滾動而滾動
header {
position: fixed;
top: 100px;
left: 100px;
}
//相對定位,不脫離文檔流,佔位位移
header {
position: relative;
top: 100px;
left: 100px;
}
這三種分別都在各自的情況下使用,均比較常用。但還有一種情況,就是:1.既要脫
離文檔流(這樣元素之間不會相互衝突);2.以父元素,比如 body或其他父元素為參考點
(這樣可以實現地區性絕對位置);3.還必須是絕對位置。
//第一步,將需要設定參考點的父元素設定為相對,且不設定座標
body {
position: relative;
}
//第二步,如果父元素設定了參考點,子項目的絕對位置將以它為基準
header {
position: absolute;
top: 0px;
left: 0px;
}
1.固定式配置
//CSS部分
body {
width: 960px;
margin: 0 auto;
position: relative;
}
header {



width: 960px;
height: 120px;
background-color: olive;
position: absolute;
top: 0;
left: 0;
}
aside {
width: 200px;
height: 500px;
background-color: purple;
position: absolute;
top: 120px;
left: 0;
}
section {
width: 760px;
height: 500px;
background-color: maroon;
position: absolute;
top: 120px;
/*left: 200px;*/
right: 0;
}
footer {
width: 960px;
height: 120px;
background-color: gray;
position: absolute;
top: 620px;
}
在上面,基本都用了定位來進行固定式配置。但細心的可以發現,其實只有右側需要實行
絕對位置,其他就按照普通的擺放即可。對於設計成流體布局,只要將長度設定成百分比即
可。


二.box-sizing
在盒模型那個章節,我們瞭解到元素盒子如果加入了內邊距 padding和邊框 border
後,它的總長度會增加。那麼如果這個元素用於非常精確的布局時,我們就需要進行計算增
減。這其實是比較煩人的操作,尤其是動態設定頁面配置的時候。
CSS3提供了一個屬性 box-sizing,這個屬性可以定義元素盒子的解析方式,從而可
以選擇避免掉布局元素盒子增加內邊距和邊框的長度增減問題。
屬性 說明
content-box 預設值,border和 padding設定後用於元素的總長度。

border-box border和padding設定後不用於元素的總長度。

//設定 border-box讓 border和 padding不在額外增加元素大小
aside {
width: 200px;
height: 500px;
background-color: purple;
padding: 10px;
border: 5px solid red;
box-sizing: border-box;
float: left;
}
box-sizing是 CSS3推出的,各個廠商在實現時設定了私人首碼。

三.resize(用來是否可以拖拽元素)
CSS3提供了一個 resize屬性,來更改元素尺寸大小。
屬性 說明
none 預設值,不允許使用者調整元素大小。
both 使用者可以調節元素的寬度和高度。
horizontal 使用者可以調節元素的寬度。
vertical 使用者可以調節元素的高度。
一般普通元素,預設值是不允許的。但如果是表單類的 textarea元素,預設是允許的。
而普通元素需要設定 overflow:auto,配合 resize才會出現可拖拽的圖形。



//允許修改
aside {
resize: both;
overflow:auto;
}


例子1:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>CSS傳統布局[下]</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<header>
header
</header>

<!-- <aside>
aside
</aside> -->


1111

</body>
</html>


@charset "utf-8";

body {
margin: 100px;
height: 800px;
border: 1px solid red;
/*這body父元素設定一個不需要top和left定位的相對定位,這個叫做設定參考點*/
position: relative;
}

header {
width: 100px;
height: 100px;
background-color: silver;
/*position: absolute;
top: 0;
left: 0;
z-index: 3;*/
/*position: fixed;
top: 100px;
left: 0;*/
/*position: relative;
top: 10px;
left: 10px;*/
position: absolute;
top: 100px;
left: 100px;
}

/*aside {
width: 100px;
height: 100px;
background-color: green;
position: absolute;
top: 20px;
left: 20px;
z-index: 2;
}*/

例子2

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>CSS傳統布局[下]</title>
<link rel="stylesheet" type="text/css" href="style2.css">
</head>
<body>

<header>
header
</header>

<aside>
aside
</aside>

<section>
section
</section>

<footer>
footer
</footer>

 

</body>
</html>


@charset "utf-8";

body {
width: 960px;
margin: 0 auto;
position: relative;
}

header {
width: 960px;
height: 120px;
background-color: olive;
position: absolute;
top: 0;
left: 0;
}

aside {
width: 200px;
height: 500px;
background-color: purple;
position: absolute;
top: 120px;
left: 0;
}

section {
width: 760px;
height: 500px;
background-color: maroon;
position: absolute;
top: 120px;
right: 0;
}

footer {
width: 960px;
height: 120;
background-color: gray;
position: absolute;
top: 620px;
left: 0;
}

例子3

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>CSS傳統布局[下]</title>
<link rel="stylesheet" type="text/css" href="style3.css">
</head>
<body>

<header>
header
</header>

<aside>
aside
</aside>

<section>
section
<textarea></textarea>
</section>

<footer>
footer
</footer>

 

</body>
</html>


@charset "utf-8";

body {
width: 960px;
margin: 0 auto;
}

header {
height: 120px;
background-color: olive;
overflow: auto;
resize: both;
}

aside {
width: 200px;
height: 500px;
background-color: purple;
border: 5px solid green;
padding: 10px;
box-sizing: border-box;
float: left;
}

section {
width: 760px;
height: 500px;
background-color: maroon;
float: right;
}

footer {
width: 960px;
height: 120;
background-color: gray;
clear: both;
}

textarea {
resize: none;
}

 

第27章 CSS傳統布局(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.