Published on

The 5 Really Obvious Ways To Make A Search Image gallery With Tailwind CSS Better That You Ever Did

Search Image gallery

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

Use tailwindcss & alpinejs to create a search engine for high-quality images.

i use pixaby api

  • It can make the building process of Search Image gallery ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Search Image gallery component file.

Free download of the Search Image gallery's source code

<!-- This is an example component -->
<body x-data="imageGallery()" 
      x-init="fetch('https://pixabay.com/api/?key=15819227-ef2d84d1681b9442aaa9755b8&q=woman+girl+food&image_type=all&per_page=52&')
      .then(response => response.json())
      .then(response => { images = response.hits })"
      class="bg-white">

<div class="main">
    <div class="px-4 sm:px-8 lg:px-16 xl:px-20 mx-auto">

        <!-- hero -->
        <div class="hero">
            <!-- hero headline -->
            <div class="hero-headline flex flex-col items-center justify-center pt-24 text-center">
                <h1 class=" font-bold text-3xl text-gray-900">Stunning free images & royalty free stock</h1>
                <p class=" font-base text-base text-gray-600">high quality stock images and videos shared by our talented community.</p>
            </div>

            <!-- image search box -->
            <div class="box pt-6">
                <div class="box-wrapper">

                    <div class=" bg-white rounded flex items-center w-full p-3 shadow-sm border border-gray-200">
                      <button @click="getImages()" class="outline-none focus:outline-none"><svg class=" w-5 text-gray-600 h-5 cursor-pointer" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" stroke="currentColor" viewBox="0 0 24 24"><path d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg></button>
                      <input type="search" name="" id="" @keydown.enter="getImages()" placeholder="search for images" x-model="q" class="w-full pl-4 text-sm outline-none focus:outline-none bg-transparent">
                      <div class="select">
                        <select name="" id="" x-model="image_type" class="text-sm outline-none focus:outline-none bg-transparent">
                          <option value="all" selected>All</option>
                          <option value="photo">Photo</option>
                          <option value="illustration">Illustration</option>
                          <option value="vector">Vector</option>
                         </select>
                      </div>
                    </div>
                  
                </div>
            </div>

            <section id="photos" class="my-5 grid grid-cols-1 md:grid-cols-4 gap-4">
                <template x-for="image in images" :key="image.id">
                  <a :href="image.largeImageURL" class="hover:opacity-75 " target="_new"><img class="w-full h-64 object-cover" :src="image.largeImageURL" :alt="image.tags" /></a>
                </template>
              </section>

        </div>

        <footer class="p-5 text-sm text-gray-600 text-center inline-flex items-center">
          <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="heart" class="svg-inline--fa fa-heart fa-w-16 text-red-600 w-4 h-4 mr-4 align-middle" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M462.3 62.6C407.5 15.9 326 24.3 275.7 76.2L256 96.5l-19.7-20.3C186.1 24.3 104.5 15.9 49.7 62.6c-62.8 53.6-66.1 149.8-9.9 207.9l193.5 199.8c12.5 12.9 32.8 12.9 45.3 0l193.5-199.8c56.3-58.1 53-154.3-9.8-207.9z"></path></svg>
          <span>Made by <a href="https://tailwindcss.com/" target="_new" class="text-teal-600 font-semibold">tailwindcss</a> & <a href="https://github.com/alpinejs/alpine" target="_new" class="text-teal-600 font-semibold">alpinejs</a></span>
        </footer>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>
<script>
    function imageGallery() {
        return {
          images: [],
          api_key : "15819227-ef2d84d1681b9442aaa9755b8",
          q: "",
          image_type: "",
          page: "",
          per_page: 52,
          getImages: async function() {
            console.log("params", this.q, this.image_type);  
            const response = await fetch(
              `https://pixabay.com/api/?key=${this.api_key}&q=${
                this.q
              }&image_type=${this.image_type}&per_page=${this.per_page}&page=${this.page}`
            );
            const data = await response.json();
            this.images = data.hits;
          }
        };
      }
</script>

Install tailwind css of verion 1.4.6

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.4.6

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">
  • bg-white
  • px-4
  • sm:px-8
  • lg:px-16
  • xl:px-20
  • mx-auto
  • flex
  • flex-col
  • pt-24
  • text-center
  • text-3xl
  • text-gray-900
  • text-base
  • text-gray-600
  • pt-6
  • w-full
  • p-3
  • border-gray-200
  • w-5
  • h-5
  • pl-4
  • text-sm
  • bg-transparent
  • my-5
  • grid
  • grid-cols-1
  • md:grid-cols-4
  • gap-4
  • h-64
  • p-5
  • inline-flex
  • text-red-600
  • w-4
  • h-4
  • mr-4
  • text-teal-600
  1. Control the background color of an element to white using the bg-white utilities.

  2. Control the horizontal padding of an element to 1rem using the px-4 utilities.

  3. Control the horizontal padding of an element to 2rem at only small screen sizes using the sm:px-8 utilities.

  4. Control the horizontal padding of an element to 4rem at only large screen sizes using the lg:px-16 utilities.

  5. Control the horizontal padding of an element to 5rem at only extremely large screen sizes using the xl:px-20 utilities.

  6. Control the horizontal margin of an element to auto using the mx-auto utilities.

  7. Use flex to create a block-level flex container.

  8. Use flex to create a block-level flex container.

  9. Control the padding on top side of an element to 6rem using the pt-24 utilities.

  10. Control the text color of an element to center using the text-center utilities.

  11. Control the text color of an element to 3xl using the text-3xl utilities.

  12. Control the text color of an element to gray-900 using the text-gray-900 utilities.

  13. Control the text color of an element to base using the text-base utilities.

  14. Control the text color of an element to gray-600 using the text-gray-600 utilities.

  15. Control the padding on top side of an element to 1.5rem using the pt-6 utilities.

  16. Use w-full to set an element to a 100% based width.

  17. Control the padding on all sides of an element to 0.75rem using the p-3 utilities.

  18. Control the border color of an element to gray-200 using the border-gray-200 utilities.

  19. Use w-5 to set an element to a fixed width(1.25rem).

  20. Use h-5 to set an element to a fixed height(1.25rem).

  21. Set the left padding of the element to 1rem using the pl-4 utilities class

  22. Control the text color of an element to sm using the text-sm utilities.

  23. Control the background color of an element to transparent using the bg-transparent utilities.

  24. Control the vertical margin of an element to 1.25rem using the my-5 utilities.

  25. Use grid to create a grid container.

  26. Use grid to create a grid container.

  27. Use grid to create a grid container at only medium screen sizes.

  28. To specify the width between columns, you can use the gap-4 utilities.

  29. Use h-64 to set an element to a fixed height(16rem).

  30. Control the padding on all sides of an element to 1.25rem using the p-5 utilities.

  31. Use inline-flex to create an inline flex container that flows with text.

  32. Control the text color of an element to red-600 using the text-red-600 utilities.

  33. Use w-4 to set an element to a fixed width(1rem).

  34. Use h-4 to set an element to a fixed height(1rem).

  35. Control the margin on right side of an element to 1rem using the mr-4 utilities.

  36. Control the text color of an element to teal-600 using the text-teal-600 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Search Image gallery components, learn and follow along to implement your own components.