如何使用纯 HTML 和 CSS 创建完全响应式产品卡片。视频教程和代码教程可能会对您有所帮助

2025-05-28

如何使用纯 HTML、CSS 创建完全响应的产品卡。

视频教程

代码

您可能会觉得有用的教程

大家好,今天我们将学习如何仅使用纯 HTML 和 CSS 制作完全响应式的产品页面。我们的产品卡片拥有精美的极简动画,带来卓越的用户体验。

如果您想查看演示或编程教程,可以观看下面的教程。

视频教程

如果您能订阅我的 YouTube 频道来支持我,我将不胜感激。

因此,不要浪费更多时间,让我们看看如何编写代码。

代码

对于这个项目,我们只有index.html一个style.css文件。以及img包含 3 张图片的文件夹,您可以从这里下载。

那么,让我们开始编写代码吧。
首先编写基本的 HTML5 结构并将style.css文件链接到页面。然后像这样创建产品卡片结构。

<div class="product">
    <div class="product-img">
        <img src="img/bag.png" alt="">
        <span class="tag">new</span>
    </div>
    <div class="product-listing">

    </div>
</div>
Enter fullscreen mode Exit fullscreen mode
@import url('https://fonts.googleapis.com/css2?family=Dosis:wght@600&family=Roboto:wght@300;400;500;700;900&display=swap');

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body{
    width: 100%;
    min-height: 100vh;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #966e4f;
    font-family: 'roboto', sans-serif;
}

body::before{
    content: '';
    position: absolute;
    left: 0%;
    transform: translateX(-50%) skewX(-15deg);
    width: 20px;
    height: 100%;
    background: #966e4f;
    border-left: 60px solid #eae3d2;
    border-right: 30px solid #eae3d2;
    opacity: 0;
    animation: slide-in 2s 1.5s forwards 1;
}

@keyframes slide-in{
    100%{
        opacity: 1;
        left: 50%;
    }
}

.product{
    position: relative;
    width: 1000px;
    min-width: 350px;
    min-height: 500px;
    height: auto;
    display: flex;
    justify-content: center;
    align-items: center;
}

.product-img{
    width: 40%;
    height: 500px;
    background: #fff;
    position: relative;
    opacity: 0;
    transform: translateY(-50px);
    animation: fade-in 1s forwards 1;
}

.product-img img{
    width: 100%;
    height: 100%;
    object-fit: contain;
    user-select: none;
}

.tag{
    position: absolute;
    top: 20px;
    left: -10px;
    transform-origin: left;
    opacity: 0;
    transform: rotate(-90deg);
    text-transform: capitalize;
    color: #eae3d2;
    padding: 5px 10px;
    width: 100px;
    font-size: 18px;
    text-align: center;
    background: #292929;
    user-select: none;
    animation: tag .5s 1s forwards 1;
}

@keyframes tag{
    100%{
        opacity: 1;
        transform: rotate(-20deg);
    }
}

.product-listing{
    width: 60%;
    min-height: 500px;
    height: auto;
    background: #292929;
    padding: 40px;
    display: flex;
    justify-content: center;
    color: #eae3d2;
    opacity: 0;
    transform: translateY(50px);
    animation: fade-in 1s forwards 1;
}

@keyframes fade-in{
    100%{
        opacity: 1;
        transform: translateY(0);
    }
}
Enter fullscreen mode Exit fullscreen mode
输出

无标题设计

很好,现在创建产品信息元素。

<div class="product-listing">
    <div class="content">
        <h1 class="name">leather bag</h1>
        <p class="info">Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque laborum optio natus quibusdam ea nam odit vitae id unde officia.</p>
        <p class="price">$ 299</p>
        <div class="btn-and-rating-box">
            <div class="rating">
                <img src="img/star.png" alt="">
                <img src="img/star.png" alt="">
                <img src="img/star.png" alt="">
                <img src="img/star.png" alt="">
                <img src="img/star stroke.png" alt="">
            </div>
            <button class="btn">buy now</button>
        </div>
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode
.name{
    font-family: 'dosis';
    font-size: 70px;
    text-transform: capitalize;
}

.info{
    font-size: 18px;
    line-height: 30px;
    margin: 50px 0;
}

.price{
    font-size: 70px;
    font-weight: 100;
    margin-bottom: 20px;
}

.btn-and-rating-box{
    width: 100%;
    display: flex;
    justify-content: space-between;
}

.rating{
    width: fit-content;
    display: flex;
    justify-content: center;
    align-items: center;
}

.rating img{
    width: 20px;
    height: 20px;
    margin: 0 2px;
}

.btn{
    background: #eae3d2;
    color: #292929;
    border: none;
    text-transform: capitalize;
    font-size: 16px;
    padding: 10px 20px;
    cursor: pointer;
}

.btn:hover{
    background-color: #eedbaf;
}
Enter fullscreen mode Exit fullscreen mode
输出

捕获

我们的产品卡片已经完成。现在,让我们让它响应式。

@media (max-width: 1100px){
    body::before{
        transform: translateX(-50%) skewX(-5deg);
    }
    .product{
        flex-direction: column;
        width: 90%;
        margin: 5vh 0;
    }
    .product-img{
        width: 100%;
        height: 300px;
    }
    .product-listing{
        width: 100%;
        min-height: auto;
    }
    .name,.price{
        font-size: 50px;
    }
    .info{
        font: 16px;
    }
}
Enter fullscreen mode Exit fullscreen mode
输出

捕获2

就这样吧。希望你理解了所有内容。如果你有疑问或者我遗漏了什么,请在评论区告诉我。

您可能会觉得有用的教程

  1. 最佳 CSS 效果
  2. 音乐播放器应用
  3. Disney+ 克隆版
  4. Youtube API - Youtube 克隆
  5. TMDB - Netflix 克隆
  6. 带有联系表格的响应式作品集
  7. 功能齐全、带后端的博客网站

如果您能订阅我的YouTube频道,我将不胜感激。我创作了非常棒的网络内容。

感谢您的阅读。

文章来源:https://dev.to/themodernweb/how-to-create-filled-responsive-product-card-using-pure-html-css-12hm
PREV
如何使用纯 HTML、CSS、JS 视频教程代码文章创建音乐播放器,您可能会觉得有用
NEXT
如何使用纯 HTML、CSS、JS 视频教程代码文章创建 Disney Plus 克隆,您可能会发现有用