{"id":225,"date":"2018-06-16T14:43:53","date_gmt":"2018-06-16T06:43:53","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=225"},"modified":"2018-09-09T12:56:16","modified_gmt":"2018-09-09T04:56:16","slug":"how-to-make-a-html-search-box-with-the-input-text-field-and-search-button-within-the-same-enclosing-box","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-make-a-html-search-box-with-the-input-text-field-and-search-button-within-the-same-enclosing-box\/","title":{"rendered":"How to make a HTML search box with the input text field and search button within the same enclosing box"},"content":{"rendered":"<p>Since Google had indexed most of the content from my website, <a href=\"https:\/\/cse.google.com\/cse\/\" rel=\"noopener\" target=\"_blank\">Google Custom Search<\/a> is the easiest way for me to implement site-wide search. In addition to fast searching, Google Custom Search also comes with <a href=\"https:\/\/developers.google.com\/custom-search-ads\/\" rel=\"noopener\" target=\"_blank\">AdSense monetization<\/a>. <\/p>\n<p>Given these points and some time on hand, I had recently implemented site-wide search with Google Custom Search.<\/p>\n<p>In addition to building the search page to display the search result, there is a need for a HTML search box beside the top navigation links: <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/techcoil-search-box-as-at-20180616.gif\" alt=\"Techcoil search box as at 20180616\"\/><\/p>\n<p>Since I had spent quite a bit of time on building the search box, I decided to document the steps in this post for future references. <\/p>\n<p>With this intention, this is how to make a HTML search box with the input text field and search button within the same enclosing box.<\/p>\n<h2>The HTML codes for rendering the HTML search box with the input text field and search button within the same enclosing box<\/h2>\n<p>Firstly, let us craft the HTML codes for rendering the HTML search box with the input text field and search button within the same enclosing box:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;div class=&quot;site-search&quot;&gt;\r\n    &lt;form role=&quot;search&quot; method=&quot;get&quot; class=&quot;search-form&quot; action=&quot;https:\/\/www.techcoil.com\/search&quot;&gt;\r\n        &lt;span class=&quot;screen-reader-text&quot;&gt;Search Techcoil for:&lt;\/span&gt;\r\n        &lt;input type=&quot;search&quot; class=&quot;search-field&quot; placeholder=&quot;Search Techcoil&quot; name=&quot;q&quot; title=&quot;Search Techcoil for&quot;&gt;\r\n        &lt;button type=&quot;submit&quot; class=&quot;button&quot;&gt;&lt;span class=&quot;screen-reader-text&quot;&gt;Submit&lt;\/span&gt;\r\n            &lt;img src=&quot;\/ph\/img\/search-icon.png&quot;\/&gt;\r\n        &lt;\/button&gt;\r\n    &lt;\/form&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>As shown above, the outermost div element (<code>div.site-search<\/code>) is for serving as the enclosing box. <\/p>\n<p>Within the enclosing box, there is a HTML form that is marked with the role of being a search form. <\/p>\n<p>In addition, the form tells the browser to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-send-http-get-request-with-java-without-using-any-external-libraries\/\" rel=\"noopener\" target=\"_blank\">send a HTTP GET to the server endpoint<\/a> at <a href=\"https:\/\/www.techcoil.com\/search\" rel=\"noopener\" target=\"_blank\">https:\/\/www.techcoil.com\/search<\/a> when the user submits this form. <\/p>\n<p>Within the <code>form<\/code> element, there are three other elements:<\/p>\n<ul>\n<li>A span element that is meant for screen readers.<\/li>\n<li>The input text field element for rendering the text field for the search query.<\/li>\n<li>The button that submits the form when it is clicked. Within the <code>button<\/code> HTML element there is another span element for screen readers and an HTML image that renders the magnifying glass.<\/li>\n<\/ul>\n<h2>The CSS codes for styling the HTML search box with the input text field and search button within the same enclosing box<\/h2>\n<p>Undeniably, the CSS codes is the gist of making the input text and button appear inside an enclosing box. As a matter of fact, without CSS, the search box will look like the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/techcoil-search-box-as-at-20180616-without-css.gif\" alt=\"Techcoil search box as at 20180616 without css\"\/> <\/p>\n<p>As can be seen, that is definitely not how we want the search box to look like. Therefore, we need to include the following CSS codes to the web page:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\nspan.screen-reader-text {\r\n    display: none;\r\n}\r\n\r\n.site-search {\r\n    border: 1px solid #CCC;\r\n    display: inline-block;\r\n    margin: 10px 0;\r\n}\r\n\r\n.site-search input:focus,\r\n.site-search button:focus {\r\n    outline: none;\r\n}\r\n\r\n.site-search .search-field {\r\n    background: none;\r\n    border: none;\r\n    border-color: transparent;\r\n    float:left;\r\n    font-size: 0.8rem;\r\n    height: 36px;\r\n    padding: 0 6px 0 6px;\r\n    -webkit-appearance: none;\r\n}\r\n\r\n.site-search button {\r\n    background:none;\r\n    border: none;\r\n    border-color: transparent;\r\n    float:left;\r\n    padding: 7px 7px 7px 0;\r\n}\r\n\r\n.site-search button img {\r\n    cursor: pointer;\r\n}\r\n<\/pre>\n<h3>Hiding the text that are meant for screen readers<\/h3>\n<p>Firstly, we include the first CSS rule-set ensures that the text that are meant for screen readers does not appear on the browser.<\/p>\n<h3>Creating the illusion that the search button is contained within the text input field<\/h3>\n<p>Following that, the second CSS rule-set applies a Gray 80 1 pixel border around the outermost div to create the enclosing box. <\/p>\n<h3>Removing the border highlight on the input text field and button<\/h3>\n<p>In order to ensure that the browser does not highlight the input text field and button when they are being focused, we include the third CSS rule-set.<\/p>\n<h3>Changing the default appearance of the input text field<\/h3>\n<p>Since different browsers render the input text field differently, we need to include the forth CSS rule-set. In short, the forth CSS rule-set:<\/p>\n<ul>\n<li>removes the background, border, border colour and webkit appearance of the text input field,<\/li>\n<li>floats the text input field to the left,<\/li>\n<li>sets the font size to 0.8 rem, <\/li>\n<li>gives the text input field a height of 36px, <\/li>\n<li>and gives a bit of padding to the top and bottom of the text within the text field.<\/li>\n<\/ul>\n<h3>Changing the default appearance of the button<\/h3>\n<p>After that, we include the fifth CSS rule-set to change the default appearance of the button. As a result of applying this CSS rule-set, we will be able to:<\/p>\n<ul>\n<li>remove the background, border, border colour of the button,<\/li>\n<li>float the button to the left,<\/li>\n<li>give a bit of padding to the top, left and bottom of the button.<\/li>\n<\/ul>\n<h3>Making the button looks clickable when the mouse hovers over it<\/h3>\n<p>Lastly, we include the last CSS rule-set to give some indication that the button can be clicked. Since the image is rendered on top of the button, we apply <code>pointer<\/code> as the <code>cursor<\/code> property value to indicate that the image is supposed to be clicked. <\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-3D\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3D&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3D&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F225&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Since Google had indexed most of the content from my website, <a href=\"https:\/\/cse.google.com\/cse\/\" rel=\"noopener\" target=\"_blank\">Google Custom Search<\/a> is the easiest way for me to implement site-wide search. In addition to fast searching, Google Custom Search also comes with <a href=\"https:\/\/developers.google.com\/custom-search-ads\/\" rel=\"noopener\" target=\"_blank\">AdSense monetization<\/a>. <\/p>\n<p>Given these points and some time on hand, I had recently implemented site-wide search with Google Custom Search.<\/p>\n<p>In addition to building the search page to display the search result, there is a need for a HTML search box beside the top navigation links: <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/techcoil-search-box-as-at-20180616.gif\" alt=\"Techcoil search box as at 20180616\"\/><\/p>\n<p>Since I had spent quite a bit of time on building the search box, I decided to document the steps in this post for future references. <\/p>\n<p>With this intention, this is how to make a HTML search box with the input text field and search button within the same enclosing box.<\/p>\n","protected":false},"author":1,"featured_media":1283,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[535,17,16,175,199,538,534,537,536,207],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Techcoil-search-box-as-at-20180616-with-320x200-dimensions.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-3D","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/225"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1283"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}