Home Web Front-end HTML Tutorial In-depth study on the usage of position attribute in DIV CSS layout_html/css_WEB-ITnose

In-depth study on the usage of position attribute in DIV CSS layout_html/css_WEB-ITnose

Jun 24, 2016 pm 12:27 PM

This article describes to you the usage of the position attribute in DIV CSS layout. The position attribute mainly has four attribute values. The default position attribute value of any element is static. This lesson mainly talks about relative and absolute.

How to learn the position attribute of DIV CSS layout

If you use the position attribute to layout the page, the position attribute of the parent element must be relative, and the element positioned at a certain position inside the parent, It's best to use absolute. Positioning and laying out the page is very easy. You just need to remember the last sentence of this lesson. If it is difficult, it is quite difficult to understand and requires a certain amount of patience. But fortunately, KwooJan has summarized it for everyone. Very easy to understand.

If the following text is really difficult to understand, then remember the sentence summarized at the end of this lesson: "If position is used to lay out the page, the position attribute of the parent element must be relative, and the position attribute of the parent element must be relative. For elements at a certain position within the level, 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 padding value when calculating. ”

The default position attribute value of any element is static. This lesson mainly talks about relative and absolute.

[position attribute of DIV CSS layout: absolute]

means: it means absolute positioning. By default, it refers to the upper left corner of the browser, with TOP, RIGHT, BOTTOM, LEFT ( (hereinafter referred to as TRBL) for positioning, has the following attributes:

1) If there is no TRBL, the upper left corner of the parent is used. When there is no parent, it refers to the upper left corner of the browser. If there is no parent, In the case of an element, if there is text, 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.

The above three points can be summarized. 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 when you use absolue to layout the page, it will not be misaligned, and it will change with the size of the browser or The size of the monitor resolution does not change.

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 element will change accordingly. has changed, but the position of the section and the parent element whose Position attribute is set to absolute has changed and become misaligned. This is because the element takes the upper right corner of the browser as its origin at this time.

An easy mistake for beginners is not to know the section whose Position attribute is absolute. If you 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 not 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 its origin, causing the page layout to be misaligned.

[DIV CSS layout position attribute: relative]

means relative positioning. It refers to the original point of the parent as the original point by default. If there is no parent, it will be in the order of the text flow. The bottom of the previous element is the original point, which is positioned with TRBL. When there are CSS attributes such as padding in the parent, the original point of the current level is positioned with reference to the original point of the parent content area. It has the following attributes:

1) If there is no TRBL, refer to the upper left corner of the parent. When there is no parent, it refers to the upper left corner of the browser (this is the same as the absolute first item here). If there is no parent element, If text exists, the bottom of the text will be used as the original point for positioning and the text will be disconnected (different from absolutet).

2) If TRBL is set, and the parent does not set the position attribute, positioning will still be based on the upper left corner of the parent (different from absolutet)

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 (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). www.jbxue.com

It can be concluded from the above three points that no matter whether the parent exists or not, whether there is TRBL or not, the positioning is based on the upper left corner of the parent, but the Padding attribute of the parent will affect it.

Based on the above description of relative, we can regard the DIV whose position attribute is relative as an ordinary DIV that can be positioned using TRBL, or as long as we add the CSS attribute of the div in our usual layout page After adding position: relative, you can not only use float to layout the page, but also use TRBL to layout the page. In other words, the DIV with position: relative can also layout the page like a normal DIV, but you can also use TRBL lays out the page. However, the position attribute with absolute cannot be used to layout the page, because if used for layout, all DIVs will be positioned relative to the upper left corner of the browser, so it can only be used to position an element on an element with the absolute attribute. somewhere inside.

In this way we can summarize the more important conclusions:

Elements with a relative attribute can be used to layout the page, and elements with an absolute attribute are used to position an element in the parent. 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 have a position attribute, 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 position attribute of the parent element can only be relative!

Summary:

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 It is not affected by the padding attribute of the parent element. Of course, you can also use position, but don't forget the padding value when calculating.

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

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months 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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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