用HTML和CSS实现搜索框

文章目录

  • 前言
  • 1.style样式
  • 2.body部分
  • 3.效果图


前言

用html和css实现简单的搜索框

 

1.style样式

代码如下(示例):

<style>
        * {
            box-sizing: border-box;  /*宽度和高度会包括内边距和边框*/
        }

        div.search {
            padding: 10px 0;
        }

        form {
            position: relative;
            width: 300px;
            margin: 0 auto;  /*设置宽度后,居中*/
        }

        input,button {
            border: none;   /*无边框样式;元素的总宽度和高度包括边框的宽度 */
            outline: none;  /*无轮廓样式;轮廓是在元素周围绘制的一条线,在边框之外并且可能与其他内容重叠,以凸显元素,元素的总宽度和高度不受轮廓线宽度的影响。*/
        }

        .search input {
            width: 100%;
            height: 42px;
            padding-left: 13px;
            border: 2px solid #c5464a;
            border-radius: 19px;   /*border-radius 属性用于向元素添加圆角边框:*/
            background: transparent;  /*背景透明*/
        }

        .search button {
            height: 42px;
            width: 42px;
            cursor: pointer;  /*cursor属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状*/
            position: absolute;
            background: #c5464a;
            border-radius: 0 19px 19px 0;
            width: 60px;
            right: 0;
        }
        .search button:hover {
            background-color: #c7585c;  /*点击搜索按钮时的背景颜色*/
        }

        /* 在<button>元素后面插入内容,并设置所插入内容的样式: */
        .search button:before {
            content: "搜索";  /*插入的内容*/
            font-size: 13px;  /*插入内容的字体大小*/
            color: #F9F0DA;  /*插入内容的字体颜色*/
        }
    </style>
 

2.body部分

代码如下(示例):

<body>
    <div class="search">
        <form action="" method="post">
            <input placeholder="请输入..." name="keyword" type="text">
            <button type="submit"></button>
        </form>
    </div>

</body>

3.效果图

                       

  • 40
    点赞
  • 322
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于 HTMLCSS 实现带搜索图标的搜索框功能的代码示例: ```html <div class="search-box"> <input type="text" placeholder="Search..."> <button type="submit"><i class="fa fa-search"></i></button> </div> ``` ```css .search-box { position: relative; display: inline-block; } .search-box input[type="text"] { width: 200px; padding: 8px 16px; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; } .search-box button[type="submit"] { position: absolute; top: 50%; right: 8px; transform: translateY(-50%); background-color: transparent; border: none; outline: none; cursor: pointer; } .search-box button[type="submit"] i { color: #ccc; font-size: 20px; } .search-box button[type="submit"]:hover i { color: #333; } ``` 解释一下代码: - `<div class="search-box">` 用来包裹搜索框和搜索按钮。 - `<input type="text" placeholder="Search...">` 是搜索框,`placeholder` 属性用来设置默认提示文字。 - `<button type="submit"><i class="fa fa-search"></i></button>` 是搜索按钮,使用了 Font Awesome 图标库的搜索图标。 - `.search-box` 的 `position: relative;` 用来设置相对定位,使得搜索按钮可以使用绝对定位。 - `.search-box input[type="text"]` 和 `.search-box button[type="submit"]` 分别用来设置搜索框和搜索按钮的样式。 - `.search-box button[type="submit"] i` 用来设置搜索图标的样式,`.fa-search` 类是 Font Awesome 图标库中的搜索图标类。 希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值