Positioning problems in CSS: absolute positioning, relative positioning, fixed and static

Source: Internet
Author: User
This article brings to you the content is about the CSS positioning problems: absolute positioning, relative positioning, fixed and static, there is a certain reference value, the need for friends can refer to, I hope you have some help.

1. Absolute Positioning (absolute): Positioning from the original position can be positioned using the left right top bottom

HTML code

<! DOCTYPE html>

CSS code:

*{margin:0;padding:0;}. Demo{position:absolute;/*top:100px;left:100px;*/width:100px;height:100px;background-color:red;opacity:0.5;     /* Transparency */}.box{width:150px;height:150px;background-color:green;}

No effect with opsition positioning (p element is block level element, exclusive row)

Plus positioning, no top left effect is set (the demo and box are not at the same level)

Plus top left effect

2. Relative positioning (relative): keep the original position for positioning, you can use the left right top bottom to locate

HTML code:

<! DOCTYPE html>

CSS Code

*{margin:0;padding:0;}. demo{position:relative;/*top:100px;left:100px;*/width:100px;height:100px;background-color:red;opacity:0.5;     /* Transparency */}.box{width:150px;height:150px;background-color:green;}

When you are not using opsition targeting

When using opsition to position without top left

Use opsition to position with top left (keep the original red position, and the upper part is empty)

3.fixed: The element is positioned relative to the browser's window and can be positioned using the left right top bottom

4.static: default value, no positioning, top left etc does not work

Note: When a parent-child element is used for positioning

Absolute: Positioning relative to the nearest targeted parent, without the most relative document positioning
Relative: Positioning relative to your original position

Typically, we use relative as a reference and use absolute for positioning

For example:

<div class= "wrapper" >    <div class= "Demo" >            <div class= "box" ></div>    </div ></div>

Shut up box to locate with reference to the demo

*{margin:0;padding:0;}. Wrapper{margin-left:100px;width:200px;height:200px;background-color:orange;}. demo{position:relative; margin-left:100px;width:100px;height:100px;background-color:red;}. Box{position:absolute;left:50px;width:50px;height:50px;background-color:green;} 
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.