Mastering responsive layout design in Nextjs using Tailwind CSS

I am attempting to design a card layout where the image is on the left and the content of the card is on the right using flex in NEXTJS and Tailwind CSS.

My goal is to ensure that the image remains responsive.

<div className="block">
  <Image
    src={item.cover}
    alt="Picture of something nice"
    width={200}
    height={200}
    layout="responsive"
    quality={65}
  />
</div>

This is how I have set up the image within the card:

<div className="container max-w-3xl text-white bg-card flex rounded space-x-5">
  <div className="block">
    <Image
      src={item.cover}
      alt="Picture of something nice"
      width={200}
      height={200}
      layout="responsive"
      quality={65}
    />
  </div>

  <section className="flex flex-col mt-2">
    <h1 className="capitalize text-lg font-semibold">{item.title}</h1>
    <h2>{item.alias}</h2>
    <h3>{item.artist}</h3>
    <h3>{item.author}</h3>
    <p>{item.description}</p>
  </section>
</div>

The issue arises when the image disappears.

I attempted using layout=fill. While the image appears, it does not achieve the desired cover image effect as seen in this example. Below is the <Image/> tag with layout=fill and objectFit=cover:

<div className="block w-36 relative">
  <Image
    src={
      'https://images.unsplash.com/photo-1651786291345-d6e61ac65358?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1288&q=80'
    }
    alt="Picture of something nice"
    layout="fill"
    objectFit="cover"
  />
</div>

However, the image is still non-responsive and fails to maintain the cover aspect.

Answer №1

Follow this code snippet to create your custom card design. For a detailed view, check the full page.

If you want to incorporate Next.js Image, simply wrap <Image> with <div> using similar properties as <img> in the provided code below. Make sure to exclude height and width from the <Image> tag while setting layout =responsive.

<script src="https://cdn.tailwindcss.com"></script>
<div class="container m-auto p-4">
  <div class="flex justify-center">
    <div class="flex flex-col md:flex-row md:max-w-xl rounded-lg bg-white shadow-lg">
      <img class=" w-full h-96 md:h-auto object-cover md:w-48 rounded-t-lg md:rounded-none md:rounded-l-lg" src="https://mdbootstrap.com/wp-content/uploads/2020/06/vertical.jpg" alt="" />
      <div class="px-6 py-10 flex flex-col justify-start">
        <h5 class="text-gray-900 text-xl font-medium mb-2">Card title</h5>
        <h5 class="text-gray-500 text-md font-extralight mb-2">Artist</h5> 
        <p class="text-gray-700 text-base mb-4">
          This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.
        </p>
        <p class="text-gray-600 text-xs">Author</p>
      </div>
    </div>
  </div>
</div>

Answer №2

When you set the `layout="fill"` attribute, the image will expand in both the X and Y axes to fill its container.

However, it's important to size your container appropriately for responsive design or to ensure the correct image size. The image will adjust based on the layout of the container.

If you want a fully responsive card, you can use the code snippet below:

<div class="max-w-sm w-full lg:max-w-full lg:flex">
  <div class="h-48 lg:h-auto lg:w-48 flex-none bg-cover rounded-t lg:rounded-t-none lg:rounded-l text-center overflow-hidden relative">
    <Image
      src="https://images.unsplash.com/photo-1651786291345-d6e61ac65358?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1288&q=80"
      alt="a car"
      layout="fill"
      objectFit="cover"
    />
  </div>
  <div class="border-r border-b border-l border-gray-400 lg:border-l-0 lg:border-t lg:border-gray-400 bg-white rounded-b lg:rounded-b-none lg:rounded-r p-4 flex flex-col justify-between leading-normal">
    <div class="mb-8">
      <h2 class="text-gray-900 font-bold text-xl mb-2">A card</h2>
      <p class="text-gray-700 text-base">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit.
        Voluptatibus quia, nulla! Maiores et perferendis eaque,
        exercitationem praesentium nihil.
      </p>
    </div>
  </div>
</div>

In this example, notice how breakpoints are utilized within the container for responsiveness. For more similar snippets like this, check out here. I hope this is helpful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide to seamlessly uploading files to s3 using nextjs, node, and typescript

I've been struggling to successfully upload a basic image to s3 using ts/nextjs/node. Despite having all the necessary credentials and code in place, I'm still unable to get it working. Can someone please provide clear instructions on how to achi ...

Is there a way to automatically shorten a text when it goes beyond the boundaries of a div container?

How can I prevent a paragraph in one of two adjacent div containers from having a line break without changing the container's size? The surrounding div uses display: flex to position its children side by side. Currently, I've applied the followin ...

What is the best way to transform the pages extracted through the Notion API into slugs?

