Css positioning details demo with instances

Source: Internet
Author: User

CSS relative positioning

Relative positioning is a very easy-to-understand concept. If an element is relatively located, it appears at its location. Then, you can set the vertical or horizontal position to move the element "relative to" its start point.

If you set top to 20px, the box is 20 pixels at the top of the original position. If left is set to 30 pixels, a 30-pixel space is created on the left of the element, that is, the element is moved to the right.

The code is as follows: Copy code
# Box_relative {
Position: relative;
Left: 30px;
Top: 20px;
}

As shown in the following figure:

 

Note that when relative positioning is used, elements still occupy the original space, regardless of whether or not they are moved. Therefore, moving an element overwrites other boxes.

Absolute CSS positioning

Absolute positioning makes the element position irrelevant to the document stream, so it does not occupy space. This is different from relative positioning. Relative positioning is actually considered as part of the normal stream positioning model, because the position of an element is relative to its position in the normal stream.

The layout of other elements in a normal stream is like that of an absolutely positioned element that does not exist:

The code is as follows: Copy code
# Box_relative {
Position: absolute;
Left: 30px;
Top: 20px;
}

As shown in the following figure:

 

The position of an absolutely located element is relative to the nearest located ancestor element. If the element does not have the located ancestor element, its position is relative to the initial contained block.


Through this demo, we can quickly help you understand the various positioning problems of css.

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> Untitled Document </title> <style>
Body
{Margin: 30px; font-size: 9pt ;}
  
. A,. B,. c,. d,. e
{
Width: 100px;
Height: 100px;
Margin: 5 auto;
Color: # fff;
Background: #000;
}
. Aa,. bb,. cc,. dd,. ee
{
Top: 10px;
Left: 10px;
Width: 10px;
Height: 10px;
Overflow: hidden;
Background: # F90;
}
. B,. d,. e
{Position: relative ;}
. Cc,. dd,. ee
{Position: absolute ;}
</Style>
</Head>
  
<Body>
  
  
<Div class = "a">
<Div class = "aa"> </div>
A: No postion is set. Generally, the nested relationship is not set.
</Div>
  
<Div class = "B">
<Div class = "bb"> </div>
B: Set relative only for external div, which is usually nested.
</Div>
  
<Div class = "c">
<Div class = "cc"> </div>
C: set absolute only for the inner div. The document contains the nested relationship. The inner div in the page float [non-float], which is independent of the outer div relative to the page positioning.
</Div>
  
<Div class = "d" style = "background: # ff0000">
<Div class = "dd"> </div>
D: Set relative for the external div, and absolute for the inner div. The inner div floats and locates relative to the outer div.
</Div>
  
<Div class = "d" style = "background: # ff0000">
<Div class = "dd" style = "position: relative"> </div>
D: Set relative for the external div, set relative for the inner div, and float the inner div and locate it relative to the outer div.
</Div>
  
<Div class = "e">
<Div class = "ee" style = "left:-10px;"> </div>
E: This indicates the boundary issue. -10! = Reverse 10px spacing
</Div>
  
</Body>
</Html>

Related Article

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.