CSS中的高度和寬度

來源:互聯網
上載者:User

標籤:style   color   ar   div   type   ad   ef   text   

1.簡單的說,常規流向的區塊層級元素,width為auto時,會盡量充滿父元素的內容寬度,而height為auto時,則是由其內部的不浮動的子項目的高度決定(無浮動,絕對位置)。

 

2.width:100%;width:auto

<div>
    <p>1</p>
</div>
<style type="text/css">
div{
  width:600px;
  overflow:auto;
}
p{

  width:100%;
      padding:10px;
}
p{
      width:auto;
      padding:10px;
}
</style>
如果是width:100%,則說明p的width會得到600px就已經充滿div地區,然後自己又有padding,所以會出現捲軸。
而width:auto則比較聰明,它是總體寬度(廣義,包括width,margin,padding,border這些)等於父級寬度(狹義,內容區,僅指width),所以如果padding已經左右佔去20px的空間,那麼width給的值就是580px。

所以,如果:

p{
     width:auto;
     background-color: red;
     margin:10px;
     padding:10px;
 }

其實最後實際的width值為560px。
但無論是width:100%還是auto,其計算的參照都是父級內容區width值,而非總寬度值。。

 

 

3.height:100%;height:auto

auto是隨內容的高度而撐開的。100%是根據父級元素的高度來決定的。

例如:

<div style="height:100px;width:200px;">  

    <div style="height:auto;width:100px;float:left;">這個容器的高度是隨裡面的內容的高度而定

  </div>  

    <div style="height:100%;width:100px;float:right;">這個容器的高度為父級的高度,100px

  </div>

</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.