响应式设计的一些方法

1、表单输入框自适应

示例代码:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>响应式设计--表单自适应</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            
            form {
                /*关键设置 margin-right*/
                margin-right: 4em;
                /*关键设置 定位*/
                position: relative;
            }
            
            form input {
                /*关键设置 宽度*/
                width: 100%;
                line-height: 1.4;
                box-sizing: border-box;
            }
            
            form button {
                /*关键设置 定位*/
                position: absolute;
                left: 100%;
                top: 2px;
                width: 4em;
                box-sizing: border-box;
            }
        </style>
    </head>

    <body>
        <form action="" method="post">
            <input type="text" name="age" value="" />
            <button type="button">提交</button>
        </form>
    </body>

</html>

效果:

 

2、导航栏自适应布局

(1)、display:table 实现

示例代码:

 

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>响应式设计--导航栏自适应布局</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            
            nav {
                /*关键属性设置*/
                display: table;
                width: 100%;
                background: #00bcd4;
            }
            
            nav a {
                /*关键属性设置*/
                display: table-cell;
                text-decoration: none;
                color: #fff;
                padding: 0 1em;
            }
            /*关键属性设置*/
            
            nav a:not(:first-child) {
                border-left: 1px solid rgba(255, 255, 255, 0.7);
            }
        </style>
    </head>

    <body>
        <nav>
            <a href="#">Home</a>
            <a href="#">Javascript</a>
            <a href="#">HTML</a>
            <a href="#">CSS</a>
            <a href="#">HTTP</a>
        </nav>
    </body>

</html>

 

 效果:

 

 (2)flex布局

示例代码:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>响应式设计--导航栏自适应布局</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            
            nav {
                /*关键属性设置*/
                display: flex;
                width: 100%;
                background: #00bcd4;
            }
            
            nav a {
                /*关键属性设置*/
                flex:1;
                text-decoration: none;
                color: #fff;
                padding: 0 1em;
            }
            /*关键属性设置*/
            
            nav a:not(:first-child) {
                border-left: 1px solid rgba(255, 255, 255, 0.7);
            }
        </style>
    </head>

    <body>
        <nav>
            <a href="#">Home</a>
            <a href="#">Javascript</a>
            <a href="#">HTML</a>
            <a href="#">CSS</a>
            <a href="#">HTTP</a>
        </nav>
    </body>

</html>

效果同上。

3、网格布局(inline-block+justify)

示例代码:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>响应式设计--网格自适应布局</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            
            ul {
                /*以下三行关键属性设置*/
                margin: 0;
                padding: 0;
                text-align: justify;
            }
            
            li {
                /*关键属性设置*/
                display: inline-block;
                /*关键属性设置*/
                width: 30%;
                height: 100px;
                line-height: 100px;
                background: lightblue;
                text-align: center;
                margin-bottom: 1em;
            }
        </style>
    </head>

    <body>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
        </ul>
    </body>

</html>

效果:

4、使用媒体查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值