本篇博文主要整理一下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属性谈起

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

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

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.

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.

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.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use