当前位置: 首页 > news >正文

style样式优先级问题【display:block依旧无法显示DOM元素】

文章目录

    • 问题描述
    • 问题原因
      • 根源及样式优先级排序
      • 有问题的代码
    • 解决方案
      • 解决方案一:
      • 解决方案二(不推荐):

问题描述

最近在实现一个记事本的时候遇到了一个问题,需求是当鼠标放在如下列表一条元素上时,这一行出现阴影效果,并且出现删除按钮在这里插入图片描述
正确结果如下图(错误结果就是红色按钮不出现)
在这里插入图片描述
由于距离学习前端三大件时间已久,忘记了优先级的问题。只顾在浏览器控制台与dom元素页面排查错误。最后想到优先级这个事恍然大悟。

问题原因

根源及样式优先级排序

根源:style样式优先级问题,搞明白优先级问题这个问题就不是问题。检查一下自己是不是使用了行内样式将相应的标签隐藏了。

优先级排序:!important>行内样式>样式选择器>继承样式>浏览器默认样式
样式选择器有:内联样式 > ID 选择器 > 类选择器 = 属性选择器 = 伪类选择器 > 标签选择器 = 伪元素选择器
优先级相等实行覆盖原则

值得注意的是:
使用!important要谨慎,一定要优先考虑使用样式规则的优先级来解决问题而不是!important。
只有在需要覆盖全站或外部CSS的特定页面中使用!important。
永远不要在你的插件中使用!important。
永远不要在全站范围的CSS代码中使用!important。

有问题的代码

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>React App</title>

  <link rel="stylesheet" href="index.css">
</head>
<body>
<div id="root">
  <div class="todo-container">
    <div class="todo-wrap">
      <div class="todo-header">
        <input type="text" placeholder="请输入你的任务名称,按回车键确认"/>
      </div>
      <ul class="todo-main">
        <li>
          <label>
            <input type="checkbox"/>
            <span>xxxxx</span>
          </label>
          <button class="btn btn-danger" style="display:none">删除</button>
        </li>
        <li>
          <label>
            <input type="checkbox"/>
            <span>yyyy</span>
          </label>
          <button class="btn btn-danger" style="display:none">删除</button>
        </li>
      </ul>
      <div class="todo-footer">
        <label>
          <input type="checkbox"/>
        </label>
        <span>
          <span>已完成0</span> / 全部2
        </span>
        <button class="btn btn-danger">清除已完成任务</button>
      </div>
    </div>
  </div>
</div>

</body>
</html>

css

/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

/*header*/
.todo-header input {
  width: 560px;
  height: 28px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 7px;
}

.todo-header input:focus {
  outline: none;
  border-color: rgba(82, 168, 236, 0.8);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}

/*main*/
.todo-main {
  margin-left: 0px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding: 0px;
}

.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
    li:hover{
      background-color: #ddd;
    }
    li:hover button{
      display:block !important;
    }
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}

解决方案

解决方案一:

删除掉删除按钮行内的样式display:none

解决方案二(不推荐):

将li:hover button{display:block;}改为 li:hover button{display:block !important;}


问题解决!


相关文章:

  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • 面试宝典------经典
  • node.js环境搭建
  • 【5G核心网】手把手教你将Open5gs托管到k8s(KubeSphere)
  • 空城机在CSDN的四周年创作纪念日
  • C++ Reference: Standard C++ Library reference: C Library: clocale: struct lconv
  • JavaSE进阶--集合(2万字总结)
  • CKA考题 [k8s1.21]
  • AcWing第 70 场周赛题解
  • 读FFA-net: Feature Fusion Attention Network for Single Image Dehazing
  • 测试需求平台4-Flask实现API服务入门实战
  • js单行代码-----dom
  • 模型压缩常用方法简介
  • css常用属性
  • 【Android】Android Binder进程间通信AIDL示例与过程分析
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 3.7、@ResponseBody 和 @RestController
  • Bootstrap JS插件Alert源码分析
  • conda常用的命令
  • EventListener原理
  • HTML5新特性总结
  • java8 Stream Pipelines 浅析
  • java多线程
  • Java方法详解
  • laravel 用artisan创建自己的模板
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • webpack+react项目初体验——记录我的webpack环境配置
  • 编写符合Python风格的对象
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 京东美团研发面经
  • 全栈开发——Linux
  • 使用Swoole加速Laravel(正式环境中)
  • 学习笔记TF060:图像语音结合,看图说话
  • 优秀架构师必须掌握的架构思维
  • 【干货分享】dos命令大全
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • # C++之functional库用法整理
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • #android不同版本废弃api,新api。
  • #if 1...#endif
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (LeetCode 49)Anagrams
  • (zhuan) 一些RL的文献(及笔记)
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (力扣)1314.矩阵区域和
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (三)Honghu Cloud云架构一定时调度平台
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转) Face-Resources
  • (转)JAVA中的堆栈
  • (转)程序员技术练级攻略
  • . NET自动找可写目录
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