about float and position in CSS

Source: Internet
Author: User

Postion:relative is the relative positioning of the child block-level element for the parent element, and the positional keyword uses left/right/top/bottom. The sibling blocks are positioned relative to each other, but the original position remains when the position is moved. And then the sibling block element is positioned based on the position before being removed.

Float:right/left is a child block-level element flow set targeting the parent element, and the keywords used to locate the margin/padding. The relative positioning of the sibling block elements is re-rendered based on the new position after the move, which can overlap and the original position is emptied.

The biggest difference between the two is position retention.

Float positioning:

<style>
*{border:1px solid #eee;}
body{
border-color: #09f;
}
ul{list-style:none;}
li{float:left;}
#li1 {width:200px;height:200px;border-color:red;}
#li2 {width:200px;height:150px;border-color:green;}
#li3 {width:200px;height:100px;border-color:blue;}
</STYLE>

<ul id= #id >
<li id=li1>li1</li>
<li id=li2>li2</li>
<li id=li3>li3</li>
</ul>

Position:relative positioning:

<style>
*{border:1px solid #eee;}
body{
border-color: #09f;
}
ul{list-style:none;width:800px;height:400px;margin:0 auto; Border-color:black;}
li{position:relative;}
#li1 {width:200px;height:200px;border-color:red;top:20px;}
#li2 {width:200px;height:150px;border-color:green;}
#li3 {width:200px;height:100px;border-color:blue;}
</STYLE>


<ul id= #id >
<li id=li1>li1</li>
<li id=li2>li2</li>
<li id=li3>li3</li>
</ul>

Run separately to see how it works.

about float and 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.