第 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

AI Hentai Generator
Generate AI Hentai for free.

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

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex
