search
HomeWeb Front-endHTML TutorialHTML页面布局基础_html/css_WEB-ITnose

本篇博文主要整理一下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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor