I'm attempting to arrange the movieCards in a horizontal row, but they keep stacking up vertically instead

I have created a movie list component with different attributes in the div section to display the cards in a row. However, all the cards are currently aligned in a column. Should I use flex or grid to achieve the desired layout? If so, how can I implement them? I have consulted various resources but haven't been successful. What steps should I take to display the cards as I intend? Your assistance is greatly appreciated.

import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import MovieCard from './MovieCard';

const MoviesList = (props) => {
    // Component logic
}

export default MoviesList;

And here is the Movie card component:

import React from 'react';
import { useDispatch } from 'react-redux';
import { removeMovie } from '../actions/moviesAction';

const MovieCard = (props) => {
    // Component logic
}

export default MovieCard;

I would appreciate your help in resolving this issue. Thank you.

Answer №1

To enhance the layout of the MovieCard section, consider adding display: flex to the div that renders it. Here's an example:

<div className="row pt-2 justify-content-around" style={{textAlign: 'center', width: '100%', display: 'flex'}}>
            {
                filterBy.length > 0 ? (
                    filterBy.map((movie) => {
                        return <MovieCard key={movie.imdbID} Poster={movie.Poster} Title={movie.Title} imdbRating={movie.imdbRating} imdbID={movie.imdbID} />
                    })
                ) : (
                    show(movies).map((movie) => {
                        return <MovieCard key={movie.imdbID} {...movie} />
                    })
                )
            }
</div>

Answer №2

To achieve the desired layout, simply utilize the flex property.

Include the code snippet below within the parent div (the div containing all the cards).

If you have a separate CSS file, assign a class to the div and apply the following styles:

display: flex;
flex-wrap: wrap;
justify-content: center;

Alternatively, for inline styling, implement the following:

<div className="row pt-2 justify-content-around" style={{ textAlign: 'center', width: '100%', display: "flex", flexWrap: "wrap", justifyContent: "center" }}>
{filterBy.length > 0 ? (
     filterBy.map((movie) => {
         return <MovieCard key={movie.imdbID} Poster={movie.Poster} Title={movie.Title} imdbRating={movie.imdbRating} imdbID={movie.imdbID} />
      })
 ) : (
     show(movies).map((movie) => {
         return <MovieCard key={movie.imdbID} {...movie} />
     }
 )
)}
</div>

Answer №3

Consider utilizing a grid system similar to the code provided below to have more control over the number of rows and columns and their respective sizes.

**

<h1>Sample { grid-template-rows: 40% 50% 50px;}</h1>
<p><strong style="color: red">For a more concise approach, the grid-template property can replace both </strong>grid-template-rows and grid-template-columns.</p>
<hr>
<p style="font-family: Courier">When using grid-template, the values before the slash define the row sizes while those after the slash determine column sizes. </p><hr>
<p style="font-family: Courier">This example showcases a grid with 3 rows and 3 columns.</p><h1>Sample {grid-template: 40% 50% 50px/100px 50% 200px;}</h1><br>
<p><strong style="color: red">Through the use of the (fr) unit, columns and rows can be defined as fractions of the grid’s length and width.</strong><br>Utilizing (fr) simplifies preventing grid items from overflowing the grid boundaries.</p>
 <h1>Sample {grid-template: 1fr 1fr 1fr / 3fr 50% 1fr;}<br> width: 400px;<br>

**

Answer №4

If you're utilizing Bootstrap, there's no requirement to directly use flex or grid. Bootstrap provides its own method to achieve the desired layout:

<div className="row">
   {books.map(item => (
      <div className="col">
        // add your content for the book here
      </div>
   ))}
</div>

Check out the Bootstrap documentation for more information

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

Limited functionality: MVC 5, Ajax, and Jquery script runs once

