Home  >  Article  >  Web Front-end  >  HTML页面布局基础_html/css_WEB-ITnose

HTML页面布局基础_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:27:541302browse

本篇博文主要整理一下html页面布局的基础知识。虽然这些知识基本都懂,但是实际用起来,其中的一些细节老是注意不到(>_

盒子模型

盒子模型是css中一个重要的概念,理解了盒子模型才能更好的排版。盒子模型范围包括: border、padding、margin、content 。盒子模型有两种,分别是IE(怪异模式)盒子模型和标准盒子模型。两者的区别是,IE盒子模型content部分包含 padding 和 border ,而标准盒子模型不包括!css3的 border-sizing 属性可以选择特定盒模型: content-boxing (默认 标准盒子模型); border-boxing (IE盒子模型)

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>box</title></head><style>/* 标准模式 */.content-box{ box-sizing: content-box; width:200px; height:100px; margin:20px; padding:30px; border:10px solid green; }/* 怪异模式 */.border-box{ box-sizing: border-box; -moz-box-sizing:border-box; /* Firefox */-webkit-box-sizing:border-box; /* Safari */width:200px; height:100px; margin:20px; padding:30px; border:10px solid green; }</style><body><divclass="content-box">标准模式</div><divclass="border-box">怪异模式</div></body></html>

呈现效果

标准模式

  • content width: 200

  • content height: 100

  • 左border到右border长度为: (10+30)*2 + 200

怪异模式

  • content width: 120

  • content height: 20

  • 左border到右border长度为: (10+30)*2 + 120

POSITION

这个属性定义建立元素布局所用的定位机制。任何元素都可以定位,不过 绝对(absolute)或固定(fixed) 元素会生成 一个块级框 ,而不论该元素本身是什么类型。相对定位元素会相对于它在正常流中的默认位置偏移。

  • static:默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)

  • absolute:生成绝对定位的元素, 相对于static定位以外 的第一个父元素进行定位

  • relative:生成相对定位的元素,相对于其 本身位置 进行定位

  • fixed:生成绝对定位的元素,相对于 浏览器窗口 进行定位

  • inherit:规定应该从父元素继承 position 属性的值

相对定位

相对定位比较简单,对应position属性的relative值,如果对一个元素进行相对定位,它将出现在他所在的位置上,然后可以通过设置垂直或水平位置,让这个元素相对于它自己移动, 在使用相对定位时,无论元素是否移动,元素在文档流中占据原来空间,只是表现会改变 。

<!-- 普通流 --><divstyle="border: solid 1px #0e0; width:200px;"><divstyle="height: 100px; width: 100px; background-color: Red;"></div><divstyle="height: 100px; width: 100px; background-color: Green;"></div><divstyle="height: 100px; width: 100px; background-color: Red;"></div></div>

<!-- 相对定位 --><divstyle="border: solid 1px #0e0; width:200px;"><divstyle="height: 100px; width: 100px; background-color: Red;"></div><divstyle="height: 100px; width: 100px; background-color: Green; position:relative; top:20px; left:20px;"></div><divstyle="height: 100px; width: 100px; background-color: Red;"></div></div>

绝对定位

相对定位可以看作特殊的普通流定位,元素位置是相对于他在普通流中位置发生变化,而 绝对定位使元素的位置与文档流无关,也不占据文档流空间,普通流中的元素布局就像绝对定位元素不存在一样。

绝对定位的元素的位置是相对于距离他最近的非static祖先元素位置决定的。也就是说离其最近的祖先元素只要position属性不是static,都可以作为绝对定位的参照标准!如果元素没有已定位的祖先元素,那么他的位置就相对于初始包含块儿(body或html)元素。

因为绝对定位与文档流无关,所以绝对定位的元素可以覆盖页面上的其他元素,可以通过z-index属性控制叠放顺序,z-index越高,元素位置越靠上。

<!-- 绝对定位 --><divstyle="border: solid 1px #0e0; width:200px; position:relative;"><divstyle="height: 100px; width: 100px; background-color: Red;"></div><divstyle="height: 100px; width: 100px; background-color: Green; position:absolute; top:20px; left:20px;"></div><divstyle="height: 100px; width: 100px; background-color: Yellow;"></div></div>

固定定位

固定定位对应position属性的fixed值,固定定位其实是一种特殊的绝对定位,固定定位的元素也不包含在普通文档流中,包含块儿是视口(viewport)

注意:该属性不兼容IE6

参考

  • 学习CSS布局

  • css3 box-sizing属性

  • CSS布局 ——从display,position, float属性谈起

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn