Skip to content
TheChatArchiveThe Chat Archive
grok-3-mini-beta· xAIHugging Face LMArena Human Preference 140K

Create a gallery component for a cake bakery webpage in astro framework using tailwind…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-r1-0528’s answer.
  1. User

    Create a gallery component for a cake bakery webpage in astro framework using tailwind to style it

  2. Assistant

    Below is a complete example of a gallery component for a cake bakery webpage, built using the Astro framework and styled with Tailwind CSS. This component is designed to be reusable, responsive, and visually appealing. It displays a grid of cake images, with a simple layout that adapts to different screen sizes.

    Prerequisites

    • Ensure you have an Astro project set up. If not, you can create one using npm create astro@latest.
    • Make sure Tailwind CSS is installed in your Astro project. You can follow the official Astro guide for integrating Tailwind: Astro + Tailwind Guide.

    Step 1: Create the Component

    Create a new file in your Astro project's src/components directory, e.g., Gallery.astro.

    Gallery.astro Code

    ---
    import type { Props } from './types'; // Optional: If you want to define types for props
    
    interface Props {
      images: Array<{ src: string; alt: string }>; // Array of objects with image source and alt text
    }
    
    const { images } = Astro.props;
    ---
    
    <section class="py-8 bg-gray-100">
      <div class="container mx-auto px-4">
        <h2 class="text-3xl font-bold text-center text-pink-600 mb-6">Our Delicious Cake Gallery</h2>
        <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
          {images.map((image, index) => (
            <div key={index} class="relative overflow-hidden rounded-lg shadow-lg transition-transform transform hover:scale-105 hover:shadow-xl">
              <img 
                src={image.src} 
                alt={image.alt} 
                class="w-full h-64 object-cover" 
                loading="lazy" 
              />
              <div class="absolute inset-0 bg-black bg-opacity-30 flex items-center justify-center opacity-0 hover:opacity-100 transition-opacity">
                <p class="text-white text-lg font-semibold">{image.alt}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
    

    Explanation of the Code

    1. Frontmatter (Astro-specific):

      • We define the component's props using an interface. The images prop is an array of objects, where each object has src (the image URL) and alt (the alt text for accessibility).
      • Astro automatically passes props to the component, so you can use Astro.props to access them.
    2. HTML Template:

      • Section and Container: The outer <section> uses Tailwind classes for padding (py-8) and a light background (bg-gray-100) to make it stand out. The inner <div> with container mx-auto px-4 centers the content and adds horizontal padding for responsiveness.
      • Heading: A simple <h2> element with Tailwind classes for styling (e.g., text-3xl for font size, text-pink-600 for a bakery-themed color). You can adjust the color to match your brand.
      • Grid Layout: The <div> with grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6 creates a responsive grid. It starts as a single column on mobile and expands to 4 columns on larger screens. The gap-6 adds spacing between grid items.
      • Image Cards: Each image is wrapped in a <div> for styling. Tailwind classes like rounded-lg shadow-lg add rounded corners and a shadow for a modern look. The hover:scale-105 and hover:shadow-xl add subtle hover effects for interactivity.
      • Image Element: The <img> tag uses w-full h-64 object-cover to ensure images fill their containers while maintaining aspect ratio. loading="lazy" improves performance by deferring image loading.
      • Overlay: An absolute-positioned div overlays the image on hover, displaying the alt text with a semi-transparent background (bg-black bg-opacity-30). This adds a nice touch for a bakery site, making it feel more engaging.
    3. Tailwind Styling:

      • Classes are used throughout for utility-based styling. For example:
        • Responsive grid: grid-cols-1 sm:grid-cols-2 ... handles breakpoints.
        • Transitions: transition-transform and hover:scale-105 for smooth animations.
        • Colors: I used text-pink-600 for a warm, bakery-like feel, but you can customize this in your Tailwind config.
      • Ensure your Tailwind config includes the necessary plugins (e.g., for forms or typography if needed).

    How to Use the Component

    In any Astro page (e.g., src/pages/index.astro), import and use the component like this:

    ---
    import Gallery from '../components/Gallery.astro';
    
    const cakeImages = [
      { src: '/images/cake1.jpg', alt: 'Chocolate Cake' },
      { src: '/images/cake2.jpg', alt: 'Vanilla Cake' },
      { src: '/images/cake3.jpg', alt: 'Red Velvet Cake' },
      { src: '/images/cake4.jpg', alt: 'Fruit Topped Cake' },
      // Add more images as needed
    ];
    ---
    
    <html lang="en">
      <head>
        <title>Cake Bakery</title>
      </head>
      <body>
        <h1>Welcome to Our Bakery</h1>
        <Gallery images={cakeImages} />
      </body>
    </html>
    

    Additional Tips

    • Images: Place your images in the public/images directory of your Astro project. Reference them with paths like /images/cake1.jpg.
    • Responsiveness: The grid is already responsive, but test it on different devices. Adjust breakpoints if needed (e.g., via Tailwind's config).
    • Accessibility: Always include meaningful alt texts. The overlay on hover helps with user experience.
    • Performance: Astro's build process optimizes static assets. If you have many images, consider lazy-loading or using a CDN.
    • Customization: You can extend this by adding filters, modals for larger images, or even integrating with a CMS for dynamic image data.
    • Styling Enhancements: If you want more bakery-specific styles, add custom Tailwind classes in your tailwind.config.cjs file, e.g., for a custom color palette.

    This component should give you a polished, professional gallery for your cake bakery webpage. If you need further modifications or integration help, let me know!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.