Got it! To make the seller categories responsive across PC, tablet, and mobile, we need to ensure:
- On wider screens (desktop/tablet), the tags behave as before: single row, collapse overflow.
- On smaller screens (mobile), since horizontal space is limited, it’s better to allow wrapped tags (multiple lines) initially, but still collapse if there are too many lines (e.g. more than 2 lines), with the "+N" toggler to expand all.
Updated approach to responsiveness:
- Default behavior on desktop and tablet: single line, overflow collapsed.
- On mobile (max-width: 600px, for example), allow wrapping up to 2 lines max before collapsing; if more lines, collapse excess categories behind "+N".
- Keep the same toggling for "Show more"/"Show less".
- CSS media queries to control flex-wrap behavior.
- JS updated to detect line count on mobile to decide which tags to show.
Here is the updated CSS (add or replace your current CSS with this):
/* Basic seller-list-tag container */
.seller-list-tag-container {
display: flex;
flex-wrap: nowrap; /* single line by default */
gap: 6px;
overflow: hidden;
align-items: center;
font-size: 13px;
line-height: 1.2;
user-select: none;
max-width: 100%;
}
/* Category tag style */
.seller-list-tag-item {
background-color: #eee;
color: #333;
padding: 3px 8px;
border-radius: 6px;
white-space: nowrap;
flex-shrink: 0;
cursor: default;
box-sizing: border-box;
}
/* "+N" toggle style */
.seller-list-tag-more {
background-color: #ccc;
color: #000;
cursor: pointer;
padding: 3px 8px;
border-radius: 6px;
flex-shrink: 0;
user-select: none;
}
/* Expanded mode shows all tags and wraps on all screens */
.seller-list-tag-container.expanded {
flex-wrap: wrap;
overflow: visible;
}
.seller-list-tag-container.expanded .seller-list-tag-more {
background-color: #999;
}
/* Hover effect */
.seller-list-tag-more:hover {
background-color: #bbb;
}
/* === RESPONSIVE === */
/* Tablet and up (min-width 601px) - same as default */
@media (min-width: 601px) {
.seller-list-tag-container {
flex-wrap: nowrap;
}
}
/* Mobile (max-width 600px) - allow wrapping */
@media (max-width: 600px) {
.seller-list-tag-container {
flex-wrap: wrap;
max-height: 3.6em; /* approx 2 lines (1.8em*2) to limit height when collapsed */
overflow: hidden;
position: relative;
transition: max-height 0.3s ease;
}
/* Expanded mode lifts height limit */
.seller-list-tag-container.expanded {
max-height: 1000px; /* big enough to show all */
}
/* Show fade out at bottom to indicate more when collapsed */
.seller-list-tag-container:not(.expanded)::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 1.2em;
background: linear-gradient(rgba(255,255,255,0), rgba(255,255,255,1));
pointer-events: none;
}
/* Show toggle button inline block at bottom-right with some margin */
.seller-list-tag-more {
flex-shrink: 0;
margin-left: auto;
margin-top: 4px;
}
}
Updated JavaScript to handle responsive logic
Replace your existing JS with this:
document.addEventListener('DOMContentLoaded', function() {
const containers = document.querySelectorAll('.seller-list-tag-container');
// Helper to get line count by comparing container height and line height
function getLineCount(element) {
const lineHeightStr = getComputedStyle(element).lineHeight;
const lineHeight = lineHeightStr === "normal" ? 18 : parseFloat(lineHeightStr);
return Math.round(element.offsetHeight / lineHeight);
}
containers.forEach(container => {
const dataCategories = container.getAttribute('data-categories');
if (!dataCategories) return;
const categories = dataCategories.split(',').map(c => c.trim()).filter(Boolean);
if (categories.length === 0) return;
container.innerHTML = '';
const tagElems = categories.map(cat => {
const span = document.createElement('span');
span.className = 'seller-list-tag-item';
span.textContent = cat;
return span;
});
const moreToggle = document.createElement('span');
moreToggle.className = 'seller-list-tag-more';
moreToggle.style.display = 'none';
moreToggle.textContent = '';
container.append(...tagElems, moreToggle);
// Helper: check if mobile view
function isMobile() {
return window.matchMedia('(max-width: 600px)').matches;
}
function updateLayout() {
if (!isMobile()) {
// DESKTOP/TABLET: single line, nowrap, collapse overflow tags
container.classList.remove('expanded');
moreToggle.style.display = 'none';
tagElems.forEach(t => t.style.display = 'inline-flex');
container.style.maxHeight = null;
const containerWidth = container.clientWidth;
let totalWidth = 0;
let lastIndexThatFits = -1;
for (let i=0; i<tagElems.length; i++) {
const tag = tagElems[i];
const style = getComputedStyle(tag);
const marginLeft = parseFloat(style.marginLeft) || 0;
const marginRight = parseFloat(style.marginRight) || 0;
const tagWidth = tag.offsetWidth + marginLeft + marginRight;
if (totalWidth + tagWidth <= containerWidth) {
totalWidth += tagWidth;
lastIndexThatFits = i;
} else {
break;
}
}
if (lastIndexThatFits === tagElems.length -1) {
// all fit
moreToggle.style.display = 'none';
} else if (lastIndexThatFits >= 0) {
for (let i=0; i<=lastIndexThatFits; i++) tagElems[i].style.display = 'inline-flex';
for (let i=lastIndexThatFits+1; i<tagElems.length; i++) tagElems[i].style.display = 'none';
const hiddenCount = tagElems.length - (lastIndexThatFits + 1);
moreToggle.textContent = `+${hiddenCount}`;
moreToggle.style.display = 'inline-flex';
} else {
// none fit at all
tagElems.forEach(t => t.style.display = 'none');
moreToggle.textContent = `+${tagElems.length}`;
moreToggle.style.display = 'inline-flex';
}
} else {
// MOBILE: allow wrapping but limit to ~2 lines height when collapsed
container.classList.remove('expanded');
tagElems.forEach(t => t.style.display = 'inline-flex');
moreToggle.style.display = 'none';
container.style.maxHeight = null;
// Use maxHeight from CSS (~2 lines height)
// Check if tags overflow more than 2 lines, if yes show +N
const maxCollapsedHeight = parseFloat(getComputedStyle(container).maxHeight);
if (container.scrollHeight > maxCollapsedHeight) {
// Calculate how many tags are basically in overflow lines
// A simplistic approach: hide tags from end (except toggle)
// and find minimal set to fit in 2 lines approx:
// We'll iterate hiding tags from the end until scrollHeight fits maxCollapsedHeight
let hiddenCount = 0;
for (let i = tagElems.length - 1; i >= 0; i--) {
tagElems[i].style.display = 'none';
hiddenCount++;
if (container.scrollHeight <= maxCollapsedHeight) {
break;
}
}
moreToggle.textContent = `+${hiddenCount}`;
moreToggle.style.display = 'inline-flex';
}
}
}
updateLayout();
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(updateLayout, 200);
});
moreToggle.addEventListener('click', () => {
if (container.classList.contains('expanded')) {
container.classList.remove('expanded');
moreToggle.textContent = ''; // will be reset in updateLayout
updateLayout();
} else {
container.classList.add('expanded');
tagElems.forEach(t => t.style.display = 'inline-flex');
moreToggle.textContent = '−';
container.style.maxHeight = 'none';
moreToggle.style.display = 'inline-flex';
}
});
});
});
How it works:
- Desktop & tablet: same as before — single line, collapse overflow tags, show +N.
- Mobile: container flex-wraps allowing multiple lines, but CSS restricts height to roughly 2 lines with overflow hidden and a fade effect.
- JS checks if actual content overflows beyond that height and hides the last tags until content fits into 2 lines, showing "+N" for hidden.
- Clicking "+N" expands container to full height, showing all tags.
- Clicking again collapses.
Summary:
- Simply add the CSS & JS above to your page.
- Leave your current HTML/PHP markup as is.
- The categories show nicely adjusted for all screen sizes.
If you want I can provide a full demo HTML snippet with your container and this snippet included for quick testing — just let me know!