search
HomeWeb Front-endHTML TutorialWeb front-end interview question No. 8—Absolute positioning and relative positioning

Relative positioning (relative):

Let’s talk about relative positioning first: just like its name, relative, there must be a reference object, but this reference is not something else, it is it own position in the original document flow. The object after relative positioning is not completely separated from the document flow. The original position of the object in the document is retained (standing in the latrine without shit), and the offset object will other objects. layer mask.

The relative positioning characteristics are summarized as follows:

①For relatively positioned elements, if the width is not set, the width is the width of the entire browser, or depends on Based on the width of the parent element.

②The relatively positioned block element moves relative to its original position. After moving, it still occupies the position of the document flow and does not affect the layout of other elements

The following is verified through code

Place 5 boxes in the browser, represented by different colors, the code is as follows

HTML code

Web front-end interview question No. 8—Absolute positioning and relative positioning


CSS code

Web front-end interview question No. 8—Absolute positioning and relative positioning


##Initial rendering

Web front-end interview question No. 8—Absolute positioning and relative positioning


Set relative positioning for the third box

Web front-end interview question No. 8—Absolute positioning and relative positioning


The element is offset relative to its original position, and its width and height remain unchanged. It expands the container and occupies its original position in the document flow, without affecting the layout of other elements.

Web front-end interview question No. 8—Absolute positioning and relative positioning


##Absolute positioning(absolute):The absolutely positioned object will be separated from the document flow. The reference position of the absolute positioning is no longer itself. Which one it is depends on whether its superior or superior has been positioned. Use

left

, right, top, bottom, etc.Attributesare absolute relative to its closest parent object that has relative or absolute positioning settings. Positioning, if the parent object does not set the positioning attribute, it will be positioned relative to the html root element. After reading some posts, I found that some people think that if the parent object does not set the positioning attribute, it will be positioned relative to the body. This statement is wrong. The characteristics of absolute positioning are summarized as follows:

① When the width of an absolutely positioned block element is not set, the width is determined by the content inside the element

②After detaching, the original position is equivalent to empty, and the following elements will occupy the position

③The absolutely positioned object is set relative to the one closest to itself Position the parent object with relative positioning or absolute positioning

④If the parent element is not positioned, position it relative to the html root element

The following still uses these five The offset of each box is used to verify

(1) The block element has no offset value. The five boxes above

only give box5 an absolute positioning and no offset value. At this time, the block element is just floating in its original position. If there is a block element behind it, it will occupy its position in the document flow. Let's add a box6 below box5 to see the effect.

Note: If the width of an absolutely positioned block element is not defined, the width is determined by the content inside the element.

Web front-end interview question No. 8—Absolute positioning and relative positioning


The rendering is as follows

Web front-end interview question No. 8—Absolute positioning and relative positioning


The effect of adding a box6

Web front-end interview question No. 8—Absolute positioning and relative positioning

##As can be seen from the picture, box6 has already occupied The position of box5 in the document flow.

(2) There is an offset value

If the offset value is set and the parent element does not set relative positioning or absolute positioning, the element is positioned relative to the root element (i.e. html element, note that it is relative to the root element, not relative to the body) Use the offset of box5 to verify.

①Give box5 an offset, the parent element has no relative or absolute positioning

Web front-end interview question No. 8—Absolute positioning and relative positioning


# #The effect is as follows

Web front-end interview question No. 8—Absolute positioning and relative positioning


##②Give box5 the same offset and give the body element an absolute positioning (body element setting For absolute positioning, the width of an absolutely positioned block element is determined by the longest p, and the width becomes smaller):


Web front-end interview question No. 8—Absolute positioning and relative positioning


The renderings are as follows

Web front-end interview question No. 8—Absolute positioning and relative positioning

Pictures

Sent from AppIt is obvious from the two renderings above that they are relatively different Positioning relative to the root element is different from positioning relative to the body. The main difference is whether the

margin

value of the body is included. Next, nest three more parent boxes outside the five boxes, and give these three parent boxes a position to verify whether they are offset based on the most recently positioned parent element.

The code is as follows

HTML code

Web front-end interview question No. 8—Absolute positioning and relative positioning


##CSS code


Web front-end interview question No. 8—Absolute positioning and relative positioning

#Rendering


Picture from AppWeb front-end interview question No. 8—Absolute positioning and relative positioning
It is obvious from the above that box5 is positioned relative to the third-layer container, which is the container closest to it. If you are interested, you can give it a try and remove the positioning of the third-layer container to see if it is positioned relative to the second-layer container. I have verified it and will not post the picture.

Some students may ask why the outermost box should be set to absolute positioning and the other two to relative positioning. The difference in this positioning method mainly affects the width of the box and the relative positioning of block elements. When the width is not set, its width is the default browser width. If there is a parent element, its width is determined by the parent element. If the content of this block element is too much, the parent element will also be Hold it big.

, that is to say, the width of the relatively positioned block element depends on the parent element. So what will be the effect if these three containers are set to absolute positioning? Let's take a look at the renderings first

The picture is from AppWeb front-end interview question No. 8—Absolute positioning and relative positioning
As can be seen from the picture, the width of the third-layer container no longer depends on the parent element, because it changes from the document Detached from the flow, he is independent on his own, and his width can only be determined by the content. To sum up, the width of an absolutely positioned block element is determined by its own content. When the width of a relatively positioned block element is not set, it defaults to the width of the browser. But regardless of the width, the absolutely positioned element will find the parent element closest to itself (absolute or relative positioning) for positioning.

Summary:

relative: positioning is relative to its own position (when setting the offset, it will be relative to its own position offset). The element set to relative is still in the document flow, the width and height of the element remain unchanged, and setting the offset will not affect the position of other elements. The outermost container is set to relative positioning. If the width is not set, the width is the width of the entire browser.

absolute: Positioning is determined relative to the parent element closest to the element that is set to absolute or relative positioning. If no parent element is set to absolute or relative positioning, the element is positioned relative to the root element, which is the html element. The element with absolute set is out of the document flow. If the width of the element is not set, the width is determined by the content inside the element. After detaching, the original position is equivalent to being empty, and the following elements will occupy the position.

Note: The experimental results of this article are obtained when the parent element and child elements are not set to a fixed width. If the parent element is set to a fixed width, its child elements will be positioned either absolutely or relatively. No child element can exceed the width of its parent element. The parent element is the big brother and no one can exceed him.

The above is the detailed content of Web front-end interview question No. 8—Absolute positioning and relative positioning. 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
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

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.

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.