理解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>
其实很多时候,网上的经验,很多很杂,自己实践一下,才能自己真正理解

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

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.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.
