html、css做一个带搜索图标的搜索框(方法分享)

html、css做一个带搜索图标的搜索框

前言

给大家分享一下前端用处很多的带小图标的搜索框的制作方法。

效果展示

在这里插入图片描述
在这里插入图片描述

基本思路

1、搜索图像用绝对定位放到搜索框的上方
2、input框设置文本缩进,大小为搜索图像大小加上图像左右两边的外边距

准备

只需一个搜索图标图片,类似于下图
在这里插入图片描述

代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>搜索框demo</title>
	<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}

		body{
			width: 100vw;
			height: 100vh;
			background: radial-gradient(at center,
				#3498db,#2980b9);
			display: flex;
			justify-content: center;
			align-items: center;
		}

		div.search{
			height: 40px;
			width: 500px;		
		}

		div.search form{
			width: 100%;
			height: 100%;
		}

		div.search form input:nth-child(2){
			width: 400px;
			height: 100%;
			font-size: 16px;
			text-indent: 40px;
			border: none;
			float: left;
		}

		div.search form input:nth-child(3){
			width: 100px;
			height: 100%;
			font-size: 16px;
			letter-spacing: 5px;
			border: none;
		}

		div.search form img{
			position: absolute;
			left: 50vw;
			transform: translateX(-250px);
			margin-top: 10px;
			margin-left: 10px;
			height: 20px;
		}
	</style>
</head>
<body>
	<div class="search">
		<form action="#" method="post">
			<img src="./search_ico.png" alt="">
			<input type="text" name="condition" placeholder="请输入搜索条件">
			<input type="submit" value="搜索">
		</form>	
	</div>
</body>
</html>

本博客其他文章推荐

菜鸟后端程序员花了两天半模仿写出了赶集网主页,速来围观!

css实现下拉菜单

只用CSS的轮播图?快来看一看!

模仿一个球落地效果,最终停在地面上(仿真效果,CSS实现)

如果有一屏幕的爱心,你愿意送给谁?(简单实现原生js、css随机生成521个心)

您可以使用以下 HTMLCSS 代码来创建一个搜索图标搜索框HTML 代码: ```html <div class="search-box"> <form action="/search" method="get"> <input type="text" placeholder="Search..."> <button type="submit"><i class="fa fa-search"></i></button> </form> </div> ``` CSS 代码: ```css .search-box { display: flex; justify-content: center; align-items: center; margin: 20px; } input[type="text"] { padding: 10px; border: none; border-radius: 5px 0 0 5px; width: 200px; font-size: 16px; } button[type="submit"] { padding: 10px 15px; background-color: #4CAF50; border: none; border-radius: 0 5px 5px 0; color: white; font-size: 16px; cursor: pointer; } i.fa.fa-search { font-size: 16px; margin-right: 5px; } ``` 这里的 HTML 代码中包含一个名为 `search-box` 的 div 元素,用于设置搜索框的样式。在 div 元素内部,使用了 form 元素和 input 元素来创建搜索框,使用了 button 元素和 i 元素来创建搜索按钮和搜索图标。`placeholder` 属性提供了默认的提示文本,`name` 属性指定了提交的参数名称。 CSS 代码中使用了 `display: flex` 属性和 `justify-content: center` 和 `align-items: center` 属性来将搜索框水平居中。`border-radius` 属性用于设置输入框和按钮的圆角,`padding` 属性用于设置输入框和按钮的内边距,`width` 属性用于设置输入框的宽度。`background-color` 属性用于设置按钮的背景颜色,`color` 属性用于设置按钮的文字颜色,`cursor` 属性用于设置鼠标悬停时的光标类型。`fa` 和 `fa-search` 类名用于引用 Font Awesome 图标库中的搜索图标样式。 请注意,这里使用了 Font Awesome 图标库,您需要在页面中引入该库的 CSS 样式文件,例如: ```html <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值