\n
Codesandbox: CLICK HERE
\n <div className="resellers">\n {images.slice(0, 2).map((image, i) => (\n <span className="reseller" key={i}>\n <img src={image} alt="" />{" "}\n </span>\n ))}\n </div>\n {images.length > 2 && (\n <div className="plus">\n <span> + {images.length - 2} </span>\n </div>\n )}\n
\n","author":{"@type":"Person","name":"Joseph"},"upvoteCount":3,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"Render all the reseller and the plus div
elements within the resellers
container. You'll want to render the avatars in reverse order so that they can correctly "overlay" the avatar to the right.
<Resellers>\n {!!images.slice(2).length && (\n <ResellerPlus>\n <span>+ {images.slice(2).length}</span>\n </ResellerPlus>\n )}\n {images\n .slice(0, 2)\n .reverse()\n .map((image, i) => (\n <Reseller key={i}>\n <img src={image} alt="reseller" />\n </Reseller>\n ))}\n</Resellers>\n
\nconst Resellers = styled.div`\n display: flex;\n align-items: center;\n flex-direction: row-reverse; // <-- un-reverse avatars\n`;\n
\nCollect all the common CSS into the "reseller" div.
\nconst Reseller = styled.div`\n align-items: center;\n color: #fff;\n background-color: #bdbdbd;\n border: 4px solid #fff; // <-- white space around\n border-radius: 50%; // <-- circle divs\n display: flex;\n flex-direction: row;\n font-family: Nunito-Bold;\n font-size: 1rem;\n height: 40px;\n overflow: hidden; // <-- hide edges of images\n text-align: center; // <-- center text\n width: 40px;\n\n :not(:first-child) {\n margin-right: -0.5rem; // <-- apply overlap\n }\n\n img {\n margin-right: 0.5rem;\n object-fit: contain;\n }\n`;\n\nconst ResellerPlus = styled(Reseller)`\n font-family: Nunito-SemiBold;\n\n span {\n width: 100%; // <-- help center text\n }\n`;\n
\n\n\n","author":{"@type":"Person","name":"Drew Reese"},"upvoteCount":3}}}Reputation: 7785
I wanted the images to overlap if images is more than 1. But only show 3 images, then add a circular shape with a plus and the number of images left. My problem is how do I overlap them the correct way?
Something like this
Codesandbox: CLICK HERE
<div className="resellers">
{images.slice(0, 2).map((image, i) => (
<span className="reseller" key={i}>
<img src={image} alt="" />{" "}
</span>
))}
</div>
{images.length > 2 && (
<div className="plus">
<span> + {images.length - 2} </span>
</div>
)}
Upvotes: 3
Views: 691
Reputation: 203471
Render all the reseller and the plus div
elements within the resellers
container. You'll want to render the avatars in reverse order so that they can correctly "overlay" the avatar to the right.
<Resellers>
{!!images.slice(2).length && (
<ResellerPlus>
<span>+ {images.slice(2).length}</span>
</ResellerPlus>
)}
{images
.slice(0, 2)
.reverse()
.map((image, i) => (
<Reseller key={i}>
<img src={image} alt="reseller" />
</Reseller>
))}
</Resellers>
const Resellers = styled.div`
display: flex;
align-items: center;
flex-direction: row-reverse; // <-- un-reverse avatars
`;
Collect all the common CSS into the "reseller" div.
const Reseller = styled.div`
align-items: center;
color: #fff;
background-color: #bdbdbd;
border: 4px solid #fff; // <-- white space around
border-radius: 50%; // <-- circle divs
display: flex;
flex-direction: row;
font-family: Nunito-Bold;
font-size: 1rem;
height: 40px;
overflow: hidden; // <-- hide edges of images
text-align: center; // <-- center text
width: 40px;
:not(:first-child) {
margin-right: -0.5rem; // <-- apply overlap
}
img {
margin-right: 0.5rem;
object-fit: contain;
}
`;
const ResellerPlus = styled(Reseller)`
font-family: Nunito-SemiBold;
span {
width: 100%; // <-- help center text
}
`;
Upvotes: 3
Reputation: 193
Adding something like this to the .reseller
class should get the basic effect.
.reseller {
margin-right: -15px;
border-radius: 100%;
border: solid white 3px;
}
If you want to stack them left in front of right, you can use z-index
or flex-direction
to change the order they are rendered at.
Upvotes: 0