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.

https://i.sstatic.net/MEJn1.png 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. https://i.sstatic.net/1gfsU.png

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

Using inline JavaScript to style an element when clicked

Looking for assistance with styling a link. I've connected an external stylesheet to my HTML file, so that part is all set. The goal is to modify the text-decoration when the onclick event occurs. Here's what I currently have: <a class="dopel ...

Everything in my middleware is functioning correctly, however, it is failing to redirect to the designated route

In my code, everything seems to be working fine without any errors, but I'm encountering an issue with the response not redirecting to the expected route. I need some help resolving this issue. I've double-checked for any coding errors or except ...

The background image is not showing up

I've been grappling with a CSS background-image dilemma that's got me stumped. Here's the situation: CSS #yes { background-image:url("../tick.png"); background-repeat: no-repeat; height: 16px; width: 16px; }​ #no { backg ...

"Enhance your website with a dynamic carousel feature using Twitter Bootstrap's JavaScript

After researching various examples, I noticed that most of them only display one Item at a time. I want to have multiple active items displayed using thumbnails, but the examples I found show that the Item itself contains multiple thumbnails inside of it. ...

Implementing ngClass with a dynamic ID in the URL condition

I am attempting to create an ngClass condition that adds an active class to a list element. Specifically, I want it to work for both the URLs '/companies' and '/company/:id'. The issue I am facing is that the current setup does not fun ...

Struggling with a Bootstrap v5.0.2 Modal glitch? Let's dive into a real-life case study to troub

I am seeking assistance with a problem that I am encountering. I am currently using Bootstrap version 5.0.2 (js and css). Despite coding all the required parameters, I am unable to make the modal functionality work. I have been trying to figure out what&ap ...

Adding a slight 1px right margin between an HTML table and its enclosing div in Bootstrap 3

Help! I'm trying to troubleshoot some HTML issues and can't figure out where this pesky 1px gap between my table and the wrapping div is coming from... Just for reference, I am using Bootstrap. I suspect that it could be a glitch in the Bootst ...

There are two separate CSS files linked to the same page, but the browser is only rendering the styles from

I am in the process of developing a new web page where I want to implement my own CSS and incorporate some components from this source: . I'm facing an issue with one of the components, as the browser seems to be applying only the styles from my custo ...

Adaptable Website Design (Without Header)

Check out my current website layout on codepen. I'm looking for a more responsive way to code this with CSS, so that the text doesn't get pushed over when the window is resized. Also, I want to improve the efficiency of coding the CSS for this l ...

Positioning the Submenu in Elementor Site

I've been working on creating a vertical navigation menu with a horizontal submenu for my website using Elementor. While I've made some progress, I'm struggling to position the submenu correctly. My goal is to have the submenu display next t ...

Slower CSS rendering as Javascript finishes its tasks

I am currently dealing with a jQuery plugin that is quite large and complex, but I will not be sharing it here to keep things simple. The issue I am facing is relatively straightforward, so I will focus on the essential code snippets: There is a click eve ...

How can I make sure that index.php displays the current page when I am navigating through categories on the WordPress

In my WordPress website, I have set up categories in the main navigation. Specifically, I have a category named "Home" which corresponds to category 4. To display the Home category (start page), I have added the following code to my index.php file: <?p ...

Understanding the Importance of CSS Specificity in Resolving Selector Conflicts

I need help with a specific pseudo-class selector issue. Even with !important, the text in the li:first-child element is not appearing in blue. The selector specificity for p is: (0, 0, 1) The selector specificity for li:first-child is: (0, 1, 1) I want ...

Resizing an image within an anchor tag

Currently, I am working on styling a page in NextJS. The challenge I'm facing is resizing an image that is inside an anchor within a div. Despite numerous attempts, the image refuses to resize as desired. I've experimented with applying properti ...

Is it common for pdf parsing libraries pdf2json and pdf-parse to encounter compatibility issues with the Next JS app router?

I've run into some challenges while trying to incorporate PDF parsing functionality into my Next JS application. It seems that the libraries pdf2json and pdf-parse are not compatible with the new Next JS app router. To replicate the issue, follow the ...

What is the best way to implement a required input field in Vue.js?

I need some assistance with my chat functionality. I want to prevent users from sending empty messages, so I want to make the input field required. Can you please help me with this? I have already tried adding "required='required'" to the input ...

"Stylish footer design in CSS featuring a sleek overflow scrollbar

Perhaps the most straightforward way to demonstrate this is by providing a direct link to the website where the addition should be made. I am in desperate need of a basic footer that remains fixed at the bottom of the page, regardless of the amount of con ...

Is there a way to incorporate CSS or tables when converting HTML to PDF using JavaScript?

While working on a project, I successfully converted an HTML file into a PDF. However, the output did not display the CSS design. Can anyone provide suggestions on how to include CSS design in the PDF file? Below is the JavaScript function code: $(funct ...

Troubleshooting Locale Persistence in Next.js 13 Application Routing: Internationalization Problems with Maintaining Language Settings between Page Trans

I'm encountering an internationalization problem in my Next.js 13 application with app routing. I carefully followed the official guide on internationalization for Next.js (Next.js Internationalization Guide), and while most features are working corre ...

Is there a way to create a dropdown menu that appears to emerge from behind a button?

I am struggling with the stack order of my dropdown menu. Despite using z-index, it appears in front of the button instead of behind it. I want it to look like it is coming out from behind the button. Here is my code: .subnav-wrapper { position: relat ...