---
title: "css的浮动"
author: "angowang"
date: "2014-09-18T00:12:31Z"
tags: []
categories: ["Web前端"]
slug: "css"
draft: false
lastmod: "2014-09-18T22:11:32Z"
id: 2117566
url: "https://angowang.iteye.com/blog/2117566"
---

1.浮动的标签：脱离文档流的标签元素；

2.css的float属性和position属性都可以让标签元素浮动起来；

3.示例：

float示例一：

具有float属性的标签脱离文档流，其原本的位置被紧接着的元素占有

&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
    &lt;head&gt;  
        &lt;meta charset="utf-8" /&gt;  
        &lt;title&gt;xxx&lt;/title&gt;  
        &lt;style type="text/css"&gt;  
            .boxLeft {  
                width: 400px;  
                height: 400px;  
                background: red;

                float: left;  
            }  
            .boxRight {  
                width: 400px;  
                height: 400px;  
                background: green;  
            }  
        &lt;/style&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div class="boxLeft"&gt;&lt;/div&gt;  
        &lt;div class="boxRight"&gt;&lt;div&gt;  
    &lt;/body&gt;  
&lt;/html&gt;

float示例二：

子标签具有float属性，为防止子元素内容溢出，父元素添加overflow：auto

&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
    &lt;head&gt;  
        &lt;meta charset="utf-8" /&gt;  
        &lt;title&gt;xxx&lt;/title&gt;  
        &lt;style type="text/css"&gt;  
            .boxParent {  
                width: 400px;  
                background: red;  
                overflow: auto;  
                zoom: 1;  
            }  
            img {  
                float: right;  
            }  
        &lt;/style&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div class="boxParent"&gt;  
            &lt;img src="http://www.learnlayout.com/images/ilta.png" /&gt;  
        &lt;/div&gt;  
    &lt;/body&gt;  
&lt;/html&gt;

float示例三：

避免紧接着的元素占用有float属性元素的原本位置

&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
    &lt;head&gt;  
        &lt;meta charset="utf-8" /&gt;  
        &lt;title&gt;xxx&lt;/title&gt;  
        &lt;style type="text/css"&gt;  
            .boxLeft {  
                width: 400px;  
                height: 400px;  
                background: red;  
                float: left;  
            }  
            .boxRight {  
                width: 400px;  
                height: 400px;  
                background: green;  
                clear: left;  
            }  
        &lt;/style&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div class="boxLeft"&gt;&lt;/div&gt;  
        &lt;div class="boxRight"&gt;&lt;div&gt;  
    &lt;/body&gt;  
&lt;/html&gt;

position示例一：

position的relative相对于其原本应占有的位置

&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
    &lt;head&gt;  
        &lt;meta charset="utf-8" /&gt;  
        &lt;title&gt;xxx&lt;/title&gt;  
        &lt;style type="text/css"&gt;  
            .box {  
                width: 400px;  
                height: 400px;  
                background: red;  
                position: relative;  
                top: 50px;  
                left: 50px;  
            }  
        &lt;/style&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div class="box"&gt;  
        &lt;/div&gt;  
    &lt;/body&gt;  
&lt;/html&gt;

position示例二：

position的absolute是相对于父元素的relative以上属性

 &lt;!DOCTYPE html&gt;  
&lt;html&gt;  
    &lt;head&gt;  
        &lt;meta charset="utf-8" /&gt;  
        &lt;title&gt;xxx&lt;/title&gt;  
        &lt;style type="text/css"&gt;  
            .boxOutside {  
                width: 400px;  
                height: 400px;  
                background: red;  
                position: relative;  
            }  
            .boxInside {  
                width: 200px;  
                height: 200px;  
                background: green;  
                position: absolute;  
                bottom: 0px;  
                right: 0px;  
            }  
        &lt;/style&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div class="boxOutside"&gt;  
            &lt;div class="boxInside"&gt;&lt;div&gt;  
        &lt;/div&gt;  
    &lt;/body&gt;  
&lt;/html&gt;

position示例三：

position的fixed相对于浏览器的窗口

&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
    &lt;head&gt;  
        &lt;meta charset="utf-8" /&gt;  
        &lt;title&gt;xxx&lt;/title&gt;  
        &lt;style type="text/css"&gt;  
            .box {  
                width: 400px;  
                height: 400px;  
                background: red;  
                position: fixed;  
                right: 0px;  
                bottom: 0px;  
            }  
        &lt;/style&gt;  
    &lt;/head&gt;  
    &lt;body&gt;  
        &lt;div class="box"&gt;  
        &lt;/div&gt;  
    &lt;/body&gt;  
&lt;/html&gt;