Css absolute positioning absolute and relative positioning relative examples

Source: Internet
Author: User
For those who are not familiar with CSS, it is very difficult to grasp the absolute location of the absolute cssposition and relative location of relative, even those who have a certain css foundation may not be able to fully master the use of absolute and relative in css. This article describes how to use absolute and relative positioning methods and techniques through examples. For more information, see the examples in this article. 1. If the position attribute value of an element is absolute, there are two situations:

1. if the position attribute (static by default) is not defined in the parent (including direct and indirect parent) element, the element is located based on the page, top, left, and other properties will refer to the page.

If the width and height attributes of the element are percentages, the two attributes refer to the width and height of the page.


  

In the above Code, p2 is absolute positioning, and its parent-level element does not set the position attribute, so this element will be located based on the page.

You can copy the above instance code and run it here to check the effect.

From the running effect, we can see that p2 is located relative to the page, and its width and height are half of the width of the page.

2. If the parent element defines the position attribute (relative or absolute), it will refer to the parent element.


  

p1

p2

You can copy the above instance code and run it here to check the effect.

From the running effect, we can see that p2 is located relative to p1, and the width and height are also relative to p1.

2. If the position attribute value of an element is relative, the element is located relative to its normal position (I .e. position: static.

By default, the width of the parent container is full, and the height is determined by the content. When it is the first child element of the parent element, the position is in the upper left corner of the parent element.

You can set the position and size of the container as the reference, and the height is special. When the height is set, the status full of the parent container is used as the reference.


  

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.