Search code examples
htmlcssfrontendmaterialize

Display error using both search bar and autocomplete (materialize CSS)


I have some trouble setting up a materialize search bar (https://materializecss.com/navbar.html) that includes autocomplete (https://materializecss.com/autocomplete.html) for my website.

The autocomplete seems to "overwrite" the display of the search icon. If you do a "select all" you will see that the icon has turned white and is not aligned anymore.

Here is my index.html file so far:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!-- Compiled and minified JavaScript -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
  </head>

  <nav>
    <div class="nav-wrapper white">
      <form action="results" method="post">
        <div class="input-field">
          <input class="autocomplete" id="search" name="q" type="search"/>
          <label class="label-icon" for="search"><i class="material-icons">search</i></label>
          <i class="material-icons">close</i>
        </div>
      </form>
    </div>
  </nav>

  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script>
      document.addEventListener('DOMContentLoaded', function() {
        var elems = document.querySelectorAll('.autocomplete');
        var instances = M.Autocomplete.init(elems, {
                data: {
        "Apple": null,
        "Microsoft": null,
        "Google": 'https://placehold.it/250x250'
      },
          minLength: 1,
          limit:5
        });
      });
    </script>
</html>

I also figured out that with no data in the autocomplete js, the display of the search bar remain "normal".

Does anyone knows how or has solved this issue? Thanks!


Solution

  • Over-ride these two things from input-field label class.

    .input-field label {
        -webkit-transform: translateY(0px) !important;
        transform: translateY(0px) !important;
    }
    

    Use new versions of cdnjs.

     <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css">
    
    <!-- Compiled and minified JavaScript -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script>