Table of Contents
Positioning model June 8, 2017 fanbright
position:absolute;
left:0; and right:0;
Note that left and right
Four relative positioning
Five floating positioning and reset
Use float to make some elements floating elements. Floating elements can be set to relative positioning through relative. Relative floating elements are still in the regular flow where the floating elements are located. You can use left and top to set its offset position in the flow. .
In css, if the parameter value is 0, do not add units,
Home Web Front-end HTML Tutorial What are the positioning models in CSS?

What are the positioning models in CSS?

Jun 24, 2017 pm 02:00 PM
css position Model

Positioning model June 8, 2017 fanbright

css supports 6 positioning models

  • static

  • Absolutely

  • Fixed

  • Relative

  • ##Floating

  • Relative floating


Set position

  • position:static; you can cancel the positioning setting of the element and restore it to its original position. The display mode in the regular flow.static is the default value.

  • position:relative; can offset the position of the element by a certain distance relative to the regular flow.

  • position:absolute;You can offset the element by a certain distance relative to the position of the regular flow or the position of the nearest positioned ancestor element.

  • position:fixed;You can make the element relative Offset a certain distance from the window.

  • z-index can set the stacking order of elements. The larger the value, the higher the element is.


Recently positioned ancestor element

  • If the element that sets the position does not have a positioned ancestor element, then

    becomes Positioning ancestor elements, in other words, is the default position element.

  • ##The most recently positioned element must be a valid ancestor element (relative|absolute|fixed ), CSS does not support positioning relative to any element in the document.
  • position:relative; is a very good way to create positioned ancestor elements because it does not leave the routine Flow. Using this method, you can create a layout that maintains regular flow and achieves absolute positioning.
  • Atomic display

    The element whose position is set is

    Atomic display

    , which means that external elements cannot appear between its static descendant elements, inline content and background. By using relative positioning, absolute positioning and fixed positioning modes, you can Elements are set to atomic display, and block-level elements set to overflow:scroll|auto are also atomically displayed

  • and are static block-level elements with no position set. When overlap occurs, their inline content will not overlap, but their
borders and background

will overlap, but text will not.

z- index

applies to all elements, the default auto

    z-index is not a global attribute, but relative to the set numeric value The z-index is determined by the most recently positioned ancestor element. The root element html will create a root stacking context. Each positioned element with a specified numeric value z-index will create a local local stacking context.
  • Static positioning elements are stacked from back to front according to the order in which the document appears.
  • Setting the position element ignores the order in which the document elements appear, but changes from small to large based on the z-index value. The largest order is stacked from back to front. Negative value set position elements are located under static positioning elements and non-set position floating elements
一static positioning model

position:static;The default is static;

    The starting position of a static element is determined by the position of the previous static element. The ## of the static element #Size
  • ,

    Padding,Border, and Margin determine the starting position of the next element.

    The vertical margins of adjacent elements will
  • merge
  • together, and the final margin is the

    larger value of the margins of the two adjacent elements

    Set the left and right margins to auto to center the
  • static block-level
  • elements with the

    set size.

  • Two absolute positioning models

position:absolute;

##Percentage
    is relative to the nearest positioned ancestor The size of the element, and the size of
  • non-parent element

    .Set the left, right, top, bottom of the element to

    auto
  • , which can restore
  • its original position in the regular flow.

    Unlike floating elements, absolute elements will not be automatically arranged and will not be affected by other elements. , will not affect other elements.

  • If all child elements of an element are set to absolute positioning, then its height will become

    0

    , all Its child elements have all left the regular flow.
  • If there is no positioned ancestor element, it will be positioned based on

  • Absolutely Positioning and centering, general elements

     div{
                position:absolute;
                width:200px;
                height:200px;
                margin:0 auto;
                border:1px solid blue;
                /*left:0;*/ 这两条没用,没有影响
                /*right:0;*/
    }
    Copy after login
    Absolute positioning and centering, static inline elementsFor static inline elements, such as em, strong, span, etc., but does not include inline replaceable elements (input, img, textarea, etc.), when using the absolute positioning mode absolute, width and right can be used. In order to be centered, you need to add

    left:0; and right:0;

    so that margin:0 auto; can take effect normally. .

    Note that left and right

    must be 0

    .<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">  #em{             position:absolute;             width:200px;             height:200px;             margin:0 auto;             border:1px solid blue;             left:0;             right:0; }</pre><div class="contentsignin">Copy after login</div></div>
    Three fixed positioning model

position:fixed;You can change any element into a fixed position element

Remember: the position of the fixedly positioned element is relative to the

window
, and
    Not
  • relative to the nearest positioned ancestor`, and the element will not scroll as the page scrolls.

  • Because it is positioned relative to the page, does not require the nearest ancestor to be positioned

  • It is best to use top and left when setting For offset positioning, when top, left, bottom, and right are set at the same time, the values ​​of top and left will be given priority . Only when top and left do not exist , bottom and right will be used. Only then will it take effect


Four relative positioning

position:relative;

  • Use left and top are used to change the position of the element. The default value of left and top is auto. Auto will keep the relatively positioned element in its original position in the regular flow.

  • Any element can be set to position:relative; Therefore, its absolutely positioned descendant elements can be positioned relative to it.


Five floating positioning and reset

Use float:left; and float:right; can make the element leave the regular flow.

  • ##Use float:none; the default is none, which can override other floating rules of the element and avoid inheriting float

  • Floating elements will not affect the position of the block-level box, but only affect the inline elements

  • ##clear:left;clear:right;clear;both;
  • Any element can be set as a floating element, clear is applicable to tables, block-level elements and floating elements
  • clear is not applicable
  • Inline

    ,Absolute positioning or Fixed positioning elements

  • ##Six relative floating positioning

Use float to make some elements floating elements. Floating elements can be set to relative positioning through relative. Relative floating elements are still in the regular flow where the floating elements are located. You can use left and top to set its offset position in the flow. .

##Only positon:relative; and position:static; are applicable to floating elements. When set to absolute and fixed, the display result is uncertain.
  • sporadic

In css, if the parameter value is 0, do not add units,

  • Before rendering the content of an element, the browser will first render its frame, starting with the background color, then the background image, then the border, and finally, the browser will render the content of the frame on top of the frame

The above is the detailed content of What are the positioning models in CSS?. 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
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles