I am creating a page in React which left side of page contains image with Carousel and right side has its information.
Before display:flex the images are visible but after adding display:flex to place image in left side but the image got shrunken and dots of Carousel is visible like this:
image
Also Carousel circle should be below image not center of the screen
Product.jsx:
import React, { Fragment, useEffect } from 'react'
import Carousel from 'react-material-ui-carousel';
import "./ProductDetails.css";
import { useSelector,useDispatch } from 'react-redux';
import { getProductDetails } from '../../actions/productAction';
import { useParams } from 'react-router-dom';
const ProductDetails = () => {
const {id}=useParams();
const dispatch=useDispatch() ;
const {product,loading,error}=useSelector((state)=>state.productDetails);
useEffect(()=>{
dispatch(getProductDetails(id));
},[dispatch,id])
return (
<Fragment>
<div className="ProductDetails">
<div>
<Carousel>
{product.image &&
product.image.map((item, i) => (
<img
className="CarouselImage"
key={item.url}
src={item.url}
alt={`${i} Slide`}
/>
))}
</Carousel>
</div>
</div>
</Fragment>
)
}
export default ProductDetails;
css is:
.ProductDetails{
width: 100vw;
max-width: 100%;
padding: 6vmax;
box-sizing: border-box;
display: flex;
}
2
Answers
Enabling flexbox via
display:flex
applies some defaults forflex-shrink
,flex-grow
, andflex-basis
.At the moment, the only elements in your component with explicitly defined widths are the
ProductDetails
container (set to100vw
) div and carousel circle buttons (set to1em
each through thereact-material-ui-carousel
package). The flexbox defaults dictate that the ideal size of a container is defined by its contents and although the images are certainly wider, they end up effectively inheriting the width from the carousel buttons, which results in the images getting smaller.To overcome this you can do a few things:
tell flexbox that child elements should fill up the space of their parents
tell flexbox that child elements should expand
See more information here
The reason why the image is shrunken and the dots of the carousel are visible is because the Carousel component is taking up the same amount of space as the ProductDetails container. This is because the Carousel component has a default
width
of 100%.To fix this, you need to set the width of the Carousel component to be the same width as the image. You can do this by adding the following CSS to your
ProductDetails.css
file:This will position the CarouselCircle component below the image.
Here is the updated code for your ProductDetails.jsx file:
JavaScript
And here is the updated CSS for your ProductDetails.css file:
I hope this helps!