css3响应式布局

版权声明:内容多为自言自语,请自行判断有无价值。 https://blog.csdn.net/weixin_41702247/article/details/81285834

利用css3中的新特性对不同分辨率的终端进行页面适配:

1.<meta name="viewport" content="width=device-width, initial-scale=1.0">

2.网格布局:整体视图区域被分为12列,每列占约8.33%。一行内各个小div的col之和为12

3.媒介查询:@media 媒介查询

整体实现方式:以一行分为三部分为例。

html:

<div class="row">
    <div class="left col-xs-6 col-sm-3 col-md-2 col-lg-1">左边的div</div>
    <div class="middle col-xs-12 col-sm-6 col-md-8 col-lg-10">中间的div</div>
    <div class="right col-xs-6 col-sm-3 col-md-2 col-lg-1">右边的div</div> 
</div>

css:通用部分

*{box-sizing: border-box;}
.row:after{content:"";clear:both;display:block;}
[class*="col-"]{float:left;padding:15px;}

.left{background-color:red;}
.middle{background-color:green;}
.right{background-color:yellow;}

移动端优先(续上部分)

/*默认移动端适配*/
[class*="col-"]{width:100;}

@media only screen and (min-width:480){
    html{background-color:lightgreen;}
    .col-xs-1 {width: 8.33%;}
    .col-xs-2 {width: 16.66%;}
    .col-xs-3 {width: 25%;}
    .col-xs-4 {width: 33.33%;}
    .col-xs-5 {width: 41.66%;}
    .col-xs-6 {width: 50%;}
    .col-xs-7 {width: 58.33%;}
    .col-xs-8 {width: 66.66%;}
    .col-xs-9 {width: 75%;}
    .col-xs-10 {width: 83.33%;}
    .col-xs-11 {width: 91.66%;}
    .col-xs-12 {width: 100%;}
}
@media only screen and (min-width:768){
    html{background-color:lightblue;}
    .col-sm-1 {width: 8.33%;}
    .col-sm-2 {width: 16.66%;}
    .col-sm-3 {width: 25%;}
    .col-sm-4 {width: 33.33%;}
    .col-sm-5 {width: 41.66%;}
    .col-sm-6 {width: 50%;}
    .col-sm-7 {width: 58.33%;}
    .col-sm-8 {width: 66.66%;}
    .col-sm-9 {width: 75%;}
    .col-sm-10 {width: 83.33%;}
    .col-sm-11 {width: 91.66%;}
    .col-sm-12 {width: 100%;}
}
@media only screen and (min-width:992){
    html{background-color:lightcoral;}
    .col-md-1 {width: 8.33%;}
    .col-md-2 {width: 16.66%;}
    .col-md-3 {width: 25%;}
    .col-md-4 {width: 33.33%;}
    .col-md-5 {width: 41.66%;}
    .col-md-6 {width: 50%;}
    .col-md-7 {width: 58.33%;}
    .col-md-8 {width: 66.66%;}
    .col-md-9 {width: 75%;}
    .col-md-10 {width: 83.33%;}
    .col-md-11 {width: 91.66%;}
    .col-md-12 {width: 100%;}
}
@media only screen and (min-width:1200){
    html{background-color:lightseagreen;}
    .col-lg-1 {width: 8.33%;}
    .col-lg-2 {width: 16.66%;}
    .col-lg-3 {width: 25%;}
    .col-lg-4 {width: 33.33%;}
    .col-lg-5 {width: 41.66%;}
    .col-lg-6 {width: 50%;}
    .col-lg-7 {width: 58.33%;}
    .col-lg-8 {width: 66.66%;}
    .col-lg-9 {width: 75%;}
    .col-lg-10 {width: 83.33%;}
    .col-lg-11 {width: 91.66%;}
    .col-lg-12 {width: 100%;}
}

若是pc端优先,则将大于1200px时的样式设为默认,再其后由大到小排列媒介查询。

顺序很重要,否则会导致样式覆盖。

核心知识点:

1. 网格系统、媒介查询

2.分移动端优先和pc端优先,二者区别在于默认样式的排序问题

3.[class*="col-"]{...;}意思是class以col开头的所有标签

猜你喜欢

转载自blog.csdn.net/weixin_41702247/article/details/81285834