Published on

A Complete Guide To Make A Tailwind CSS with Alpine JS Photo Gallery With Tailwind CSS

Tailwind CSS with Alpine JS Photo 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.

Very simple photo gallery that supports scrolling and selection by thumbnail images. adapted for this site from a laravel blade component.

  • It can make the building process of Tailwind CSS with Alpine JS Photo Gallery ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Tailwind CSS with Alpine JS Photo Gallery component file.

Free download of the Tailwind CSS with Alpine JS Photo Gallery's source code

<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="https://unpkg.com/[email protected]^2/dist/tailwind.min.css">
		<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
	</head>
	<body>
        <div class="flex-shrink-0">
            <div x-data="photoGalleryApp" class="max-w-xl flex flex-col">
                <div class="flex items-center sm:h-80">
                    <div :class="{'cursor-not-allowed opacity-50': ! hasPrevious()}"  class="hidden sm:block cursor-pointer">
                        <svg version="1.0" viewBox="0 0 60 60" xmlns="http://www.w3.org/2000/svg" fill="currentColor" stroke="currentColor" class="h-8" x-on:click="previousPhoto()">
                            <path d="m42.166 55.31-24.332-25.31 24.332-25.31v50.62z" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round" stroke-width="3.125"/>
                        </svg>
                    </div>
                    <div class="w-full sm:w-108 flex justify-center">
                        <img x-ref="mainImage" class="w-full sm:w-auto sm:h-80" src="" loading="lazy" />
                    </div>
                    <div :class="{'cursor-not-allowed opacity-50': ! hasNext()}"  class="hidden sm:block cursor-pointer">
                        <svg version="1.0" viewBox="0 0 60 60" xmlns="http://www.w3.org/2000/svg" fill="currentColor" stroke="currentColor" class="h-8" x-on:click="nextPhoto()">
                            <path d="m17.834 55.31 24.332-25.31-24.332-25.31v50.62z" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round" stroke-width="3.125"/>
                        </svg>
                    </div>
                </div>
                <div class="flex justify-center mt-1 space-x-1">
                    <img src="https://inaturalist-open-data.s3.amazonaws.com/photos/58049699/square.jpg" :class="{'ring-2 opacity-50': currentPhoto == 0}" class="h-16 w-16" x-on:click="pickPhoto(0)">
                    <img src="https://inaturalist-open-data.s3.amazonaws.com/photos/100821385/square.jpg" :class="{'ring-2 opacity-50': currentPhoto == 1}" class="h-16 w-16" x-on:click="pickPhoto(1)">
                    <img src="https://inaturalist-open-data.s3.amazonaws.com/photos/75873313/square.jpg" :class="{'ring-2 opacity-50': currentPhoto == 2}" class="h-16 w-16" x-on:click="pickPhoto(2)">
                    <img src="https://inaturalist-open-data.s3.amazonaws.com/photos/65267550/square.jpg" :class="{'ring-2 opacity-50': currentPhoto == 3}" class="h-16 w-16" x-on:click="pickPhoto(3)">
                    <img src="https://inaturalist-open-data.s3.amazonaws.com/photos/58914463/square.jpg" :class="{'ring-2 opacity-50': currentPhoto == 4}" class="h-16 w-16" x-on:click="pickPhoto(4)">
                </div>
            </div>
        </div>

        <script>
            document.addEventListener('alpine:init', () => {
                Alpine.data('photoGalleryApp', () => ({
                currentPhoto: 0,
                photos: [
                    "https://inaturalist-open-data.s3.amazonaws.com/photos/58049699/medium.jpg",
                    "https://inaturalist-open-data.s3.amazonaws.com/photos/100821385/medium.jpg",
                    "https://inaturalist-open-data.s3.amazonaws.com/photos/75873313/medium.jpg",
                    "https://inaturalist-open-data.s3.amazonaws.com/photos/65267550/medium.jpg",
                    "https://inaturalist-open-data.s3.amazonaws.com/photos/58914463/medium.jpg"
                ],
                init() { this.changePhoto(); },
                nextPhoto() {
                    if ( this.hasNext() ) {
                        this.currentPhoto++;
                        this.changePhoto();
                    }
                },
                previousPhoto() {
                    if ( this.hasPrevious() ) {
                        this.currentPhoto--;
                        this.changePhoto();
                    }
                },
                changePhoto() {
                    this.$refs.mainImage.src = this.photos[this.currentPhoto];
                },
                pickPhoto(index) {
                    this.currentPhoto = index;
                    this.changePhoto();
                },
                hasPrevious() {
                    return this.currentPhoto > 0;
                },
                hasNext() {
                    return this.photos.length > (this.currentPhoto + 1);
                }
                }))
            })
        </script>
    </body>
</html>

Install tailwind css of verion 3.0.18

Use the script html tag to import the script of Tailwind CSS of the version 3.0.18

<script src="https://cdn.tailwindcss.com"></script>
  • flex-shrink-0
  • max-w-xl
  • flex
  • flex-col
  • sm:h-80
  • hidden
  • sm:block
  • h-8
  • w-full
  • sm:w-108
  • sm:w-auto
  • mt-1
  • h-16
  • w-16
  1. Use flex to create a block-level flex container.

  2. Set the maximum width/height of an element using the max-w-xl utilities.

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

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

  5. Use sm:h-80 to set an element to a fixed height(20rem) at only small screen sizes.

  6. Use hidden to set an element to display: none and remove it from the page layout.

  7. Use inline utilities to put the element on its own line and fill its parent at only small screen sizes.

  8. Use h-8 to set an element to a fixed height(2rem).

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

  10. Use sm:w-108 to set an element to a fixed width(27rem) at only small screen sizes.

  11. The w-auto utility can be useful if you need to remove an element’s assigned width under a specific condition, like at a particular breakpoint.

  12. Control the margin on top side of an element to 0.25rem using the mt-1 utilities.

  13. Use h-16 to set an element to a fixed height(4rem).

  14. Use w-16 to set an element to a fixed width(4rem).

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Tailwind CSS with Alpine JS Photo Gallery components, learn and follow along to implement your own components.