CSS - 鼠标移入整行高亮显示,适用于会员套餐各参数对比页面(display: table,div 转表格形式)

效果图

可根据基础示例和进阶示例,复制进行改造样式。

如下图所示,本文提供 2 个示例。

基础示例

找个 HTML 页面,一键复制运行。

html 复制代码
<body>
    <h1 style="text-align: center;">基础示例</h1>
    <section class="content">
        <div class="row">
            <div class="cell">张三</div>
            <div class="cell">李四</div>
            <div class="cell">王五</div>
        </div>
        <div class="row">
            <div class="cell">张三</div>
            <div class="cell">李四</div>
            <div class="cell">王五</div>
        </div>
    </section>
</body>

<style>
body {
    background: #eeeeee;
}
/* 根节点 */
.content {
    display: table;
    /*display: table时padding会失效*/
    width: 800px;
    margin: 0 auto;
    background: #fff;
}
/* 每行 */
.row {
    display: table-row;
    /*display: table-row时margin、padding同时失效*/
    /* background: red; */
    text-align: center;
}
.row:hover {/* 这里写鼠标移入时的样式 */
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    font-weight: bold;
}
/* 每列 */
.cell {
    display: table-cell;
    /*display: table-cell时margin会失效*/
    padding: 15px;
}
</style>

进阶示例

找个 HTML 页面,一键复制运行。

html 复制代码
<body>
    <h1 style="text-align: center;">进阶示例</h1>
    <section class="content">

        <!-- 头部自定义内容 -->
        <div class="row-hidden">
            <div class="cell">
                <div class="header">
                    <h3>普通会员</h3>
                    <button class="button">立即开通</button>
                    <div style="height: 20px;"></div>
                </div>
            </div>
            <div class="cell">
                <div class="header">
                    <h3>中级会员</h3>
                    <button class="button">立即开通</button>
                    <div style="height: 20px;"></div>
                </div>
            </div>
            <div class="cell">
                <div class="header">
                    <h3>高级会员</h3>
                    <button class="button">立即开通</button>
                    <div style="height: 20px;"></div>
                </div>
            </div>
        </div>
        <!-- END -->

        <!-- 激活高亮区域 -->
        <div class="row">
            <div class="cell">
                <div class="item">
                    [普通会员] 这是第一行
                </div>
            </div>
            <div class="cell">
                <div class="item">
                    [中级会员] 这是第一行
                </div>
            </div>
            <div class="cell">
                <div class="item">
                    [高级会员] 这是第一行
                </div>
            </div>
        </div>
        <div class="row">
            <div class="cell">
                <div class="item">
                    [普通会员] 这是第二行
                </div>
            </div>
            <div class="cell">
                <div class="item">
                    [中级会员] 这是第二行
                </div>
            </div>
            <div class="cell">
                <div class="item">
                    [高级会员] 这是第二行
                </div>
            </div>
        </div>
        <!-- END -->

        <!-- 底部自定义内容 -->
        <div class="row-hidden">
            <div class="cell">
                <div class="bottom">
                    <div style="height: 10px;"></div>
                    <h3>底部内容</h3>
                </div>
            </div>
            <div class="cell">
                <div class="bottom">
                    <div style="height: 10px;"></div>
                    <h3>底部内容</h3>
                </div>
            </div>
            <div class="cell">
                <div class="bottom">
                    <div style="height: 10px;"></div>
                    <h3>底部内容</h3>
                </div>
            </div>
        </div>
        <!-- END -->

    </section>
</body>

<style>
body {
    background: #eeeeee;
}
/* 根节点 */
.content {
    display: table;
    /*display: table时padding会失效*/
    width: 1300px;
    margin: 0 auto;
    border-collapse: collapse;
}
/* 每行 */
.row {
    display: table-row;
    /*display: table-row时margin、padding同时失效*/
    text-align: center;
}
.row-hidden {
    display: table-row;
    /*display: table-row时margin、padding同时失效*/
    text-align: center;
}
.row:hover {/* 这里写鼠标移入时的样式 */
    font-weight: bold;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    cursor: pointer;
    color: red;
}
/* 每列 */
.cell {
    display: table-cell;
    /*display: table-cell时margin会失效*/
    /* 这行代码是为了侧边留空隙使用,同背景色一致造成的假象 */
    border-right: 10px solid #eeeeee;
}


/* 额外样式 */
.item {
    /* padding */
    background: #fff;
    text-align: left;
    padding-left: 30px;
    padding-right: 30px;
    /* padding-left: 35px; */
    padding-bottom: 10px;
    padding-top: 10px;
}
.header {
    background: #fff;
    padding: 20px;
}
.button {
    width: 90%;
    height: 30px;
}
.bottom {
    background: #fff;
    padding: 20px;
}
</style>
相关推荐
吴长建先生1 小时前
Flutter IM 即时聊天应用开发:从 0 到 1 实
css
htzyl2061 天前
前端样式-弹窗淡入淡出,为什么你的 `transition` 一直不生效
前端·css·html
Brookty1 天前
CSS基础知识(二):盒模型、内外边距、外边距折叠与父子元素的布局关系
前端·css
Mh2 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
Brookty2 天前
CSS基础知识(一):选择器、优先级、层叠与继承、边框属性及盒模型
前端·css
htzyl2062 天前
CSS中,`:has()` 能帮你删掉的 4 类 JS
前端·css
jianqiang.xue3 天前
Espressif-IDE进入维护模式:乐鑫把ESP32开发的宝押给VS Code
前端·css·ide
feixing_fx3 天前
CSS 变量(Custom Properties):构建可扩展的主题切换系统
前端·css·前端框架·html·css3
晓得迷路了4 天前
栗子前端技术周刊第 154 期 - Parcel 3 Alpha、JS 调查报告、Vite 漏洞...
前端·javascript·css
ZhengEnCi6 天前
登录页电脑端怎么设计才不空:5 种布局方案与可粘贴 AI 提示词
前端·css·设计