網上百度了幾種可以按照百分比畫方框的方法
1.CSS3 vw 單位
1vw = 1% viewport width
<div class="vw">hello,viewport</div> .vw { width: 50%; height: 50vw; background: #ccc; }
實測不管用(chrome,版本 49.0.2623.110)
2.padding-bottom
<div class="placeholder"></div> .placeholder { width: 100%; padding-bottom: 100%;/* padding百分比相對父元素寬度計算 */ height: 0;//避免被內容撐開多余的高度 }
這個確實管用,但是沒有辦法在里面寫字了
3,padding-bottom+:after+absolute
<div class="square"> <div class="content"> Hello! </div> </div> .square { width: 50%; background: #ccc; } .square:after { content: ""; display: block; padding-bottom: 100%; } .content { position: absolute; width: 100%; height: 100%; }
這個方法不錯,而且還能在里面寫字
我還想到了其他幾種方法:
1.利用js計算出方塊(笨方法)
2.畫一個正方形表格