I am developing the online web shop, and making the product list, the following code are…
- User
I am developing the online web shop, and making the product list, the following code are related:
<!-- Brand Filter --> <div class="shop-sidebar__box border border-gray-100 rounded-8 p-32 mb-32"> <h6 class="text-xl border-bottom border-gray-100 pb-24 mb-24">品牌</h6> <ul class="max-h-540 overflow-y-auto scroll-sm"> <li class="mb-24"> <div class="form-check common-check common-radio"> <input class="form-check-input" type="radio" name="brand" id="brand1"> <label class="form-check-label" for="brand1">Apple</label> </div> </li> <li class="mb-24"> <div class="form-check common-check common-radio"> <input class="form-check-input" type="radio" name="brand" id="brand2"> <label class="form-check-label" for="brand2">Samsung</label> </div> </li> <li class="mb-24"> <div class="form-check common-check common-radio"> <input class="form-check-input" type="radio" name="brand" id="brand3"> <label class="form-check-label" for="brand3">Microsoft</label> </div> </li> <li class="mb-24"> <div class="form-check common-check common-radio"> <input class="form-check-input" type="radio" name="brand" id="brand4"> <label class="form-check-label" for="brand4">HP</label> </div> </li> <li class="mb-0"> <div class="form-check common-check common-radio"> <input class="form-check-input" type="radio" name="brand" id="DELL"> <label class="form-check-label" for="DELL">DELL</label> </div> </li> </ul> </div> </div> </div> <!-- Sidebar End --> <!-- Content Start --> <div class="col-lg-9"> <div class="flex-between gap-16 flex-wrap mb-40 "> <div class="position-relative flex-align gap-16 flex-wrap"> <div class="list-grid-btns flex-align gap-16"> <button type="button" class="w-44 h-44 flex-center border border-gray-100 rounded-6 text-2xl list-btn"> <i class="ph-bold ph-list-dashes"></i> </button> <button type="button" class="w-44 h-44 flex-center border border-main-600 text-white bg-main-600 rounded-6 text-2xl grid-btn"> <i class="ph ph-squares-four"></i> </button> </div> <div class="position-relative text-gray-500 flex-align gap-4 text-14"> <label for="product_sort_by" class="text-inherit flex-shrink-0">排序方式: </label> <select class="form-control common-input px-14 py-14 text-inherit rounded-6 w-auto" id="product_sort_by"> <option value="" selected>預設排序</option> <option value="top-rated" <?= ($this->input->get('sort') == "top-rated") ? 'selected' : '' ?>>最熱門</option> <option value="date-desc" <?= ($this->input->get('sort') == "date-desc") ? 'selected' : '' ?>>最新</option> <option value="price-asc" <?= ($this->input->get('sort') == "price-asc") ? 'selected' : '' ?>>價格: 低至高</option> <option value="price-desc" <?= ($this->input->get('sort') == "price-desc") ? 'selected' : '' ?>>價格: 高至低</option> </select> </div> <button type="button" class="w-44 h-44 d-lg-none d-flex flex-center border border-gray-100 rounded-6 text-2xl sidebar-btn"><i class="ph-bold ph-funnel"></i></button> </div> </div> <!-- Product List Container --> <div class="row" id="product-list"> <!-- Products will be loaded here by JS --> </div> <div id="loading" class="text-center py-3" style="display:none;">加載更多商品...</div> </div> <!-- Content End --> </div> </div>CSS: /* ============================= Product Card Css Start ========================== */ .product-card { display: flex; flex-direction: column; /justify-content: space-between;/ align-items: start; }
.product-card:hover .product-card__thumb img { transform: scale(1.05); }
.product-card__cart{ position: absolute; bottom: 0px; left: 0px; width: 100%; }
@media screen and (max-width: 1499px) { .product-card__cart { font-size: 12px; padding: 9px 12px !important; } }
.product-card__badge { border-radius: 16px 0 16px 0; }
.product-card__thumb { /height: 232px;/ width: 100%; transition: 0s !important; }
.product-card__thumb img { max-width: 160px; width: 100%; transition: 0.2s linear; }
.product-card.style-two { flex-direction: row; }
.product-card.style-two .product-card__thumb { width: 260px; height: 190px; }
.list-grid-wrapper:not(.list-view) > .product-card{ max-width: 270px !important; }
@media screen and (max-width: 991px) { .product-card.style-two .product-card__thumb { width: 190px; } }
@media screen and (max-width: 575px) { .product-card.style-two .product-card__thumb { width: 124px; } .list-grid-wrapper:not(.list-view) > .product-card{ max-width: 100% !important; width: 100% !important; } }
@media screen and (max-width: 1199px) { .product-card__thumb { min-height: 160px; }
.product-card__thumb img { max-width: 120px; } .product-card__content .product-card__price span { font-size: 0.75rem !important; } .product-card__content .title .link { font-size: 0.875rem; } .product-card__content .text-xs { font-size: 0.625rem !important; }} /* ============================= Product Card Css End ========================== */
And the current layout is look like the image: https://i.ibb.co/YTfyPFJ4/image.png
Please view the code and the image in link, and then solve the following problem: The product name will be over the tags below the name (e.g. the fourth grid) The first row tags and the second tags should be set within one row, if the length exceed one row, add the + sign at the end for view more by hover (PC) or Click (Mobile)
- Assistant
Thanks for the detailed code and the screenshot reference. The main issue you pointed out:
- Product name overlaps the tags below it, especially when tags are multi-line or there's not enough vertical space.
- Display tags in a single line per product with overflow handling so they don't wrap to a second line.
- If tags overflow the container width (exceed one line), show a
+Nindicator that shows hidden tags on hover (desktop) or click (mobile).
Problem Analysis:
- Currently, tags are placed above the product name within
.product-card__content > .product-card-tags. .product-card-tagsprobably wraps tags naturally, causing vertical overflow and overlap.- Need to limit tags container height to one line, truncate with ellipsis or hide extra tags, then show "+N" for overflow.
- On hover/click, show a tooltip or popup with the full tag list.
Solution Outline
- CSS: Make
.product-card-tagsa single line container that hides overflow and uses ellipsis or hides extra items. - JS (or CSS) toggle: Show hidden tags on hover (desktop) or click (mobile) using a tooltip-like behavior.
- Adjust spacing/margins so product name never overlaps with tags.
Step 1: Change HTML structure slightly to support overflow indicator:
In the product HTML build line inside JS:
Add a container for tags plus an overflow indicator, something like this:
let tags_html = ''; if (row.tags && Array.isArray(row.tags)) { let tagsLinks = row.tags.map(tag => `<span class="tag-item py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50"><a href="<?=base_url("products/tags/")?>${encodeURIComponent(tag)}">${escapeHtml(tag)}</a></span>`).join(' '); tags_html = ` <div class="product-card-tags-container" tabindex="0" aria-haspopup="true" aria-expanded="false" role="button"> <div class="product-card-tags" title="點擊查看全部標籤..."> ${tagsLinks} </div> <span class="tags-more-indicator" aria-label="show more tags" hidden>+0</span> <div class="product-card-tags-popup" hidden> ${tagsLinks} </div> </div> `; }.product-card-tags-container: wrapper that is focusable/clickable.product-card-tags: visible tags container, one line, overflow hidden.tags-more-indicator: +N, shown only if overflow detected.product-card-tags-popup: hidden popup with full tags shown on hover or click
Step 2: Add CSS for single-line tags, overflow, and popup
.product-card-tags-container { position: relative; cursor: pointer; display: inline-block; max-width: 100%; outline: none; } .product-card-tags { display: flex; gap: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; } .product-card-tags .tag-item { flex-shrink: 0; user-select: none; } .tags-more-indicator { background-color: #e6f7ff; color: #1890ff; font-weight: 600; font-size: 12px; margin-left: 4px; padding: 0 6px; border-radius: 12px; cursor: pointer; user-select: none; flex-shrink: 0; } /* Popup with full tags */ .product-card-tags-popup { position: absolute; top: 100%; left: 0; z-index: 100; background: white; border: 1px solid #ccc; border-radius: 8px; padding: 8px 12px; box-shadow: 0 8px 16px rgba(0,0,0,0.15); white-space: normal; display: none; min-width: 160px; max-width: 240px; } .product-card-tags-popup .tag-item { display: inline-block; margin: 4px 6px 4px 0; } /* show popup when container is hovered (desktop) */ @media (hover: hover) and (pointer: fine) { .product-card-tags-container:hover .product-card-tags-popup { display: block; } } /* On focus too (keyboard navigation) */ .product-card-tags-container:focus .product-card-tags-popup { display: block; } /* For mobile, will use JS toggle */
Step 3: Add JS for overflow detection + show +N indicator + click toggle for mobile
Add this JS snippet after your
buildProductHTMLor at page load:function updateTagsOverflow() { document.querySelectorAll('.product-card-tags-container').forEach(container => { const tagsDiv = container.querySelector('.product-card-tags'); const indicator = container.querySelector('.tags-more-indicator'); const popup = container.querySelector('.product-card-tags-popup'); if (!tagsDiv || !indicator || !popup) return; // Check if tags div overflows horizontally if (tagsDiv.scrollWidth > tagsDiv.clientWidth + 1) { // Count tags const totalTags = tagsDiv.children.length; // Approximate number of visible tags by width // We'll remove tags from popup from the left until fits one line let visibleTagsCount = 0; let accumulatedWidth = 0; for(let tag of tagsDiv.children) { accumulatedWidth += tag.offsetWidth + 4; // add gap if(accumulatedWidth <= tagsDiv.clientWidth) visibleTagsCount++; else break; } let hiddenTagsCount = totalTags - visibleTagsCount; if(hiddenTagsCount <= 0) hiddenTagsCount = 1; // fallback indicator.textContent = '+' + hiddenTagsCount; indicator.hidden = false; } else { indicator.hidden = true; } // Hide popup initially popup.style.display = 'none'; container.setAttribute('aria-expanded', 'false'); // Add click toggle for touch devices container.onclick = (e) => { // Prevent link clicks inside tags from closing if (e.target.tagName.toLowerCase() === 'a') return; if(popup.style.display === 'block'){ popup.style.display = 'none'; container.setAttribute('aria-expanded', 'false'); } else { popup.style.display = 'block'; container.setAttribute('aria-expanded', 'true'); } } // Click outside closes popup document.addEventListener('click', function onDocClick(evt){ if(!container.contains(evt.target)){ popup.style.display = 'none'; container.setAttribute('aria-expanded', 'false'); } }); }); } // Call updateTagsOverflow on page load and after renderBatch: window.addEventListener('load', updateTagsOverflow); function renderBatch(index) { if (index >= productBatches.length) { currentBatchIndex = 0; // Loop back to first batch index = 0; } const batch = productBatches[index]; let html = ''; batch.forEach(item => { html += buildProductHTML(item); }); productListElem.insertAdjacentHTML('beforeend', html); // Limit max products chunks: const maxBatchesInDOM = 5; if(productListElem.children.length > maxBatchesInDOM * batchSize) { for(let i = 0; i < batchSize; i++) { if(productListElem.firstChild) productListElem.removeChild(productListElem.firstChild); } } updateTagsOverflow(); // Update after adding products }
Step 4: Adjust margin/padding to prevent overlapping product name/tags
In CSS add spacing between product name and tags container:
.product-card-tags-container { margin-bottom: 4px; } .product-card__content h6.title { margin-top: 4px; margin-bottom: 4px; }Or tweak as per your design, just to keep proper vertical separation.
Summary:
.product-card-tagsnow forced to single line, hide overflow.- If overflow: show
+Nindicator next to tags. - On hover (desktop) or click (mobile), show full tag list in a popup.
- Proper margins to prevent overlap of name and tags.
- This way, product name will never overlap tags, and tag list is neat with more indicator.
Note: You may want to refine the overflow detection by measuring tag widths more precisely or limiting max tags shown inline for smoother UX. The JS approach here provides a good start.
If you want, I can help you modify
buildProductHTMLfunction fully with these changes. Let me know! - User
Please help me modify buildProductHTML function fully with these changes
- Assistant
Certainly! Below is the modified
buildProductHTMLfunction integrating the suggested single-line tags with overflow handling and the+Nindicator along with the popup container.
Full updated
buildProductHTMLfunction:function buildProductHTML(row) { if (!row || !row.variants || row.variants.length == 0) return ''; let variant_price = (row.variants[0].special_price > 0 && row.variants[0].special_price !== '') ? row.variants[0].special_price : row.variants[0].price; let original_price = row.variants[0].price; let special_price = row.variants[0].special_price; let name = escapeHtml(row.name); let slug = escapeHtml(row.slug); let image_sm = escapeHtml(row.image_sm); let category_name = escapeHtml(row.category_name); let store_name = escapeHtml(row.store_name); let seller_slug = escapeHtml(row.seller_slug); // Build tags HTML with container, +N indicator, and popup let tags_html = ''; if (row.tags && Array.isArray(row.tags) && row.tags.length > 0) { const tagsLinks = row.tags.map(tag => `<span class="tag-item py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50"> <a href="<?=base_url("products/tags/")?>${encodeURIComponent(tag)}">${escapeHtml(tag)}</a> </span>` ).join(''); tags_html = ` <div class="product-card-tags-container" tabindex="0" aria-haspopup="true" aria-expanded="false" role="button" title="點擊查看全部標籤..."> <div class="product-card-tags"> ${tagsLinks} </div> <span class="tags-more-indicator" aria-label="show more tags" hidden>+0</span> <div class="product-card-tags-popup" hidden> ${tagsLinks} </div> </div> `; } return ` <div class="col-6 col-md-3 col-lg-2 product-grid mb-30"> <div class="product-card h-100 rounded-16 position-relative transition-2" style="padding:0 !important;"> <div class="product-card__thumb rounded-8 position-relative"> <a href="<?=base_url()?>products/details/${slug}" class="w-100 h-100 flex-center"> <img src="${image_sm}" alt="${name}" class="w-auto rounded-8"> </a> <div class="expand-btn-container bg-white p-2 rounded-pill z-1 position-absolute inset-inline-end-0 inset-block-start-0 me-16 mt-16 shadow-sm"> <button type="button" class="expand-btn w-40 h-40 text-md d-flex justify-content-center align-items-center rounded-circle hover-bg-main-two-600 hover-text-white"> <i class="ph ph-plus"></i> </button> <div class="expand-icons gap-20 my-20"> <button type="button" class="text-neutral-600 text-xl flex-center hover-text-main-two-600 wishlist-btn"> <i class="ph ph-heart"></i> </button> <button type="button" class="text-neutral-600 text-xl flex-center hover-text-main-two-600"> <i class="ph ph-eye"></i> </button> <button type="button" class="text-neutral-600 text-xl flex-center hover-text-main-two-600"> <i class="ph ph-shuffle"></i> </button> </div> </div> </div> <div class="product-card__content w-100 pt-3"> ${tags_html} <h6 class="title text-lg fw-semibold" style="margin-bottom:0;"> <a href="<?=base_url()?>products/details/${slug}" class="link text-line-2" tabindex="0">${name}</a> </h6> <span class="py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50 mt-4 d-inline-block"> <a href="<?=base_url()?>products/category/${encodeURIComponent(category_name)}">${category_name}</a> </span> <span class="py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50 mt-4 d-inline-block"> <a href="<?=base_url()?>sellers/seller_details/${seller_slug}">由${store_name}提供</a> </span> <div class="product-card__price mt-4 mb-4"> ${special_price && special_price > 0 && special_price != original_price ? `<span class="text-gray-400 text-md fw-semibold text-decoration-line-through">${formatCurrency(original_price)}</span>` : ''} <span class="text-heading text-md fw-semibold">${formatCurrency(variant_price)}</span> </div> </div> </div> </div> `; }
Notes:
- The whole tag list is rendered twice inside
.product-card-tags(visible line with overflow) and.product-card-tags-popup(hidden popup with full tags). .tags-more-indicatoris initially hidden and will be updated and toggled visible by the JavaScript overflow detection code.- The container is focusable with keyboard to improve accessibility.
- The
title="點擊查看全部標籤..."shows a hover tooltip hint that user can interact.
Reminder
- Don't forget to also add the CSS and the JS overflow detection and toggling code I provided earlier in your script and stylesheet.
- The tagging system relies on this JS run after the product HTML is injected — so call
updateTagsOverflow()after each batch render.
If you want, I could also prepare the entire CSS and the helper JS snippet again in one single chunk, just ask!
- The whole tag list is rendered twice inside