search
HomeWeb Front-endHTML TutorialSummary of position attribute_html/css_WEB-ITnose

The position attribute specifies the positioning type of the element

This attribute defines the positioning mechanism used to establish the layout of the element. Any element can be positioned, but absolute or fixed elements generate a block-level box, regardless of the type of the element itself. A relatively positioned element is offset from its default position in normal flow.

Value description
absolute
Absolute positioning
值 描述
absolute
绝对定位

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed
固定定位

生成绝对定位的元素,相对于浏览器窗口进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative
相对定位

生成相对定位的元素,相对于其正常位置进行定位。

因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。
inherit 规定应该从父元素继承 position 属性的值。
Generate absolutely positioned elements, Positioned relative to the first parent element other than static positioning.

The position of the element is specified through the "left", "top", "right" and "bottom" attributes.

fixed
Fixed positioning
Generate absolutely positioned elements, positioned relative to the browser window.

The position of the

element is specified through the "left", "top", "right" and "bottom" attributes.

relative
Relative positioning
Generates a relatively positioned element, positioned relative to its normal position.

So "left:20" adds 20 pixels to the element's LEFT position.

static Default value. Without positioning, the element appears in normal flow (ignoring top, bottom, left, right or z-index declarations).
inherit Specifies that the value of the position attribute should be inherited from the parent element.
Images can be superimposed when used with z-index. The default z-index is 0. The value increases from small to large, and the priority increases (that is, the element on the surface has the highest priority)

The above is excerpted from W3C’s definition of position

After trying and searching for information, I summarized the following points:

1. static, inherit: Well, I happily decided to ignore it. Come on (sweat!!!);

<html>    <head>        <style type="text/css">            .div1{                position: relative;                border: 10px solid red;                padding: 10px;                width: 100px;                height: 100px;                background-color: green;                background-clip: content-box;            }            .div2{                positon: absolute;                width: 50px;                height: 50px;                background-color: yellow;                left: 10px;//此处left会以padding开始定位            }        </style>    </head>    <body>        <div class='div1'>div1            <div class='div2'>div2</div>        </div>    </body></html>
2. fixed: positioned relative to the visible part of the browser; (depends on the browser)

3. relative: positioned relative to the normal flow ; (Depends on the normal position of the element itself)

4. Absolute: Positioning relative to the first parent element whose position attribute is not static (it’s really a twist, this is something I didn’t grasp at the beginning. If There is no position attribute in the parent element, or the position attributes are all static, which will rely on body positioning)

Of course, there are four diamonds in the box model: content, padding, border, and margin. Keep in mind that absolute will position

based on content adding (that is, the position where padding starts). Rendering:

 1 <html> 2     <head> 3         <style type="text/css"> 4             .div1{ 5                 position: relative; 6                 border: 10px solid red; 7                 padding: 10px; 8                 width: 100px; 9                 height: 100px;10                 background-color: green;11                 background-clip: content-box;12             }13 14             .div2{15                 positon: absolute;16                 width: 50px;17                 height: 50px;18                 background-color: yellow;19                 margin: 10px;20             }21         </style>22     </head>23 24     <body>25         <div class='div1'>div126             <div class='div2'>div2</div>27         </div>28     </body>29 </html>

A pitfall was discovered during the experiment! ! ! ! ! ! What will be the effect if left is changed to margin in the above code?

Although only one attribute has been changed, please attach the code:

Still the rendering:

The margin attribute is based on the content of the parent element.

Hmm, don’t be fooled next time.

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
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

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.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

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 of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

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.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

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 It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

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

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.

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 Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.