Horizontal Product Card for List Views
Efficiently display product information in a compact, horizontal layout with this list view product card.
This design is ideal for secondary display formats like a user-toggled "list view" on a category page, or for layouts where vertical space is at a premium, such as a shopping cart summary.
About this Component
This component arranges the product image and its corresponding details side-by-side using a Flexbox layout. This horizontal orientation allows for more items to be visible on the screen at once compared to a standard vertical card grid. It also includes a media query to stack the layout vertically on very small screens, ensuring readability on mobile devices.
Features
- Horizontal Layout: Uses Flexbox to align the image and content horizontally, creating a space-efficient design.
- Responsive Stacking: A built-in media query switches the card to a vertical, stacked layout on narrow screens to prevent content from becoming too compressed.
- Truncated Description: The product description uses a CSS line-clamping technique to limit the text to two lines, keeping the card height consistent and the layout tidy.
- Clean Information Display: The layout provides clear sections for the product image, title, description, price, and call-to-action button.
Code Breakdown
HTML Structure
Each card is an article with the class .hp-card. It's a flex container holding two direct children. The first is a div wrapping the image. The second child is another div which wraps all the textual content and the footer. This simple two-column structure is the foundation of the horizontal layout.
CSS Styling
The main card (.hp-card) is set to display: flex, which places the image and content side-by-side. The content section is itself a flex container with flex-direction: column, allowing us to use flex-grow: 1 on the main info block to push the footer to the bottom. The text-clamping effect on the description (.hp-card-description) is a modern CSS technique that uses the -webkit-line-clamp property to truncate text after a specific number of lines, adding an ellipsis automatically.
A media query at the end of the stylesheet checks for a screen width of 500px or less. When this condition is met, it changes the main card's flex direction to column, effectively stacking the image on top of the content for a better mobile viewing experience.
Code
Here's the complete, self-contained code. It's ready to be used to build a product list or a shopping cart interface.