css中的幾個小tip(一),csstip(

來源:互聯網
上載者:User

css中的幾個小tip(一),csstip(

原博:http://www.webhek.com/post/7-tips-web-front-developer-must-know__trashed.html

1.元素的margin-top、margin-bottom及padding-top、padding-bottom使用百分比作為單位時,其是相對父元素的寬度width,而不是我們想象的高度height;

<style type="text/css">        .parent{            outline: 1px solid orange;            width: 200px;            height: 300px;            padding: 10px;            margin: 10px;        }        .child{            outline: 1px solid purple;            width: 200px;            height: 80px;            padding-top: 10%;      /*20px = 父容器的width值200px * 10% */            padding-bottom: 5%;     /*10px = 200px * 5% */            margin-top: 20%;      /*40px = 200px * 20%*/            margin-bottom: 15%;    /*30px = 200px * 15%*/        }<body>    <div class="parent">        <div class="child"></div>    </div></body>

子盒子參數如下:

 

2.含有定位屬性的元素,其top、bottom單位為百分比時,該百分比是相對父元素的高度height。left、right則是相對父元素的寬度width.

.parent{            outline: 1px solid orange;            width: 200px;            height: 300px;            padding: 0px;            margin: 0px;            position: relative;        }        .child{            outline: 1px solid purple;            width: 200px;            height: 80px;            position: absolute;            top: 5%;  /* 15px = 300px * 5% 上邊框離父盒子上邊框15px的距離*/            left: 20%;  /*  40px = 200px * 20% 左邊框離父盒子左邊框40px的距離  
                   也就是子盒子左上方的座標為x=15,y=40(父盒子左上方為原點) */ }

3.邊框寬度不能用百分比表示

4.width:100%

4.1當父容器裡有絕對位置的子項目時,子項目設定width:100%實際上指的是相對於父容器的padding+content的寬度。

r

 1 <style type="text/css"> 2     .parent{ 3         outline: 1px solid orange; 4         width: 200px; 5         height: 300px; 6         padding: 10px; 7         margin: 10px; 8         position: relative; 9     }10     .child{11         outline: 1px solid purple;12         width: 100%;  /* width = 220px = 父容器的padding+content*/13         height: 80px;14         position: absolute;15         top: 0;16         left: 0;17     }18 </style>

 

 

4.2 當子項目是非絕對位置的元素時(可以是相對定位),或者如果都沒有定位,width:100%才是指子項目的 content ,其等於父元素的 content寬度。

.parent{    outline: 1px solid orange;    width: 200px;    height: 300px;    padding: 10px;    margin: 10px;}.child{    outline: 1px solid purple;    width: 100%;  /* width:200px = 父盒子的content*/    height: 80px;}
.parent{    outline: 1px solid orange;    width: 200px;    height: 300px;    padding: 10px;    margin: 10px;    position: relative;}.child{    outline: 1px solid purple;    width: 100%;     height: 80px;    position: relative;}

聯繫我們

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