Table of Contents
Reply to discussion (solution)
Home Web Front-end HTML Tutorial Relative position and absolute position of position_html/css_WEB-ITnose

Relative position and absolute position of position_html/css_WEB-ITnose

Jun 24, 2016 am 11:41 AM

When the parent container is relative, the child container is set to absolute.

Why should the child container be set to absolute and the parent container be set to relative???

Isn’t it okay to use Relative for both the parent element and the child element? ? ?

What are the advantages and disadvantages of using position and using float and margin? ? ?


Reply to discussion (solution)

Only when the parent container is set to relative will the child container be positioned based on the parent container. Relative is only relative to the original The position is shifted.

relative
absolute

float
margin

What do they implement respectively?
Just write it down and your problem will be solved

Why should the child container be set to absolute and the parent container be set to relative?
This statement is not strict
When the child container is set to absolute, the parent container It can be relative or absolute

absolute is the absolute positioning within the parent container
relative is the relative positioning relative to the default position of the document flow

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><body>有三种布局:1. 行布局(默认布局方式);2. 列布局(使用float);3. 定位布局(使用position)在定位布局中,需要搞清楚两个问题:1. 目标;2. 参照物。当使用绝对定位时,必须指定参照物,然后进行定位;而成为参照物的条件是:1. 是目标元素的父元素或祖先元素;2. position属性的值不能为static(默认值)。成为参照物的元素,其position属性值可以是 absolute、relative、fixed;其中relative是最常见的,因为将元素的position属性设置为relative,不会使该元素从标准的文档流脱离出来(也就是没什么影响)。<style type="text/css">    .relObj { position: relative; }    .absObj { position: absolute; }    .container {width: 300px; height: 200px; border: solid 1px gray; }    .closeBtn {color: red; font-weight: bold; cursor: pointer;        top: 1px; right: 1px; }</style><div class="container relObj">    <div class="closeBtn absObj">X</div></div></body></html>
Copy after login
Copy after login
Copy after login

Four Lou's explanations were spot on and his demonstrations were effective.
Simply put, the postion attribute requires a reference object. Absolute is absolute positioning. It will break away from the document flow and does not occupy the width and height. Generally, absolute positioning requires a fixed width and height, otherwise the content will not be displayed.
Relative positioning requires width and height.

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><body>有三种布局:1. 行布局(默认布局方式);2. 列布局(使用float);3. 定位布局(使用position)在定位布局中,需要搞清楚两个问题:1. 目标;2. 参照物。当使用绝对定位时,必须指定参照物,然后进行定位;而成为参照物的条件是:1. 是目标元素的父元素或祖先元素;2. position属性的值不能为static(默认值)。成为参照物的元素,其position属性值可以是 absolute、relative、fixed;其中relative是最常见的,因为将元素的position属性设置为relative,不会使该元素从标准的文档流脱离出来(也就是没什么影响)。<style type="text/css">    .relObj { position: relative; }    .absObj { position: absolute; }    .container {width: 300px; height: 200px; border: solid 1px gray; }    .closeBtn {color: red; font-weight: bold; cursor: pointer;        top: 1px; right: 1px; }</style><div class="container relObj">    <div class="closeBtn absObj">X</div></div></body></html>
Copy after login
Copy after login
Copy after login




Very good, you can refer to it

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><body>有三种布局:1. 行布局(默认布局方式);2. 列布局(使用float);3. 定位布局(使用position)在定位布局中,需要搞清楚两个问题:1. 目标;2. 参照物。当使用绝对定位时,必须指定参照物,然后进行定位;而成为参照物的条件是:1. 是目标元素的父元素或祖先元素;2. position属性的值不能为static(默认值)。成为参照物的元素,其position属性值可以是 absolute、relative、fixed;其中relative是最常见的,因为将元素的position属性设置为relative,不会使该元素从标准的文档流脱离出来(也就是没什么影响)。<style type="text/css">    .relObj { position: relative; }    .absObj { position: absolute; }    .container {width: 300px; height: 200px; border: solid 1px gray; }    .closeBtn {color: red; font-weight: bold; cursor: pointer;        top: 1px; right: 1px; }</style><div class="container relObj">    <div class="closeBtn absObj">X</div></div></body></html>
Copy after login
Copy after login
Copy after login



Thank you, I would like to ask again, if "absolute" is used to make an element out of the document flow, will its child elements also be out of the document flow? ? ?
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

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

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 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.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

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 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.

See all articles