第 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 为起点
<span style="color: #800000;">header </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 100px</span>; }
所谓脱离文档流的意思,就是本身这个元素在文档流是占位的。如果脱离了,就不占有文档的位置,好像浮在了空中一般,有了层次感。
由于绝对定位脱离了文档流,出现层次概念。那么每个元素到底在那一层,会不会冲突覆盖。这时通过 z-index 属性来判定它们的层次关系。
属性 |
说明 |
auto |
默认层次 |
数字 |
设置层次,数字越大,层次越高 |
//设置在 100 层上
<span style="color: #800000;">header </span>{<span style="color: #ff0000;"> z-index</span>:<span style="color: #0000ff;"> 100</span>; }
//以窗口参考定位,脱离文档流,会随着滚动条滚动而滚动
<span style="color: #800000;">header </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> fixed</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 100px</span>; }
//相对定位,不脱离文档流,占位偏移
<span style="color: #800000;">header </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 100px</span>; }
这三种分别都在各自的情况下使用,均比较常用。但还有一种情况,就是:1.既要脱离文档流(这样元素之间不会相互冲突);2.以父元素,比如 body 或其他父元素为参考点(这样可以实现区域性绝对定位);3.还必须是绝对定位。
//第一步,将需要设置参考点的父元素设置为相对,且不设置坐标
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>; }
//第二步,如果父元素设置了参考点,子元素的绝对定位将以它为基准
<span style="color: #800000;">header </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0px</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 0px</span>; }
1.固定布局
//CSS 部分
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 960px</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>; }<span style="color: #800000;"> header </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 960px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 120px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> aside </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 500px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 120px</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> section </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 760px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 500px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 120px</span>; <span style="color: #008000;">/*</span><span style="color: #008000;">left: 200px;</span><span style="color: #008000;">*/</span><span style="color: #ff0000;"> right</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> footer </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 960px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 120px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 620px</span>; }
在上面,基本都用了定位来进行固定布局。但细心的可以发现,其实只有右侧需要实行绝对定位,其他就按照普通的摆放即可。对于设计成流体布局,只要将长度设置成百分比即可。
二.box-sizing
在盒模型那个章节,我们了解到元素盒子如果加入了内边距 padding 和边框 border 后,它的总长度会增加。那么如果这个元素用于非常精确的布局时,我们就需要进行计算增减。这其实是比较烦人的操作,尤其是动态设置页面布局的时候。
CSS3 提供了一个属性 box-sizing,这个属性可以定义元素盒子的解析方式,从而可以选择避免掉布局元素盒子增加内边距和边框的长度增减问题。
属性 |
说明 |
content-box |
默认值,border 和 padding 设置后用于元素的总 |
border-box |
border 和 padding 设置后不用于元素的总长度。 |
//设置 border-box 让 border 和 padding 不在额外增加元素大小
<span style="color: #800000;">aside </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 500px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> purple</span>;<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 5px solid red</span>;<span style="color: #ff0000;"> box-sizing</span>:<span style="color: #0000ff;"> border-box</span>;<span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; }
|
Opera |
Firefox |
Chrome |
Safari |
IE |
支持需带前缀 |
无 |
2 ~ 28 |
4 ~ 9 |
3.1 ~ 5 |
8.0+ |
支持不带前缀 |
10.1+ |
29+ |
10+ |
6+ |
9.0+ |
//完整形式
<span style="color: #800000;">-webkit-box-sizing: border-box; -moz-box-sizing: border-box; -ms-box-sizing: border-box; box-sizing: border-box;</span>
三.resize
CSS3 提供了一个 resize 属性,来更改元素尺寸大小。
属性 |
说明 |
none |
默认值,不允许用户调整元素大小。 |
both |
用户可以调节元素的宽度和高度。 |
horizontal |
用户可以调节元素的宽度。 |
vertical |
用户可以调节元素的高度。 |
一般普通元素,默认值是不允许的。但如果是表单类的 textarea 元素,默认是允许的。而普通元素需要设置 overflow:auto,配合 resize 才会出现可拖拽的图形。
//允许修改
<span style="color: #800000;">aside </span>{<span style="color: #ff0000;"> resize</span>:<span style="color: #0000ff;"> both</span>;<span style="color: #ff0000;"> overflow</span>:<span style="color: #0000ff;"> auto</span>; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...
