如何实现网页的布局自适应

一、实现适配(@media + rem)

效果图:适用于各种屏幕尺寸大小的显示图

二、使用步骤

1.body中代码:

代码如下(示例):

<ul>
    <li>
        <div></div>
        <span>
            我是个头像
        </span>
    </li>
    <li>
        <div></div>
        <span>
            我是个头像
        </span>
    </li>
    <li>
        <div></div>
        <span>
            我是个头像
        </span>
    </li>
    <li>
        <div></div>
        <span>
            我是个头像
        </span>
    </li>
    <li>
        <div></div>
        <span>
            我是个头像
        </span>
    </li>
    <li>
        <div></div>
        <span>
            我是个头像
        </span>
    </li> 
</ul>

2.style中代码:

代码如下(示例):




@media screen and (min-width: 768px){
            ul{
                background-color: blue !important;
            }
            li{
                width: 50% !important;
            }
            div{
                width: 5rem !important;
                height: 5rem !important;
            }
            span{
                font-size: 2.4rem !important;
            }
        }
        @media screen and (min-width: 992px){
            ul{
                background-color: orange !important;
            }
            li{
                width: 33.3333% !important;
            }
            div{
                width: 5rem !important;
                height: 5rem !important;
            }
            span{
                font-size: 2.4rem !important;
            }
        }
        @media screen and (min-width: 1200px){
            ul{
                background-color: orange !important;
            }
            li{
                width: 16% !important;
            }
            div{
                width: 3rem !important;
                height: 3rem !important;
            }
            span{
                font-size: 1.6rem !important;
            }
        }
        html{
            font-size: 10px;
        }
        ul{
            margin: 0;
            padding: 0;
            list-style: none;
            background-color: orangered;
            display: flow-root;
        }
        li{
            float: left;
            width: 100%;
            padding: 2rem 0;
        }
        div{
            border-radius: 50%;
            background-color: white;
            width: 8rem;
            height: 8rem;margin: auto;
        }
        span{
            display: block;
            text-align: center;
            line-height: 1.4em;
            font-size: 3rem;
        }




# 总结:利用@media screen实现网页布局的自适应,定义四种不同大小页面的尺寸,最小尺寸直接用无序列表,中间两种宽度利用在盒子内的宽度百分比自适应换行,最大尺寸用左浮动,使其能够排列于一行。



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值