Afraid to forget the absolute and relative usage of position in CSS

Source: Internet
Author: User

The positioning in CSS2.0 does sometimes confuse people, so write it down today and check it out later. The following is a part of the content of the W3cschool and divcss5 the two official website, here specifically to explain

CSS2.0 position is mainly used for the positioning of HTML elements, which is very useful in practical design. But for learners, they will encounter relative positioning and absolute positioning in the end how to use, very hit skull

No matter how why did the skull, it is important to remember several of position's property values

    • Default positioning method for static:html elements
    • Absolute: Drag objects out of the document stream and use attributes such as left,right,top,bottom for absolute positioning. And its cascade is defined by the Z-index property. The position of an absolutely positioned element is relative to the nearest positioned parent element, if the element has no positioned parent element, then its position is relative to the
    • Relative: objects are not stackable but will be offset in normal document flow based on attributes such as Left,right,top,bottom
    • Fixed: The position of the element relative to the browser window is pinned, and it does not move even if the window is scrolled

Because static and fixed are relatively simple, so the following case we introduce a relative and absolute this two attributes. First look at a case

The above consists of 3 Div, the outer div name is scroll, the 2nd div is a sub, and the 3rd div is named Ssub. Now I'm going to tell you how to use position absolute and relative.

Remember the following formula

    • The absolute positioning (absolute) position is relative to the most recently positioned parent element, and if the parent element itself is not using position positioning, then the document (HTML) is positioned relative to
    • Absolute positioning uses usually the parent defines position:relative positioning, the child defines the Position:absolute absolute positioning attribute, and the child uses left or right and top or bottom for absolute positioning

Case code

<!DOCTYPE HTML><HTML><Head><style>div.scroll/* Parent element, note the use of relative*/{Background-color:#00FFFF;width:350px;Height:350px;position:relative;}div.sub{/*-2nd child element * /width:200px;Height:200px;Border:1px solid Red;position:Absolute; Left:10px;Top:10px;}div.ssub{/* innermost element * /width:100px;Height:100px;Border:1px solid Red;position:Absolute; Left:10px;Top:10px;}</style></Head><Body><Divclass= "Scroll">  <Divclass= "Sub">    <Divclass= "Ssub"></Div>  </Div></Div></Body></HTML>

CSS positioning summary (from the DIVCSS5 official website summary, I feel very good, just write down). Thanks divccs5,http://www.divcss5.com/rumen/r403.shtml.

Usually we use position:absolute;position:relative for absolute positioning layout, to define positioning through CSS, div layout html, note where to use position:relative, Where to use Position:absolute for positioning, and do not forget to use the left, right, top, bottom to locate the specific location of the mate. Absolute positioning if the parent does not use position:relative, and the direct use of position:absolute absolute positioning, this time will be the body tag as the parent, using Position:absolute to define the object regardless of the number of layers in the div structure, Will be dragged out to be absolutely positioned at <body> for the parent (reference level). Absolute positioning is very useful, but remember not to misuse, where all use, so sometimes lazy to calculate the distance up, down, left, right, and may cause the CSS code bloated


In absolute positioning we can use CSS Z-index to define the CSS layer overlap order. You can use the Photoshop PS slicing tool to get accurate values for left, right, bottom, and top values.

Afraid to forget the absolute and relative usage of position in CSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.