原生css初級網頁響應式佈局
阿新 • • 發佈:2019-01-10
1.確保邊框邊距包含在元素的寬度和高度間
*{
box-sizing:border-box;
}
2.基本模板
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33 %;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
<div class="row">
<div class="col-3"></div>
<div class="col-6"></div>
<div class="col-3"></div>
</div>
3.對擁有寬度設定的div,配置屬性
[class*="col-"] {
float:left;
}
.row:after {
content: "";
clear : both;
display: block;
}
4.但上述給col設定百分比,在移動裝置上無效,可做如下設定
@media only screen and (max-width: 768px) {
/* For mobile phones: */
[class*="col-"] {
width: 100%;
}
}
/* 為移動端設計: */
[class*="col-"] {
width: 100%;
}
@media only screen and (min-width: 768px) {
/* For desktop: */
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
}
5.橫屏豎屏設定
@media only screen and (orientation: landscape){}
@media only screen and (orientation: portrait){}