search
HomeWeb Front-endHTML Tutorial理解css 中的position五个属性_html/css_WEB-ITnose

在实际开发页面布局时,运用position,对定位的块级元素的嵌套的效果总是不太理解,这里做了几个测试

一般的在w3c中我们可以很容易的获取定义:

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

fixed :生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

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

absolute : 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative : 生成相对定位的元素,相对于其正常位置进行定位。因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

总的来说 :

      static呢,就是正常的文档流顺序,默认的,相当于没有定位!

      fixed呢, 就是相对于浏览器窗口,就是你滚动条怎么滚动,他还是那个位置,就想是 “粘” 在窗口上了!

  inherit呢, 就是从父元素继承 position 属性的值,

      absolute呢,是脱离文档流的原来的位置是不继续占据了,如果他的父级元素中有已经定位了的不管是absolute的还是relative,它都会相对于他的父级元素来定位,如果他的父级元素中没有定位了的那么它就是相对于body来定位的。也就是说absolute的绝对是有参照物的!

      relative呢,是不会脱离文档流的原来的位置也就继续占据了,它是只相对于自身原来的位置来定位的!

  前三个是很容易理解的,对于absolute和relative的结合使用,做了几个测试

测试(absolute和relative)

  1.单独的absolute和relative  

  2.relative中的relative,absolute中的relative

  3.absolute中的absolute,relative中的absolute

  

<!DOCTYPE html><html><head>	<title>position -- absolute -- relative</title></head><style>		.test-a{		position: absolute;		top:20px;		left:60px;		width:200px;		height: 100px;		background: red;	}	.test{		width:400px;		height: 100px;		background: green;	}	.test-r{		position:relative;		top:50px;		left:130px;		background: yellow;		width:160px;		height: 180px;	}	.test-rr{	    position: relative;	    top: 20px;	    left: 100px;	    width: 600px;	    height: 300px;	    background: blue;	}	.test-aa{		position: absolute;	    top: 24px;	    left: 34px;	    background: orange;	}	.test-aaa{		position: absolute;	    top: 24px;	    left: 34px;	    width:400px;	    height:200px;	    background: #18E457;	}	.test-aaaa{		position: absolute;	    top: 124px;	    left: 134px;	    width:400px;	    height:200px;	    background: yellow;	}	.test-rrr{		position: relative;	    top: 24px;	    left: 34px;	    width:400px;	    height:200px;	    background: yellow;	}	.test-rrr{		position: relative;	    top: 124px;	    left: 134px;	    width:400px;	    height:200px;	    background: red;	}	.test-r-a{		position: absolute;	    top: 124px;	    left: 134px;	    width:800px;	    height:800px;	    background: yellow;	}	.test-a-r{		position: relative;	    top: 124px;	    left: 134px;	    width:700px;	    height:700px;	    background: red;	}</style><body>	<div class="test-a">absolute</div>	<div class="test">分割</div>	<div class="test-r">relative</div>	<h2 id="结合使用">结合使用</h2>	<p>parentNode的position不是relative或absolute,那absolute的绝对对象是针对body的 	  parentNode的position  是relative或absolute,那absolute的绝对对象是针对parentNode的 	  也就是说absolute的绝对是有参照Node的 </p>	<div class="test-rr">		absolute外的relative		<div class="test-aa">			relative内的absolute,,,里面这个div是相对外面那个div定位的		</div>	</div>	<div class="test-aaa">			absolute外的absolute			<div class="test-aaaa">				absolute内的absolute,,,,,,,,里面这个div是相对外面那个div定位的			</div>	</div>	<div class="test-rrr" >			relative外的relative			<div class="test-rrrr">				relative内的relative,,,,,,,,里面这个div是相对原来位置定位的			</div>	</div>	<div class="test-r-a">		relative外的absolute		<div class="test">								<div class="test-a-r">				relative内的absolute,,,里面这个div是相对原来位置定位的			</div>		</div>	</div></body></html>

  其实很多时候,网上的经验,很多很杂,自己实践一下,才能自己真正理解

     

 

 

 

 

 

 

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

MantisBT

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools