I was creating a React Movie app, and then I noticed that the tailwind class’s mx-auto was not working properly for every item of the array. The mx-auto was only working for the first element.
const MoviesList = () => {
return(
<div className='MoviesList space-x-0 space-y-10 bg-red-400 my-10 flex flex-col mx-auto w-11/12 md:space-x-6 md:space-y-0'>
{title.map((data,index)=> <MovieCard key={index} movieTitle={data.name}/>)}
</div>
);
}
export default MoviesList;
const MovieCard = ({movieTitle}) => {
const [moviePoster,setMoviePoster] = useState('');
const getMoviePoster = async () => {
const response = await fetch(`http://www.omdbapi.com/?apikey=c4bceb23&t=${movieTitle}`);
const data = await response.json();
setMoviePoster(data.Poster);
}
getMoviePoster();
return(
<div className= " rounded-xl shadow-xl w-64 mx-auto my-6 overflow-hidden">
<div className='Movieposter w-full'>
<img src={moviePoster}/>
</div>
<div className='Moviedetail w-full bg-blue-200 p-3'>{movieTitle}</div>
</div>
);
}
export default MovieCard;
So In the MovieCard component, I have given some tailwind classes but on rendering only the first item of array is able to get the class (mx-auto) and rest were not able to get the class.
Please help me to resolve this issue.
2
Answers
Try add ‘container’ class with ‘mx-auto’
The classes
space-x-0
andmd:space-x-6
on the parent element applymargin-left
andmargin-right
to the child elements, and the rules have higher specificity thanmx-auto
‘s rule. Since thespace-x-*
applies to (effectively) all but the first element, this is whymx-auto
works only on the first one.You could consider removing the
space-x-*
classes on the parent – they don’t seem to be functionally doing anything significant since the layout of child elements are vertical in nature: