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
HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

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.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

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

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

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.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

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.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

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.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

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.