Di bawah ini kita akan mempelajari cara membuat button search dengan CSS.
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style> body { font-family: Arial; } * { box-sizing: border-box; } form.example input[type=text] { padding: 10px; font-size: 17px; border: 1px solid grey; float: left; width: 80%; background: #f1f1f1; } form.example button { float: left; width: 20%; padding: 10px; background: #7FFF00; color: white; font-size: 17px; border: 1px solid grey; border-left: none; cursor: pointer; } form.example button:hover { background: #0b7dda; } form.example::after { content: ""; clear: both; display: table; } </style> </head> <body> <h2> Button Search </h2> <p>Full width:</p> <form class="example" action="/action_page.php"> <input type="text" placeholder="Search.." name="search"> <button type="submit"><i class="fa fa-search"></i></button> </form> <p>Pusatkan di dalam formulir dengan Full width:</p> <form class="example" action="/action_page.php" style="margin:auto;max-width:300px"> <input type="text" placeholder="Search.." name="search2"> <button type="submit"><i class="fa fa-search"></i></button> </form> </body> </html>
Cara Membuat Button Search
Langkah 1) Tambahkan HTML:
Contoh
<!-- Load icon library --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- The form --> <form class="example" action="action_page.php"> <input type="text" placeholder="Search.." name="search"> <button type="submit"><i class="fa fa-search"></i></button> </form>
Langkah 2) Tambahkan CSS:
Contoh
* {
box-sizing: border-box;
}
/* Style untuk field search */
form.example input[type=text] {
padding: 10px;
font-size: 17px;
border: 1px solid grey;
float: left;
width: 80%;
background: #f1f1f1;
}
/* Style untuk submit button */
form.example button {
float: left;
width: 20%;
padding: 10px;
background: #7FFF00;
color: white;
font-size: 17px;
border: 1px solid grey;
border-left: none; /* Mencegah border ganda */
cursor: pointer;
}
form.example button:hover {
background: #0b7dda;
}
/* Hapus floats */
form.example::after {
content: "";
clear: both;
display: table;
}