CSS sets three common positions for position positioning, cssposition

Source: Internet
Author: User

CSS sets three common positions for position positioning, cssposition

In CSS, you can set a position attribute value for an element to display different elements in different positions, fixed positions, or pages.

PositionCan be setRelative,Absolute,Fixed,Static.

Two blocks are defined in the HTML code:

1 <div class="box">2     <div class="box1"></div>3 </div>

1. Relative positioning:

1 .box {2     width: 50px;3     height: 50px;4     position: relative;5     top: 50px;6     left: 10px;7 }

The above code moves the box 50px to the right at the initial position of the box, and moves 10 PX down.

2. Absolute positioning:

1 .box1 {2     width: 10px;3     height: 20px;4     position: absolute;5     top: 10px;6     left: 10px;7 }

The above code moves the box1 position to the origin of the current viewport position as the reference position;

If relative is set for the parent box1, The box1 will be shifted based on the origin of the box.

 1 .box { 2     position: relative; 3 } 4 .box1 { 5     width: 10px; 6     height: 20px; 7     position: absolute; 8     top: 10px; 9     left: 10px;10 }

3. Fixed positioning:

1 .box {2     width: 50px;3     height: 50px;4     position: fixed;5     top: 20px;6     left: 0px;7 }

The above code will fix the box to the left edge position of 20 px from the top of the window.

Note:

1. Absolute positioning will remove the element from the document stream. The element located after the positioning element will be moved to the position of the element to fill the blank area;

2. In general, it will be used together with ixnagdui in absolute positioning;

3. When using fixed and absolute, you must identify who is the benchmark position.

Instance:The image is displayed in a fixed position (not the top or bottom) of the window, and the relative position of the element is not changed as the window scales.

1 <div class="aside-cover">2     <div class="cover">3          <div class="aside-left">4               5          </div>6     </div>7 </div>
. Cover {width: 500px; height: 364px; margin: auto; position: relative;}/* level 2 sublevel absolute positioning */. aside-left {width: 60px; height: 94px; position: absolute; top: 422px; left:-0.2%; overflow: hidden;}/* relative position of the image */. aside-left img {position: relative; left:-64px; top:-125px ;}

With the above code, you can display the image content area to a fixed position in the window.

 

 

 

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.