会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134267个问题
WEB前端全系列/第一阶段:HTML5+CSS3模块/浮动与定位 484楼

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>响应式布局实操</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

         body{
            background-color: #f1f1f1;
         }

        .nav {
            width: 100%;
            height: 50px;
            background-color: #555;
            overflow: hidden;
            clear: both;
            line-height: 50px;
        }
        .list{
            width: 80%;
            margin: 0 auto;
            /* list让内容往中间显示 */
        }
        .nav ul{
            overflow: hidden;
            clear: both;
            float: left;
        }

        .container .nav ul li {
            list-style: none;
            float: left;
            padding: 0 20px;
        }

        .container .nav ul li a {
            text-decoration: none;
            color: white;
            font-size: 16px;
        }
        .container .nav ul li.select {
            background-color: orange;
        }

         .nav .search {
            float: right;
            /* position: relative; */
        }

        .container .nav .search input {
            height: 30px;
            width: 200px;
            border: none;
            padding-left: 10px;
            border-radius: 50px;
            display: inline-block;
            /* position: absolute;
            top: 10px;
            right: 100px; */

        }

        .container .nav .search button {
            height: 30px;
            width: 60px;
            border: none;
            border-radius: 50px;
            margin-left: 20px;
            background-color: orange;
            color: white;
            font-size: 16px;
            display: inline-block;
            /* position: absolute;
            top: 10px;
            right: 20px; */
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="nav">
            <div class="list">
                <ul>
                    <li class="select"><a href="#">首页</a></li>
                    <li><a href="#">关于</a></li>
                    <li><a href="#">服务</a></li>
                    <li><a href="#">联系</a></li>
                </ul>
                <div class="search">
                    <input type="text" placeholder="搜索...">
                    <button>搜索</button>
                </div>
            </div>
        </div>
    </div>
    </div>
</body>

</html>

老师,我在搜索框后面加了button按钮,但是搜索框input和button不在一条水平线上,是因为浮动的影响吗?还是我别的地方写错了?我上述代码中注释掉的部分是给父元素search添加了position,然后分别给子元素input搜索框和button添加了绝对定位,解决了这个问题,请问这样做对吗?还有没有其他解决办法使他们的高度齐平image.png

WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS3新特性 485楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/商城官网项目 487楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS盒子模型 488楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS盒子模型 489楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS3新特性 492楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS盒子模型 493楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/HTML5基础元素 494楼

/* 导航样式开始 */
.nav {
    width: 100%;
    height: 66px;
    background: #88c5e1;
    border-bottom: 5px solid #54abd4;
}

.wrap {
    width: 87%;
    margin: 0 auto;
}

.logo,
.nav-bar,
.search {
    float: left;
    height: 66px;
}


.search .text-input {
    width: 100%;
    height: 40px;
    background-color: #f5f5f5;
    border: 1px solid #f5f5f5;
    box-sizing: border-box;
    border-radius: 2px;
    outline: none;
    margin-top: 13px;
    padding-left: 10px;
    transition: all 0.2s;
}

.search button {
    width: 40px;
    height: 34px;
    background-color: #f5f5f5;
    color: #54abd4;
    font-weight: 900;
    position: absolute;
    top: 16px;
    right: 2px;
}

.text-input:hover {
    background-color: #f5f5f5;
    border-color: #54abd4;
}


.nav-bar>ul {
    float: right;
}

.nav-bar ul li {
    float: left;
}

.nav-bar ul li a {
    color: #fff;
    padding: 10px;
    display: block;
    margin-left: 8px;
    margin-top: 12.5px;
}

.nav-bar-active {
    border-bottom: 2px solid #fff;
}

.nav-bar a:hover {
    border-bottom: 2px solid #fff;
}

/* 导航样式结束 */

/* **************  响应式设计  *********** */
/* pc端 */
@media screen and(min-width:992px) {
    .logo {
        width: 25%;
        background: url("../images/Brand.png") no-repeat left;
        background-size: 183px;
    }

    .search {
        width: 40%;
        position: relative;
    }

    .nav-bar {
        width: 35%;
    }

    .btn {
        display: none;
    }
}

/* pad端 */
@media screen and(min-width:768px) and (max-width:992px) {
    .logo {
        width: 18%;
        background: url("../images/Brand-M.png") no-repeat left;
        background-size: 86px;
    }

    .search {
        width: 42%;
        position: relative;
    }

    .nav-bar {
        width: 40%;
    }

    .btn {
        display: none;
    }
}

/* M端 */
@media screen and(max-width:768px) {
    .logo {
        width: 16%;
        background: url("../images/Brand-S.png") no-repeat left;
        background-size: 38px;
    }

    .search {
        width: 68%;
        position: relative;
    }

    .nav-bar {
        display: none;
    }

    .btn {
        width: 43px;
        height: 43px;
        border: 1px solid #fff;
        font-weight: 700;
        line-height: 43px;
        float: right;
        color: #fff;
        margin-top: 10.5px;
    }

    .btn i {
        font-size: 20px;
    }
}

也不知道哪里出问题了,对照着源码对了好几遍了,但是源码复制过来就没问题了

image.png

WEB前端全系列/第一阶段:HTML5+CSS3模块/响应式项目 495楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637