Sleek and modern responsive carousel design with TailwindCSS

I am currently troubleshooting an issue with the Tailwind CSS Carousel related to responsiveness.

While the Carousel displays correctly on most devices, I am facing a problem on mobile devices. When the screen size is reduced, instead of scaling down, the images in the carousel are being cropped:

 <div className=" h-1/2 w-full relative group">
      <div
        style={{ backgroundImage: `url(${slides[currentIndex].url})` }}
        className="h-5/6 bg-cover duration-500 overflow-hidden"
      ></div>
      {/* Left Arrow */}
      <div className="hidden group-hover:block absolute top-[50%] -translate-x-0 translate-y-[-50%] left-5 text-2xl rounded-full p-2 bg-black/20 text-white cursor-pointer">
        <BsChevronCompactLeft onClick={prevSlide} size={30} />
      </div>
      {/* Right Arrow */}
      <div className="hidden group-hover:block absolute top-[50%] -translate-x-0 translate-y-[-50%] right-5 text-2xl rounded-full p-2 bg-black/20 text-white cursor-pointer">
        <BsChevronCompactRight onClick={nextSlide} size={30} />
      </div>
      <div className="flex top-4 justify-center py-2">
        {slides.map((slide, slideIndex) => (
          <div
            key={slideIndex}
            onClick={() => goToSlide(slideIndex)}
            className="text-2xl cursor-pointer"
          >
            <RxDotFilled />
          </div>
        ))}
      </div>
    </div>

Answer №1

Using background-size: contain; will ensure that the image scales down proportionally with the screen size.

Let's compare the effects of using cover and contain values for background sizing in CSS:

.div1{
  background:no-repeat url('https://randomwordgenerator.com/img/picture-generator/53e1d1414b5bab14f1dc8460962...);
  background-size:contain ;
  width:100%;
  height:200px;
  border:5px solid green;
}
.div2{
  background:no-repeat url('https://randomwordgenerator.com/img/picture-generator/53e1d1414b5bab14f1dc8460962...);
  background-size:cover ;
  width:100%;
  height:200px;
  border:5px solid green;
}
<div class='div1'>
</div>
<br/>

<div class='div2'>
</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

experiencing difficulties setting up reactJS on my system

I'm running into issues while trying to set up reactJS by following this guide: https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm My setup includes: npm 4.2.0 node 7.10.0 Red Hat Enterprise Linux Server release 7.2 (Maipo) Here is ...

Alignment of Material-UI dialogue buttons on the left side

I have a Dialog containing three buttons as shown below: https://i.stack.imgur.com/T6o35.png Here is the JSX code: <DialogActions classes={{ root: this.props.classes.dialogActionsRoot }} > <Button classes={{ root: this.props ...

Show outcomes in a single div with 2 columns

I am having an issue with the arrangement of child elements in a parent div. Here is the code that I am working with: JSBin These items are collapsible panels. When all of them are collapsed, everything looks fine. However, when you expand the first or ...

The correct way to use the useState hook for implementing an icon in React

My attempt at implementing a feature in React allows users to see active feedback on the screen by changing its icon when clicked. How can I make it change to another icon (e.g. from Box to BoxChecked) once it is clicked? Note: I believe this code line i ...

How do you go about indenting a paragraph in CSS following the insertion of

My objective: To have all paragraphs indented except for the ones following the .firstCharacter class. In some sections of my page, I utilize a drop cap to start off the first paragraph. I have configured all subsequent paragraph tags to be indented using ...

Adjusting the height of the background image using a percentage value

I'm struggling to get my background image to fill 100% of the width and only 60% of the height. I've tried using "vw" and "vh" for setting the size, but it's still not displaying properly. I even experimented with setting it as a block eleme ...

Position various images at specific coordinates on the screen using CSS

Looking for help with positioning four images on the screen using coordinates specified in the code. The top-left coordinate positioning is not displaying properly as intended. Can you assist in identifying and correcting the error? <!DOCTYPE html&g ...

Activate the timepicker in Bootstrap when the user clicks on the input field

Struggling to implement a timepicker feature? Look no further than this resource. After adding the necessary js and css to your project, you may encounter issues with the popup not appearing when clicked. The code provided offers a TextBox control with s ...

Developing data visualizations in React.js

Looking to implement a donut chart similar to the one shown in this image https://i.sstatic.net/8Pl0g.png Seeking guidance on how to create this and any recommended libraries for achieving this design. ...

Issue with building new Bare Expo app on Expo SDK 42 for the first time

My React Native app was created using expo init AwesomeProject I opted for the bare workflow. However, when I attempt to run the app with yarn android, the build fails. This is the error message I encounter: > Task :unimodules-react-native-adapter:c ...

Encountering an issue with AWS Amplify in Next.js where the properties of undefined are unable to be read, specifically in relation to 'graphql

Here's a refined version of your query tailored for Stack Overflow with properly formatted code: Title: "Error Message: Cannot read properties of undefined (reading 'graphql') while using AWS Amplify in Next.js" Context: Current ...

A guide on incorporating Bootstrap 5 modal into an Angular project

I have added bootstrap 5 to my index.html file <link href="assets/css/bootstrap.min.css" rel="stylesheet" media="screen"> I attempted to implement the code from Bootstrap documentation on my Angular page, but it's not functioning correctly. ...

Using CSS/HTML5 with either the :target or :active selector can allow for the display of different text content when a

Is it possible to change the text displayed on a website without reloading the page when a link is clicked? For instance, if I have an "About" or "Contact" link on my personal website, can the text in the body of the page be switched dynamically? In the i ...

Tips for creating a custom Menu with content overflow in a single direction

What is the goal of this task? I have developed a custom Menu in my application with 8 options. There is a need to dynamically disable certain menu options based on specific conditions (e.g. disabling the last 4 options). When a user hovers over a disable ...

Example code demonstrating how to include an annotation or note using Javascript

I need help developing a feature in an HTML file that I'm opening with Webkit. The goal is to create an app where users can select text and mark it as a note by clicking a 'Note Text' button. After pressing the button, I want a note image t ...

Ways to eliminate a particular element from the React state

I need help removing a specific item from my React state. Current scenario: When certain checkboxes are selected, I update their state accordingly. My useEffect hook has the checkboxes state as its dependency, and every time there is a change, I add cont ...

React will only display a single component at a time

Component 1: Only Renders When by Itself const React = require('react'); class URI extends React.Component {render() { return <h1>URI</h1>; }} module.exports = URI; Component 2: Renders Correctly, but Disappears When Another Compone ...

Issue with spacing in CSS sticky header on mobile devices

When implementing CSS position sticky for a floating header, I have noticed that on some mobile devices there is a small gap of around 1px between "element_b" and "element_c" while scrolling. #element_a{ width: 100%; position: -webkit-sticky !impo ...

Tips on how to optimize form input mapping without losing focus on updates

I am facing an issue with a form that dynamically renders as a list of inputs. The code snippet looks like this: arrState.map((obj,index)=>{ return( <input type="text" value={obj} onChange{(e) => stateHandler(e,index)}<inpu ...

JQuery Mobile pop-up error: Property 'is' is not defined

I am attempting to utilize jQuery to open a dialog box. I have followed the instructions provided in this link: Here is the code snippet for the dialog: <div data-role="popup" id="popupDialog" data-overlay-theme="a" data-theme="c" style="max-width ...