Home Web Front-end CSS Tutorial In css position attribute: the difference between absolute and relative

In css position attribute: the difference between absolute and relative

Jun 22, 2017 am 10:28 AM
css position the difference Attributes

If you use position to lay out the page, the position attribute of the parent element must be relative, and for elements positioned somewhere inside the parent, it is best to use Absolute, because it is not affected by the padding attribute of the parent element. Of course, you can also use position, but don’t forget the value of padding when calculating. ”

The default position attribute value of any element is static, static. Today 51RGB Q group 186242343 mainly talks about relative (relative) and absolute (absolute).

[position:absolute]
means: He means absolute positioning, which defaults to the upper left corner of the browser , combined with TOP, RIGHT, BOTTOM, LEFT (hereinafter referred to as TRBL) for positioning, has the following attributes:
1) If there is no TRBL, use the upper left corner of the parent, in When there is no parent, it refers to the upper left corner of the browser. If there is text without a parent element, it will be positioned with the upper right corner of the last text in front of it as the origin but without breaking the text and covering it above. .
2) If TRBL is set and the parent does not set the position attribute, then the current absolute will be positioned with the upper left corner of the browser as the original point, and the position will be determined by TRBL.
3) If TRBL is set and the parent sets the position attribute (whether it is absolute or relative), the upper left corner of the parent is used as the origin for positioning, and the position is determined by TRBL. Even if the parent has a Padding attribute, it has no effect on it. To put it simply: it only insists on one point and uses the upper left corner of the parent as the origin for positioning. The parent's padding has no effect on it at all.

It can be concluded from the above three points that if you want to position an element with absolute positioning attribute within its parent element, only two conditions must be met,
First: Set TRBL
Second: Set the Position attribute of the parent
The above summary is very important to ensure that you are using absolue to layout the page will not be misaligned, and will not change with the size of the browser or the size of the display resolution.
As long as there is any dissatisfaction, the element will take the upper left corner of the browser as the origin. This is where beginners tend to make mistakes. When the size of the positioned section changes, the parent The element will change accordingly, but the position of the section and parent element whose Position attribute is set to

absolute has changed and become misaligned. This is because the element is positioned in the upper right corner of the browser. The reason why the angle is the origin.
It is easy for beginners to make mistakes if they do not know the section whose Position attribute is absolute. If they want to position it in the parent section, and when the size of the browser changes or the resolution of the monitor changes, the layout will change. If there is no change, two conditions need to be met. As long as one of them is not met, the element will take the upper left corner of the browser as the origin, causing page layout to be misaligned.

[position:relative]
means relative positioning. It refers to the original point of the parent as the original point by default. If there is no parent, the bottom of the previous element in the order of text flow will be used as the original point, and TRBL is used for positioning. When there are CSS attributes such as padding in the parent, the original point of the current level will be based on the original point of the parent content area. Positioning has the following attributes:
1) If there is no TRBL, it is based on the upper left corner of the parent. When there is no parent, it refers to the upper left corner of the browser (go here and absolute first) Same), if there is text without a parent element, the bottom of the text is used as the original point for positioning and the text is disconnected (different from absolut).
2) If TRBL is set and the parent does not set the position attribute, the upper left corner of the parent will still be used as the origin for positioning (different from absolut)
3) If TRBL is set and the parent sets the position attribute (whether absolute or relative), the upper left corner of the parent is used as the origin for positioning, and the position is determined by TRBL (the first half is the same as absolute). If the parent has the Padding attribute, then the upper left corner of the content area is used as the origin for positioning (the second half is different from absolute).

It can be concluded from the above three points that regardless of whether the parent exists or not, whether there is TRBL or not, the upper left corner of the parent is positioned, but the Padding attribute of the parent will its impact.
Based on the above description of relative, we can regard the p whose position attribute is relative as an ordinary p that can be positioned using TRBL, or in other words, we only need to add position to the CSS attribute of p in our usual layout page. After :relative, you can not only use float to layout the page, but also use TRBL to layout the page. In other words, adding position:relative p can also layout the page like a normal p, but you can also use TRBL to layout the page. page. But the position attribute is absolute It cannot be used to layout the page, because if used for layout, all p's will be positioned relative to the upper left corner of the browser, so it can only be used to position an element somewhere inside an element with the absolute attribute. In this way, we can summarize the more important conclusions. Since the element with the absolute attribute is used to locate the position of an element in the parent, TRBL is indispensable. At this time, according to the third article of absolute mentioned at the beginning, if the parent element does not
position attribute, then the absolute element will be separated from the parent element, but if it is a layout page, the position attribute of the parent element cannot be absolute, otherwise the upper left corner of the browser will be the origin, so the parent element The position attribute of level elements can only be relative!

Elements with the relative attribute can be used to layout the page, and elements with the absolute attribute can be used to locate the position of an element in the parent

Summary: If position is used to lay out the page, the position attribute of the parent element must be relative. For elements positioned at a certain position inside the parent, it is best to use absolute, because it is not affected by the padding attribute of the parent element. Of course you can also use position, but don’t forget the value of padding when calculating.
But positioning has a disadvantage, it does not adapt to the height of internal elements, so when we usually lay out the page, if the height of one or some modules never changes, it can Use positioning, but Fan recommends that when laying out the page, you should still use Float as the main method and Position as a supplement! Only in this way can you create high-quality pages.

Finally, please remember my words: "When laying out a page, Float is the main one and Position is the secondary one!"

The above is the detailed content of In css position attribute: the difference between absolute and relative. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1279
29
C# Tutorial
1257
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 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.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

How to optimize website performance: Experiences and lessons learned from using the Minify library How to optimize website performance: Experiences and lessons learned from using the Minify library Apr 17, 2025 pm 11:18 PM

In the process of developing a website, improving page loading has always been one of my top priorities. Once, I tried using the Miniify library to compress and merge CSS and JavaScript files in order to improve the performance of the website. However, I encountered many problems and challenges during use, which eventually made me realize that Miniify may no longer be the best choice. Below I will share my experience and how to install and use Minify through Composer.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

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.

How to generate html by sublime How to generate html by sublime Apr 16, 2025 am 09:03 AM

There are two ways to generate HTML code in Sublime Text: Using the Emmet plugin, you can generate HTML elements by entering an abbreviation and pressing the Tab key, or use a predefined HTML file template that provides basic HTML structure and other features such as code snippets, autocomplete functionality, and Emmet Snippets.

Laravel8 optimization points Laravel8 optimization points Apr 18, 2025 pm 12:24 PM

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

See all articles