search
HomeWeb Front-endHTML TutorialWhat are the positioning models in CSS?

What are the positioning models in CSS?

Jun 24, 2017 pm 02:00 PM
csspositionModel

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;*/
    }
    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

    .<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>
    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
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
The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 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.

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.

What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?Apr 05, 2025 pm 01:24 PM

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...

What HTML elements does the online code editor use to implement code input?What HTML elements does the online code editor use to implement code input?Apr 05, 2025 pm 01:21 PM

HTML Element Analysis in Web Code Editor Many online code editors allow users to enter HTML, CSS, and JavaScript code. Recently, someone proposed...

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)