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個心)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章