(轉載)html中div使用自動高度

來源:互聯網
上載者:User

標籤:hot   靈活   分享   isp   middle   注意   原則   class   邊框   

 

為什麼要使用div標籤

1.更多的配置項,那就意味著更靈活,當然,難度也更高;2.可以方便的容納其他html標籤;    static定位就是不定位,出現在哪裡就顯示在哪裡,這是預設取值,只有在你想覆蓋以前的定義時才需要顯示指定;relative 就是相對元素static定位時的位置進行位移,如果指定static時top是50象素,那麼指定relative並指定top是10象素時,元素實際top就是60象素了。absolute絕對位置,直接指定top,left,right,bottom。有意思 的是絕對位置也是“相對”的。它的座標是相對其容器來說的。容器又是什麼呢,容器就是離元素最近的一個定位好的“祖先”,定位好的意思就是其 Position是absolute或fixed或relative。如果沒有這個容器,那就使用瀏覽器初始的,也就是body或者html元素。標準是 說只需要指定left和right,width可以自動根據容器寬度計算出來,可惜ie不支援。fixed:fixed才是真正的絕對位置,其位置永遠相對瀏覽器位置來計算。而且就算使用者滾動頁面,元素位置也能相對瀏覽器保持不變,也就是說永遠可以看到,這個做一些彩單的時候可以用。可惜的是ie還不支援。    可以用“流”的概念來理解div的position屬性,一個html文檔可以看成一棵樹,relative和static是在流中的,講究先後順序,位置和父節點及前面的兄弟節點是相關的,而absolute和fixed不在流中,不講先後順序,只和父節點相關。float屬性    float指定了div的浮動模式,可取none|left|right,並使div丟失clear:both和display:block的樣式,並使div不會向“自動高度”的父div索要位置,在下面自動高度裡有講到。height屬性    height指定裡div的高度,如果指定裡height屬性,就算高度不夠容納所有子項目,也不會被撐開。自動高度    未指定height屬性時,div就會自動計算自己的高度。使用好div的自動高度,並不是一件很容易的事,我總結了一條原則:必須高到足夠容納最後一個向自己“索要”位置的子項目。一般子項目都認為會向div索要位置,但設定了float屬性的div標籤是不會的    代碼:<div style="width:200px;border:1px solid red;">
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
</div>    效果:    可以看到,紅色邊框的div並沒有被撐開。    下面我們加點代碼:

<div style="width:200px;border:1px solid red;">
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="clear:both;"></div>
</div>

    效果:    把紅色的代碼上移試試:

<div style="width:200px;border:1px solid red;">
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="clear:both;"></div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
</div>

    效果:    現在,讀者應該理解:必須高到足夠容納最後一個向自己“索要”位置的子項目的意義了。    如果父元素也設定float屬性呢?代碼:

<div style="float:left;width:200px;border:1px solid red;">
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
</div>

    效果:    這下紅框的div撐開了,但它把問題拋給了它的上級。    如果不想在每段代碼的地方都加一個空的div,那就用css來解決吧,css代碼:.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;  overfolw:hidden;    visibility: hidden;
}    html代碼:<div style="width:200px;border:1px solid red;" class="clearfix">
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
    <div style="float:left;width:80px;height:80px;border:1px solid blue;">TEST DIV</div>
</div>    效果也會撐開父div。就不貼圖了。注意:某些windows系統下的某些版本可能並不支援通過css這樣設定。原部落格地址:html中div使用自動高度,即不設定height屬性要注意的問題

(轉載)html中div使用自動高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.