<script> $(function () { var ajaxSubmit = function () { var $form = $(this); var settings = { data: $(this).serialize(), url: $(this).attr("action"), type: $(this).attr("method") }; ...

Having trouble reaching a public method within an object passed to the @Input field of an Angular component

My configurator object declaration is as follows. export class Config { constructor(public index: number, public junk: string[] = []) { } public count() : number { return this.junk.length; } } After declaring it, I pass it into the input decorated fi ...

The contents table remains fixed in the top right corner as you scroll

I have developed an Angular app with a table-of-contents component that only displays two items. The code for the script is as follows: ts import { Component, OnInit } from '@angular/core'; import { pdfDefaultOptions } from 'ngx-extended-p ...

Troubleshooting Vue.js dynamic image path loading issues

Struggling to dynamically load images in a single file component, I keep encountering an error stating that the module cannot be found. It seems like I'm attempting something similar to what was discussed in this post on Stack Overflow, but I can&apos ...

Do you have any recommendations for a creative CSS method to achieve a full-screen background image?

After searching online and experimenting with different methods, I have yet to discover a solution that suits my needs. My goal is to have the background image on my website centered, filling the entire browser screen, while also being compatible with resp ...

Creating a Full Screen Image with HTML and CSS

On my current web project, I am aiming for an immediate impact with a full-screen image that transitions to reveal text and information when the user starts scrolling. The challenge lies in ensuring this effect is consistent across various screen resolutio ...

Error: It seems that the function (0 , _redux_slices_loginAuthApiSlice__WEBPACK_IMPORTED_MODULE_4__.useLoginMutation) is not a valid function or the value it returns is not iterable

Need help with resolving an issue in NextJS 14. Please guide me if I am doing anything wrong. Here are the package versions: "@reduxjs/toolkit" : "^2.0.1", "next" : "^14.0.4", "react" : "^18", "react-dom" : "^18", "react-redux" : " ...

It is important for the CSS :active state to transition to an "inactive" state after a certain period of

For mobile, I utilized the :hover and :active pseudo-classes to create a transition effect when tapping on a tab that enlarges the elements for more information. However, I am seeking a way for the activated element to return to its original state after a ...

Cease the continuous scrolling of the display element now

I'm having trouble with my progressbar animation when scrolling. It seems to run multiple times instead of just once. I apologize if there's an error in my code. Could someone please assist me? This is the code I am using: <div class="demo- ...

Error: Syntax mishap detected - Oh dear

Recently, I've been developing a small slideshow / public display for a client using the HTML5 Rock's Slideshow code. Everything was going smoothly until I encountered a DOM Exception 12 - an error related to CSS selectors. Strangely, I couldn&ap ...

The dynamically rendered component cannot be assigned to the type 'IntrinsicAttributes & ContentOutlineProps & ContentBrainstormProps'

I am facing an issue on my page where a <SideBar /> component is causing a Typescript error with the setActivePage hook. The error message points to a specific line in my code: const Content: (({ question_blocks, }: ContentBrainstormProps) => JSX. ...

Converting Blob URLs to MP3 Audio Files in Django

Currently, I am tackling the task of recording audio from a browser using the Mic recorder API. This API generates a blob URL after the recording process, which I then send to my Django backend. However, I am facing the challenge of retrieving the audio fi ...

Command to conceal components for users visiting as guests

I'm looking to develop a directive that hides specific elements for guest users. Here is the current code I have: angular.module('someMod') .directive('premiumUser', premiumUser) .controller('PremiumUserCtrl', Pr ...

Can a page name and folder name containing dynamic pages be identical?

insert image overview here In the image above, I have a section called "Movies" along with a directory named movies I am trying to navigate so that if the URL in the address bar is '/movies', it directs me to the "movies.js" page. Only when the ...

Steps for adding an npm dependency as a peer dependency:

Is there a way in npm to install dependencies as peer-dependencies similar to the yarn option --yarn, rather than manually adding them? For instance: "peerDependencies": { "@angular/core": "^7.0.0" } Update: Further clarifi ...

"An error occurs when trying to trigger a .click() event within a list element

There is a list that can contain either plain text or a link. If there is a link present, the entire list element should be clickable. When attempting to execute the following code: if ($('.link').length) { $('li[data-contains-link]' ...

Having trouble with importing a TypeScript class: encountering a "cannot resolve" error message

Could you lend me your expertise? I'm puzzled by this issue that seems to be quite simple and straightforward: export class Rectangle { height: number = 0 width: number = 0 constructor(height: number, width: number) { this. ...

Adaptive design exhibits varying appearances across various screen sizes

Currently, I am working on a responsive design WordPress site and here is the markup: <div class="contact-wrap"> <div class="contact-number">123-456-7890</div><!--.contact-number--> <div class="user-login"><a href="#"& ...

The Ajax script seems to be failing to update the PHP file along with its corresponding variable value

I have a simple form where upon clicking the "=" button, I want the calculated value to be displayed in file process.php. However, this functionality is not working for me. It seems that when the "=" button is clicked, nothing happens. The default value in ...

How to Send Javascript DOM Value to Inner HTML

I am trying to extract a value from a hidden element ID and set it as the inner HTML for another element ID. Below is my code: <script> function replaceText(){ var x=document.getElementById("mainTitle1").textContent; document.getElementById("mainTi ...