I'm new to Next.js and Notion API, and I want to create a blog site on my personal website using the Notion API. While I can easily pull the posts, I am facing a challenge in slugifying the endpoint IDs with post titles. When attempting this based on ...

Submenu animation that "bursts onto the scene"

I'm facing an issue with my menu that has sub-items inside it. To achieve the animation effect I desire, I need to extract the width, height, and first-child's height of the sub-menu. While my animation is working most times, there are instances ...

Concealing divisions on a website with CSS styling

In my div, I have some text that I want to hide. <div style='visibility:hidden;' id='emailid'>$email</div> Although the visibility attribute successfully hides the text, it leaves behind empty space on the webpage where th ...

Exploring CSS3 animations: customizing animations for individual elements based on scroll movements

Can you help me with CSS3 animations triggered by scrolling? I came across a code that reveals a DIV element as the user scrolls down: <script type="text/javascript"> $(document).ready(function() { /* Every time the window ...

Combining inline input fields and select buttons with Bootstrap 3

I've been exploring different ways to align form elements inline, including dropdown buttons and select buttons, but have only had success with smaller size buttons around 40px wide. My main challenge now is trying to create a search bar with an input ...

How to perfectly position various height <a> elements in a WordPress mega menu

I'm currently working on a WordPress mega menu with four columns, each with a heading or top menu item. The headings should have a gradient border at the bottom and differ in length. However, I'm facing an issue where some headings span two lines ...

I am experiencing issues with arrow pagination not functioning properly in TypeScript

My current project involves building a customer table with 10 customers displayed on each page. Additionally, there are arrows below the table to help users navigate and view more customers. Unfortunately, there seems to be an issue with the functionality ...

The event listener function is not functioning properly on images generated by JavaScript

I'm currently working on placing multiple images on a grid in the center of the page and would like to include a function that triggers when each individual image is clicked. The images are dynamically created using JavaScript and inserted into the do ...

The Navigation Stops Working Following an Automated Redirect from a 404 Page to the Homepage with next/router and useEffect()

I encountered a problem with next/router. I created a '404 Page' and configured it to redirect clients back to the home page router.push('/') after 3 seconds. However, when clients return to the main page, they encounter a console error ...

Using jQuery to alter the background position of multiple backgrounds in CSS

I'm using jQuery to dynamically adjust the background image position of an element based on screen resolution. Here's the code: var pwwidth = $(window).width(); var pwheight = $(window).height(); var bg1posx = pwwidth - $('h1.portofolio&ap ...

Is there a way to choose columns 2 to 6 of the row that is currently selected

I am looking to create a hover effect for columns 1 through 7 of a row where, when the user hovers over any column within that range, certain styles are applied to all columns in the row except for columns 1 and 7. This means that the background color shou ...

MUI useStyles/createStyles hook dilemma: Inconsistent styling across components, with styles failing to apply to some elements

I have been trying to style my MUI5 react app using the makeStyles and createStyles hooks. The root className is being styled perfectly, but I am facing an issue with styling the logoIcon. Despite multiple attempts to debug the problem, I have not been suc ...

What is the best way to link to a CSS file located in a different directory while being in a subdirectory?

For a project I am working on, I am developing a simple streaming site and have organized my files into different folders. These folders include: HTML CSS Shows Within the "Shows" folder, there is a subfolder named "Testshow". I am facing an issue with ...

**Updated** Utilizing OutsideClickHandler in NextJS for a seamless user experience on a Burger/Dropdown menu, ensuring functionality for both the OutsideClickHandler and the Links

I am currently working on a project in NextJS, focusing on developing the NavBar component. The NavBar displays links along the top in wide screens and switches to a burger menu for mobile devices which opens a dropdown menu. Everything was functioning wel ...

Navigating with Breadcrumbs in NextJS

Could anyone provide guidance on creating a breadcrumb for my NextJS application? I am interested in using Ant Design, but the example of the component with React Router is not working for me. Any help would be greatly appreciated! ...

CSS: Adjusting the vertical position of text without affecting the background

I am currently working on some CSS buttons that expand in size when hovered over. I have successfully increased the text size as well, but now I am facing an issue with aligning the text a few pixels lower without affecting the background image layout. Ca ...

Is there a way to make all Bootstrap column heights equal using only jQuery?

I am currently working on matching the heights of two columns without using an existing library like match-height.js. In this specific case, I have a row with 2 columns where the first column contains a centered black square and the second column contains ...

What is the best way to make a gradient fill and rounded border with CSS?

I am interested in creating a gradient and rounded border similar to the top bar on Instagram. Although I know how to create a rounded and solid border, this one utilizes a gradient. It appears that Instagram uses a canvas, but I am wondering if it can b ...