I have a horizontal scrolling div with several child items. As you can see from the example, depending on the width, child items can get cropped. So you might get 4 and a 1/2 visible items.
Ideally I’d always show 4-5 items. Although I have a fixed with on the .p-card
for now, this will be responsible.
So normally I’d set 20%
or 25%
at breakpoints to do this. But because the parent is 100% wide I can’t see a way to do this.
Would I need to use Javascript to detect the width of .palette
and divide that by the number of items I want to show (minus margins) and apply that to each .swatch
.
Or is there a better/simpler way?
.p-card {
background: #eee;
box-sizing: border-box;
display: flex;
flex-shrink: 0;
overflow: hidden;
padding: 30px 15px;
max-width: 300px;
}
.palette {
display: flex;
overflow-x: scroll;
}
.palette__inner {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE and Edge */
}
.palette__inner::-webkit-scrollbar {
display: none;
}
.palette__group {
display: flex;
flex-direction: column;
}
.palette__title {
font-family: "Arial", sans-serif;
font-size: 11px;
font-weight: normal;
margin: 0 10px 10px 0;
padding: 0;
position: sticky;
align-self: flex-start;
left: 0;
}
.palette__swatches {
display: flex;
}
.swatch {
background: red;
display: flex;
height: 20px;
margin-right: 10px;
scroll-snap-align: start;
width: 40px;
}
<div class="p-card">
<div class="palette">
<div class="palette__inner">
<div class="palette__group">
<h4 class="palette__title">Classic</h4>
<div class="palette__swatches">
<div class="swatch" style="background: red;"></div>
<div class="swatch" style="background: red;"></div>
<div class="swatch" style="background: red;"></div>
<div class="swatch" style="background: red;"></div>
<div class="swatch" style="background: red;"></div>
<div class="swatch" style="background: red;"></div>
</div>
</div>
<div class="palette__group">
<h4 class="palette__title">Matte</h4>
<div class="palette__swatches">
<div class="swatch" style="background: blue;"></div>
<div class="swatch" style="background: blue;"></div>
<div class="swatch" style="background: blue;"></div>
<div class="swatch" style="background: blue;"></div>
<div class="swatch" style="background: blue;"></div>
<div class="swatch" style="background: blue;"></div>
</div>
</div>
<div class="palette__group">
<h4 class="palette__title">Glimmer</h4>
<div class="palette__swatches">
<div class="swatch" style="background: green;"></div>
<div class="swatch" style="background: green;"></div>
<div class="swatch" style="background: green;"></div>
<div class="swatch" style="background: green;"></div>
<div class="swatch" style="background: green;"></div>
<div class="swatch" style="background: green;"></div>
</div>
</div>
</div>
</div>
</div>