margin、padding單位百分比

  年前作了一個測試題 https://www.wenjuan.com/s/VjaEva/,裏面有一道題目涉及到了margin和padding單位爲百分比的狀況。寫出來記錄一下以防止本身忘記。css

  

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <style type="text/css">
 7         .warp{
 8             width:500px;
 9             height:300px;
10             overflow: hidden;
11             background-color: #279186;
12             position: relative;
13         }
14         .ctx{
15             padding:10%;
16             margin:10%;
17             width:100px;
18             height:50px;
19             background-color: #A56D1C;
20         }
21         .top{
22             position: absolute;
23             width: 100%;
24             top:50px;
25             left:0;
26             padding:0;
27             margin:0;
28             color:#333;
29             height:1px;
30             border:0 none;
31             background-color: #333;
32         }
33         .left{
34             position: absolute;
35             width:1px;
36             height:100%;
37             left:50px;
38             top:0;
39             background-color: #333;
40         }
41     </style>
42 </head>
43 <body>
44     <div class="warp">
45         <div class="ctx"></div>
46         <!-- 輔助線 -->
47         <hr class="top " />
48         <div class="left "></div>
49     </div>
50 </body>
51 </html>

在瀏覽器裏表示爲 html

能夠見到,padding和margin的單位,均可以用百分比來表示。chrome

以後我又在其餘瀏覽器裏測試了一下兼容性,雖然這種百分比單位的方式在PC端用的不是很廣泛。瀏覽器

只有IE6下出現了問題。 ie6下marginTop的距離是不對的 IE6效果以下測試

 可是在chrome,IE 7 8 9 10 ,FireFox瀏覽器上都表現良好spa

相關文章
相關標籤/搜索