go to  ForumEasy.com   
CSS + jQuery  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » CSS Layout » CSS Float
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: CSS Float
CyberSpider
member
offline   
 
posts: 47
joined: 04/10/2016
from: San Francisco, CA
  posted on: 04/20/2016 05:56:33 AM    Edit  |   Quote  |   Report 
CSS Float
The CSS float property is used, in similar way as position:absolute, to instruct device that the target element should be taken away from its normal flow and placed along the left or right side of its container. In contrast to position:absolute which may overlap other elements, float will squeeze the inline elements as if those inline elements are wrapping around the floated element.

The value list of float are:
  • none -- default;
  • left -- to the left side of its container;
  • right -- to the right side of its container.

    First, take a look at the following normal flow:
      .container {
            margin: 5px;
            border: 2px dashed #000000;
            width: auto;
            height: 220px;
        }
        .box {
            margin: 10px;
            width: 40px;
            height: 50px;
        }
        #box1 {background-color: red;}
        #box2 {background-color: green;}
    
    <DIV class="container">
        <div class="box" id="box1">Box#1</div>
        <div class="box" id="box2">Box#2</div>
    This is some text. This is some text. This is some text. 
    This is some text. This is some text. This is some text. 
    This is some text. This is some text. This is some text. 
    This is some text. This is some text. This is some text. 
    This is some text. This is some text. This is some text.  
    </DIV>
    
    

    Try it yourself in sandbox »

    As it can be seen, the normal flow is that each box starts with a new line and shows up one after another, followed by some text.


    Now let the box#1 float to left and box#2 float to right:
        #box1 {
            background-color: red;
            float: left;
        }
        #box2 {
            background-color: green;
            float: right;
        }
    

    Try it yourself in sandbox »

    As it can be seen, the two boxes are out of normal flow with one floating to the left and the other to the right. More important is that the text are not overlapped by those bloating boxes.


  •  Profile | Reply Points Earned: 0
    CyberSpider
    member
    offline   
     
    posts: 47
    joined: 04/10/2016
    from: San Francisco, CA
      posted on: 04/20/2016 07:18:18 PM    Edit  |   Quote  |   Report 
    Float vs Position
    What happens if float and position are both present? Who is going to win?

    Here is what we have noticed:
  • float vs position:relative -- float takes precedence!
  • float vs position:absolute -- position takes precedence!


  •  Profile | Reply Points Earned: 0
    CyberSpider
    member
    offline   
     
    posts: 47
    joined: 04/10/2016
    from: San Francisco, CA
      posted on: 07/31/2023 10:21:15 PM    Edit  |   Quote  |   Report 
    How to centralize an element?
    To center a box/block DIV:
    .center_div {
        margin: auto;
        width: 80%;
    }
    


    To center text:
    .center_text {
        text-align: center;
    }
    
     Profile | Reply Points Earned: 0

     
    Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.