Image dynamically resizes when viewed on different screen sizes using tailwind CSS

I'm currently working on a React and Tailwind project. One of the components in my project includes an image and text, which is laid out like this on small screens:

https://i.sstatic.net/d6hku.png

The component I'm referring to is the card under the word "Events". It looks perfect in this layout, but on larger screens, I want it to be more horizontal like this:

https://i.sstatic.net/yDPuo.png

Both of these layouts are exactly what I envisioned. However, when I resize the window to a certain size (somewhere between md and sm), the image shrinks and doesn't fill the card completely. Here's a visual representation of the issue:

https://i.sstatic.net/yAdOc.png

You can see that the height of the image only takes up about 2/3 of the card's height. I tried adjusting the width of the image to half of the card's width in this situation, but couldn't get it right.

Below is the code snippet for the component:

<div className='flex flex-col w-auto bg-white border border-gray-500 rounded-t-xl overflow-hidden lg:flex-row md:rounded-xl'>
    {/* Image Container */}
    <div className='xs:w-full lg:w-1/4'>
        <img className='object-cover' src={event.featured_image} alt={event.title} />
    </div>
    {/* Content Container */}
    <div className="flex flex-col justify-between p-2">
        {/* Content */}
        <div>
            <p className='text-xl text-blue-700 font-semibold'>{event.title}</p>
            <p className='py-2 text-gray-700'>{event.description}</p>
        </div>
        <div className='pt-8 sm:pt-0'>
            <p className='text-sm text-blue-500'>{event.start_date}{event.end_date ? ` - ${event.end_date}` : ''}</p>
        </div>
    </div>
</div>

Answer №1

Include the flex-shrink-0 property and apply it to the image element to prevent it from shrinking

<div className='xs:w-full lg:w-1/4 flex-shrink-0'>
    <img className='object-cover' src={event.featured_image} alt={event.title} />
</div>

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

Sequentially animate objects with a fade-out and fade-in effect

Attempting to achieve a fade-out, fade-in animation using JavaScript and CSS. Here is the CSS animation code: @keyframes fade-in{ 0%{ opacity: 0%; } 100%{ opacity: 100%; } } @keyframes fade-out{ 0%{ opacity: 100%; } 100%{ opacity: 0%; } } Impleme ...

Creating a proxy using the Next.js API is not compatible with the next-http-proxy-middleware package

I'm attempting to create a proxy using the Next.js API, but it appears to be malfunctioning. This is how my pages/api/index.js file looks: import { NextApiRequest, NextApiResponse } from 'next'; import httpProxyMiddleware from 'next-ht ...

The image does not resize vertically to fit the browser window

How can I make a large background image stretch to the size of the browser window when a user first visits the site, similar to how it is done on ? The current code I am using stretches horizontally but won't stretch vertically past a certain min-heig ...

Dealing with images on my live React application

For managing static images in my react app, I have integrated cloudinary as a CDN service. Can anyone suggest a seamless way to switch between using local image folders during development and switching to the CDN URL for production efficiently? ...

The Recoil Effect is acting unusually

I'm diving into Recoil for the first time. Just created a sample form with 2 buttons - one for decreasing and one for increasing a counter. I decided to add an effect just to test it out, but strangely enough, the effect only triggers when I decrease ...

Looking for a Horizontal Layout?

@foreach (var item in APModel) { <div class="row row-cols-1 row-cols-md-2 g-4"> <div class="col"> <div class="card" style="width: 18rem;"> <img src="h ...

How can I incorporate a new object into an existing object in ReactJS?

I am facing an issue where I have an object named waA that is required in the final step. However, in ReactJS, the new object is not updating the previous object using a switch statement in a function. Below is the code for the object: waA = { jso ...

Error occurs in ReactJS App when the state is updated in Redux only after dispatching the same action twice

Check out the following code snippet: ChartActions.js import * as types from './ChartTypes.js'; export function chartData(check){ return { type: types.CHART_DATA,check }; } ChartTypes.js export const CHART_DATA = 'CHART_DATA ...

I seem to be experiencing an issue with the script tag not functioning properly in my

I'm facing an issue with the script tag in index.html not functioning properly. I can't figure out why it's not working despite trying to add it into another file and providing a link. let stars = document.getElementById('stars' ...

Failing to update the state with the data returned from a fetch request

Despite being able to console.log(data) before and after the line of code with setState, I am facing difficulties in successfully setting the state of confirmationDetail. However, the cart functionality is working fine. placeOrder(information) { cons ...

Issue with multiple dropdowns not functioning in Bootstrap 5

Before you criticize my imports, here are the scripts I am using: <script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script ...

What are the ways to enhance a React component?

Imagine there is a React component that catches your eye, but you want to tweak it to fit your needs. To illustrate, let's take Material UI's LinearProgress. Suppose I want to turn it into a clickable seek bar. class SeekBar extends LinearProgre ...

The lifecycle of a Field component in React JS with Formik does not inherit the same props as its parent Formik component

I am working on a parent component that uses Formik Parent Component: <Formik validationSchema={validationSchema} render={({ props }) => ( //… more code <ChildrenComponent props={props} /> )} /> Child ...

jQuery issue: Inability of "Read More" span to reappear after clicking "Read Less"

Currently in the process of creating a portfolio website that showcases project descriptions with an excerpt, revealing full details upon clicking "Read More." My experience with jQuery is limited, but I managed to implement functionality where clicking "R ...

Create a division element with an image that can be dragged around

I'm having trouble making a div element draggable. Inside the div, there is an img and some text that acts as a label for the image. The issue is that when I begin dragging by clicking on the img, it's the img that gets dragged instead of the par ...

The error that has occurred is: `TypeError: The object on the right side of the 'instanceof' keyword is

Testing my function that verifies if a variable is an instance of a specific type and performs an action has been successful. I conduct the verification using the following code: myCheckingFunction = () => { if (target instanceof H.map.Marker) { ... ...

Issue: The "tus-js-client" npm package is not permitted to access the potentially unsafe "Location" header

Currently, I am utilizing the npm package found at https://www.npmjs.com/package/tus-js-client for uploading files in my react.js project. While it functions smoothly on most devices, some are encountering the error message "Refused to get unsafe header ...

What is the most effective way to adjust the size of my list items using classes and

I'm looking to adjust the size of my <li><a href="Next word requires resizing">WORD HERE REQUIRES RESIZING</a></li> Below is an example code snippet: <li> <a href="index.html"> Home </a> </li> <li & ...

Track changes to pivot tables in React using webdatarocks

I'm currently in the process of integrating webdatarocks into my React project. Following the tutorial provided in the official documentation has allowed me to successfully load the desired report and display the pivot table within my React component. ...

What are the best practices for integrating PrimeVue with CustomElements?

Recently, I decided to incorporate PrimeVue and PrimeFlex into my Vue 3 custom Element. To achieve this, I created a Component using the .ce.vue extension for the sfc mode and utilized defineCustomElement along with customElements.define to compile it as a ...