search
HomeWeb Front-endHTML TutorialIntroduction to the position attribute value in HTML

This article introduces to you the content of the article about the position attribute value in HTML. It has a good reference value. I hope it can help friends in need.

Theoretically, there are 8 values ​​for all positions

Including: position: static | relative | absolute | fixed | sticky | initial | inherit | unset

The most commonly used ones are static, relative, absolute, fixed and sticky

initial, inherit and unset are css Keywords, any css attribute value can be set to these values

position: static

Default value, in normal flow, the set top, left, right, bottom, z-index should be ignored

position: relative

Relative positioning, Offset relative to its original position, (Example: top: 10px; // After moving, the top of the element is located 10px below the top of the original position;)

Detach from the document flow, but retain the space at the original position in the document flow (Reserved space),

That is to say , the original position of the element will always remain a blank space, and the adjacent sibling elements will maintain their original position and will not change with the movement of the element

Note: position: relative to table-* -The group, table-row, table-column, table-cell, table-caption elements are invalid.

Chestnut:

##position: absolute

Absolute positioning, if the ancestor element has an attribute value other than position: static, then

Absolute positioning is relative to the ancestor element; otherwise, Absolute positioning is relative to the browser viewport

(One thing to note here is that most people think that it is absolutely positioned relative to the html or body element. This is a misunderstanding; when the page is scrollable, it can be seen that it is absolutely positioned relative to the browser window. Instead of the entire html content; I am too lazy to write examples here, so I will just learn from others:

CSS Advanced - How to define the width and height of absolutely positioned elements)

Therefore, the general approach is to add the position: relative

attribute to the parent element of the absolutely positioned element

out of the document flow without reserving space. Move the position of sibling elements up

Chestnut:

But position: absolute does not only have the above purpose;

is based on its content size For resized elements (for example, height and width are set to auto, or inline elements), if the element is absolutely positioned position: absulute, you can

specify top / bottom / left / right, leaving height unspecified (i.e. auto), to fill the available vertical (horizontal) space

What does it mean? Calm down, let’s eat some chestnuts:

(In addition,

absolutely positioned elements can be set with margins , and will not be merged with other margins, this will not Give me an example)

When I wrote the above example, I still had a

question:

Since absolute positioning is detached The document is flowing. Why is the origin of an inline element located at its original position when the left value is not set when it is absolutely positioned? Instead of being at the origin of the parent element?

(I haven’t found the reason for a long time, I’ll leave this for later)

position: fixed

Fixed positioning,

Fixed positioning relative to the browser window, not scrolling with the scroll bar, the sample implemented is the advertising pop-up window in daily web pages

out of the document The stream, fixed attribute creates a new cascading context.

Note: When the transform attribute of the ancestor element of the fixed element is not none, the container is changed from the browser window to the ancestor element

position: sticky

Sticky positioning is equivalent to a mixture of relative positioning and fixed positioning. Sticky positioning is determined based on a threshold. When it is greater than or equal to the threshold, relative positioning is used, and when it is less than the threshold, it is fixed positioning.

This threshold is one of top, right, bottom, and left. One of them must be set for sticky positioning to take effect, otherwise it will always appear as relative positioning.

In addition to the above conditions, there are several points that need to be paid attention to, otherwise sticky positioning will be invalid:

1. The parent element The content needs to be scrolled and cannot have overflow: hidden or overflow: auto attributes

2. The height of the parent element cannot be lower than the height of the sticky positioning element

3. Sticky is container-related and will only take effect in the container (parent element) in which it is located.

sticky Chestnut:demo

Sticky positioningCompatibilityQuestions:

1. Need to write The next two css statements:

position: sticky; and position: -webkit-sticky; /* Safari */

2. Internet Explorer, Edge 15 and above Earlier IE versions did not support sticky positioning.

position: initial

The initial keyword is used to set the CSS property to its default value (in this case, position: static)

Note: IE does not support this keyword

position: inherit

Every The overview of each CSS property definition indicates whether the property is inherited by default ("Inherited: Yes") or not inherited by default ("Inherited: no").

You can read about the wonderful use of inherit here: Talk about some interesting CSS topics (4) – Starting with reflection, talk about CSS inheritance

position: unset

The unset keyword is a combination of initial and inherit:

1. If the attribute is If the attribute is inherited by default, the value is equal to inherit

2. If the attribute is a non-inherited attribute, the value is equal to initial

Related recommendations:

Summary of different table attributes of HTML (with code)

HTML imitation of Baidu homepage

The above is the detailed content of Introduction to the position attribute value in HTML. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment