CSS之float vs position:absolute

來源:互聯網
上載者:User

標籤:.com   cli   utf-8   log   anything   -o   images   1.0   存在   

補充:ul 應該設定下 list-style: none;

 

題外話:看了張鑫旭的視頻,這傢伙把簡單的css玩出了新花樣,絕對大神級的存在。膜拜下先~

 

float的作用前面一章已經說過了,但沒考慮過的是 position:absolute 作用與float極其類似。

二者都是將自身浮空(垂直於螢幕) -- 其實是將其容器高度塌陷。

不同點在於:float會改變位置,但 position:absolute 則會儲存其原有位置!!!

 

利用 position:absolute 的這個特性,我們可以更好的實現一個搜尋方塊的功能:

代碼如下,

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>POSITION:ABSOLUTE vs FLOAT</title>    <style>        ul,        li,        input,        button {            padding: 0;            margin: 0;        }        .box,        section {            width: 254px;            height: 32px;            border: 1px solid #e6e8e9;            box-sizing: content-box;            overflow: hidden;        }        input {            width: 200px;            height: 30px;            border: 1px solid #009527;            box-sizing: content-box;            /*padding-left: 5px;*/            text-indent: 5px;            text-overflow: ellipsis;        }        button {            width: 50px;            height: 30px;            border: 1px solid #009527;            box-sizing: content-box;        }        ul{            color: #333;            border: 1px solid #999;            margin-top: -1px;            margin-left: 1px;            /*transition: all 1.25s;*/        }        li{            width: 252px;            background-color: #d5d7d9;            text-indent: 10px;        }    </style></head><body>    <h3>使用absolute!!!</h3>    <div class="box">        <section>            <input type="text" name="key" placeholder="Anything you wanna know..."><button type="button">Go!</button>        </section>        <div>            <ul>                <li>AAA</li>                <li>BBB</li>                <li>CCC</li>                <li>DDD</li>                 <li>EEE</li>            </ul>        </div>    </div></body><script>    var count=0;    var btn = document.getElementsByTagName(‘button‘)[0];    btn.onclick = function () {        count++;        var ul = document.getElementsByTagName(‘ul‘)[0];        if(count%2)            ul.style.position = ‘absolute‘;        else            ul.style.position = ‘static‘;    }</script></html>
View Code

 

其效果如下:

CSS之float vs position:absolute

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.