PROWARE technologies
PROWARE technologies

Expanding Search Bar/Box/Field Using Only HTML/CSS

This is a working search box that expands when in use. It requires only HTML and CSS — no JavaScript. The end-user can click the search button to expand the search box or simply hover over it.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
	<title>EXPANDING SEARCH BAR/BOX/FIELD</title>
	<style>
	body {
		padding: 0;
		margin: 0;
		font-family: sans-serif;
		background-color: white;
	}
	main {
		display: flex;
		height: 100vh;
		justify-content: center;
		align-items: center;
	}
	.search-box {
		display: inline-flex;
		padding: 2px;
		border: 2px solid purple;
		outline: none;
		border-radius: 30px;
	}
	.search-box:focus-within { /* IE11 WILL IGNORE BECAUSE IT IS NOT SUPPORTED */
		border-color: dodgerblue;
	}
	.search-box input {
		outline: none;
		border: none;
		width: 0;
		background-color: transparent;
		color: black;
		font-size: 16px;
		line-height: 30px;
		padding: 0;
		transition: width .4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
	}
	.search-box input::placeholder {
		color: lightgray;
	}
	.search-box:hover input,
	.search-box input:focus,
	.search-box input:valid {
		margin-left: 5px;
		width: 250px;
	}
	.search-box button {
		outline: none;
		border: none;
		cursor: pointer;
		background-color: purple;
		padding: 8px 8px 7px 7px;
		border-radius: 50%;
		color: white;
	}
	.search-box:focus-within button { /* can't combine selectors here because IE11 will whine */
		background-color: red;
	}
	.search-box input:focus + button {
		background-color: red;
	}
	.search-box input:valid + button {
		background-color: limegreen;
	}
	</style>
</head>
<body>
	<main>
		<div>
			<form action="https://www.bing.com/search" class="search-box">
				<input type="text" name="q" autocomplete="off" autocapitalize="off" placeholder="enter search" required />
				<button type="submit"><i class="fas fa-search"></i></button>
			</form>
		</div>
	</main>
</body>
</html>

Coding Video

https://youtu.be/jDoe3lGNVNU