flex布局实现横向滚动

效果图

代码实现

核心代码

js 复制代码
.box {
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
    overflow-x: scroll;
}
.item {
    width: 100px;
    height: 100px;
    flex: 0 0 auto;
}

完整代码

js 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            margin: 80px auto;
            background-color: #ccc;
            width: 350px;
            background-color: gainsboro;
            display: flex;
            align-items: center;
            flex-wrap: nowrap;
            overflow-x: scroll;
        }
        .item{
            width: 100px;
            height: 100px;
            flex: 0 0 auto;
            line-height: 100px;
            text-align: center;
            color: #fff;
        }
        .item1{
            background-color: #cf2424;
        }
        .item2{
            background-color: #cf9324;
        }
        .item3{
            background-color: #8dcf24;
        }
        .item4{
            background-color: #24cfaa;
        }
        .item5{
            background-color: #2432cf;
        }
        .item6{
            background-color: #cf24b5;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="item item1">1</div>
        <div class="item item2">2</div>
        <div class="item item3">3</div>
        <div class="item item4">4</div>
        <div class="item item5">5</div>
        <div class="item item6">6</div>
    </div>
</body>

</html>
相关推荐
Csvn5 分钟前
性能监控
前端
Csvn31 分钟前
构建工具
前端
FRing031 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
kyriewen3 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒4 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫4 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库
jerrywus4 小时前
我用 GPT-6.1-sol 做了台会讲解的 3D 售货机:60 罐饮料,一口没喝上
前端·agent·claude
孟陬6 小时前
Foobar 技术周刊#159:当看不懂代码时,如何高效排障调试。脏页陷阱:一条只读查询,写入数 TB 数据。推测解码:大模型加速利器
前端·人工智能·后端
Zhao-Jichao6 小时前
AI Prompting for Everyone 课程笔记 - P17 图像生成 Imaging Generation
前端
Highcharts.js6 小时前
智能数据分析组件Orbit 正式发布与快速上手体验教程
前端·人工智能·信息可视化·数据分析系统·highcharts·orbit·智能可视化