Home Web Front-end HTML Tutorial Summary of position attribute_html/css_WEB-ITnose

Summary of position attribute_html/css_WEB-ITnose

Jun 24, 2016 am 11:58 AM
position Attributes

The position attribute specifies the positioning type of the element

This attribute defines the positioning mechanism used to establish the layout of the element. Any element can be positioned, but absolute or fixed elements generate a block-level box, regardless of the type of the element itself. A relatively positioned element is offset from its default position in normal flow.

Value description
absolute
Absolute positioning
值 描述
absolute
绝对定位

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed
固定定位

生成绝对定位的元素,相对于浏览器窗口进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative
相对定位

生成相对定位的元素,相对于其正常位置进行定位。

因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。
inherit 规定应该从父元素继承 position 属性的值。
Generate absolutely positioned elements, Positioned relative to the first parent element other than static positioning.

The position of the element is specified through the "left", "top", "right" and "bottom" attributes.

fixed
Fixed positioning
Generate absolutely positioned elements, positioned relative to the browser window.

The position of the

element is specified through the "left", "top", "right" and "bottom" attributes.

relative
Relative positioning
Generates a relatively positioned element, positioned relative to its normal position.

So "left:20" adds 20 pixels to the element's LEFT position.

static Default value. Without positioning, the element appears in normal flow (ignoring top, bottom, left, right or z-index declarations).
inherit Specifies that the value of the position attribute should be inherited from the parent element.
Images can be superimposed when used with z-index. The default z-index is 0. The value increases from small to large, and the priority increases (that is, the element on the surface has the highest priority)

The above is excerpted from W3C’s definition of position

After trying and searching for information, I summarized the following points:

1. static, inherit: Well, I happily decided to ignore it. Come on (sweat!!!);

<html>    <head>        <style type="text/css">            .div1{                position: relative;                border: 10px solid red;                padding: 10px;                width: 100px;                height: 100px;                background-color: green;                background-clip: content-box;            }            .div2{                positon: absolute;                width: 50px;                height: 50px;                background-color: yellow;                left: 10px;//此处left会以padding开始定位            }        </style>    </head>    <body>        <div class='div1'>div1            <div class='div2'>div2</div>        </div>    </body></html>
Copy after login
2. fixed: positioned relative to the visible part of the browser; (depends on the browser)

3. relative: positioned relative to the normal flow ; (Depends on the normal position of the element itself)

4. Absolute: Positioning relative to the first parent element whose position attribute is not static (it’s really a twist, this is something I didn’t grasp at the beginning. If There is no position attribute in the parent element, or the position attributes are all static, which will rely on body positioning)

Of course, there are four diamonds in the box model: content, padding, border, and margin. Keep in mind that absolute will position

based on content adding (that is, the position where padding starts). Rendering:

 1 <html> 2     <head> 3         <style type="text/css"> 4             .div1{ 5                 position: relative; 6                 border: 10px solid red; 7                 padding: 10px; 8                 width: 100px; 9                 height: 100px;10                 background-color: green;11                 background-clip: content-box;12             }13 14             .div2{15                 positon: absolute;16                 width: 50px;17                 height: 50px;18                 background-color: yellow;19                 margin: 10px;20             }21         </style>22     </head>23 24     <body>25         <div class='div1'>div126             <div class='div2'>div2</div>27         </div>28     </body>29 </html>
Copy after login

A pitfall was discovered during the experiment! ! ! ! ! ! What will be the effect if left is changed to margin in the above code?

Although only one attribute has been changed, please attach the code:

Still the rendering:

The margin attribute is based on the content of the parent element.

Hmm, don’t be fooled next time.

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

Python's dir() function: View the properties and methods of an object Python's dir() function: View the properties and methods of an object Nov 18, 2023 pm 01:45 PM

Python's dir() function: View an object's properties and methods, specific code example required Summary: Python is a powerful and flexible programming language, and its built-in functions and tools provide developers with many convenient features. One of the very useful functions is the dir() function, which allows us to view the properties and methods of an object. This article will introduce the usage of the dir() function and demonstrate its functions and uses through specific code examples. Text: Python’s dir() function is a built-in function.

Flexible application skills of position attribute in H5 Flexible application skills of position attribute in H5 Dec 27, 2023 pm 01:05 PM

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

CSS layout property optimization tips: position sticky and flexbox CSS layout property optimization tips: position sticky and flexbox Oct 20, 2023 pm 03:15 PM

CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

What is the role of pageXOffset attribute in JavaScript? What is the role of pageXOffset attribute in JavaScript? Sep 16, 2023 am 09:17 AM

If you want to get the pixels to which the document is scrolled from the upper left corner of the window, use the pageXoffset and pageYoffset properties. Use pageXoffset for horizontal pixels. Example You can try running the following code to learn how to use the pageXOffset attribute in JavaScript - Live Demonstration<!DOCTYPEhtml><html> <head> <style> &amp

Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

How to use position in h5 How to use position in h5 Dec 26, 2023 pm 01:39 PM

In H5, you can use the position attribute to control the positioning of elements through CSS: 1. Relative positioning, the syntax is "style="position: relative;"; 2. Absolute positioning, the syntax is "style="position: absolute;" "; 3. Fixed positioning, the syntax is "style="position: fixed;" and so on.

See all articles