zoukankan      html  css  js  c++  java
  • CSS float

    <div style="border:1px solid #cccccc;">
    
      This is the first text
    
      <div style="border: 2px solid red;">This is box 1</div>
      <div style="border: 2px solid green;">This is box 2</div>
    
      This is the last text
    </div>
    

      

     

    Table of Contents

    The float CSS property can make HTML elements float to the left or right inside their parent element. Content inside the same parent element will move up and wrap around the floating element.

    Look at this HTML code:

    
    

    The HTML code contains a div element which has a text, two div elements and another text inside its body. When rendered, here is what that looks like:

    HTML elements with no float

    In the following sections I will show you how to make the two nested div elements float left and right inside their parent element.

    float : left

    Now, let us try to make the first nested div element float left using the float CSS property. We do so by settingfloat to the value left. Here an example showing that:

    <div style="border:1px solid #cccccc;">
    
      This is the first text
    
      <div style="float: left; border: 2px solid red;">This is box 1</div>
      <div style="border: 2px solid green;">This is box 2</div>
    
      This is the last text
    </div>
    

      

    Here is what the example looks like with the left floating element when rendered in the browser:

    HTML elements with one float : left element

    Notice how the first div element (with the red border) now floats to the left inside its parent element. The first text is now wrapping nicely around the first div element, to the right of it. The second div element is still positioned below the first div element, and the last text below that.

    Now, let us try to make the second nested div float left too. Here is what the code looks like:

    <div style="border:1px solid #cccccc;">
    
      This is the first text
    
      <div style="float: left; border: 2px solid red;">This is box 1</div>
      <div style="float: left; border: 2px solid green;">This is box 2</div>
    
      This is the last text
    </div>
    

      

    And here is what the code looks like when rendered in the browser:

    HTML elements with two float : left elements

    Now both the first and second nested div element is floating to the left inside their parent element. The text wraps nicely around the two floating elements.

    float : right

    Now let us try to make the second nested div float to the right instead. We do so by setting its float CSS property to right instead of left. Here is the code:

    <div style="border:1px solid #cccccc;">
    
      This is the first text
    
      <div style="float: left; border: 2px solid red;">This is box 1</div>
      <div style="float: right; border: 2px solid green;">This is box 2</div>
    
      This is the last text
    </div>
    

      

    Here is what it looks like when rendered in the browser:

    HTML elements with a float : left and float : right element

    See how the two elements are now floating in each their direction inside their parent element. They are even located at the same vertical position (towards the top of the parent element). The text now wraps around the two divelements by being rendered between the div elements.

    clear

    Now, let us add a third div element which also floats left:

    <div style="border:1px solid #cccccc;">
    
      This is the first text
    
      <div style="float: left; border: 2px solid red;">This is box 1</div>
      <div style="float: right; border: 2px solid green;">This is box 2</div>
      <div style="float: left; border: 2px solid red;">This is box 3</div>
    
      This is the last text
    </div>
    

      

    This is what the code looks like now, when rendered in a browser:

    HTML elements with no float

    Notice how the two left floating elements are positioned at the same horizontal "line", after each other.

    Imagine now that you want the two div elements with the red borders to float left, but under each other instead of next to each other. To do that, you need to set the clear CSS property.

    The clear CSS property can take one of these values:

    • left
    • right
    • both
    • none

    The left value means that the element should stay clear of all left floating elements. The right value means that the element should stay clear of all right floating elements. The both value means that the element should stay clear of both left and right floating elements. The none value means no clearing, which is the same as leaving out theclear CSS property.

    Let us set the clear CSS property of the last div element to left. Then the last div element should still float left, but stay clear of the first left floating div element. Here is how the code looks:

    <div style="border:1px solid #cccccc;">
    
      This is the first text
    
      <div style="float: left;  border: 2px solid red;">This is box 1</div>
      <div style="float: right; border: 2px solid green;">This is box 2</div>
      <div style="float: left; clear: left border: 2px solid red;">This is box 3</div>
    
      This is the last text
    </div>
    

      

    Here is what the code looks like when rendered in a browser:

    HTML elements with no float

    Notice how the last div element still floats left, but stays under (clear of) the first left floating div element.

  • 相关阅读:
    关于css兼容性问题及一些常见问题汇总
    CSS3使用transition属性实现过渡效果
    CSS3 画基本图形,圆形、椭圆形、三角形等
    总结30个CSS3选择器
    javascript中call()、apply()的区别
    JavaScript面试技巧之数组的一些不low操作
    详解bootstrap-fileinput文件上传控件的亲身实践
    js控制随机数生成概率代码实例
    jQuery 第十章 工具方法-高级方法 $.ajax() $.Callbacks() .....
    jQuery 第九章 工具方法之插件扩展 $.extend() 和 $.fn.extend()
  • 原文地址:https://www.cnblogs.com/hephec/p/4592137.html
Copyright © 2011-2022 走看看