CSS 优先级搞砸了

标签 css

为了正确设置 CSS 优先级,我搜索了一下,没有找到类似的东西。依稀记得很久以前有人在评论里发过关于CSS优先级的帖子,但我完全不记得了。因此,我问这个问题 - 一方面是为了得到我的答案 - 并将这个答案存档以供其他人将来引用(包括我自己,以防再次发生这种情况)。

这是我的片段:

.ContainerTitle {
  background: #fff;
  float: left;
  padding: 15px;
  padding-left: 15px;
  border: solid 1px #000;
  font-size: Large;
  color: #000;
  text-align: left;
  cursor: pointer;
  transition: all .1s ease-out;
}
.DownloadThis,
.ViewThis {
  display: inline-block;
  padding: 20px;
  padding-bottom: 10px;
  border: solid 1px #fff;
  width: 40px;
}
/*PROBLEM BELOW HERE HELP!!! */

.showHint {
  margin-top: 20px;
  background: #f1f1f1;
}
.ViewThis:hover .showHint:after {
  content: '*Hint: View Online';
}
.DownloadThis:hover .showHint:after {
  content: '*Hint: Download';
}
/*PROBLEM ABOVE HERE HELP!!! */

.DownloadThis {
  background: #1D9C73;
  color: #fff;
}
.ViewThis {
  background: #7D529E;
  color: #fff;
}
.DownloadThis:hover,
.ViewThis:hover {
  border: solid 1px #000;
  background: #fff;
}
.DownloadThis:hover {
  color: #1D9C73;
}
.ViewThis:hover {
  color: #7D529E;
}
.ContainerTitle:before,
.DownloadThis:before,
.ViewThis:before {
  font-family: FontAwesome;
  font-style: normal;
  font-weight: normal;
  text-decoration: inherit;
  padding-left: 6px;
  padding-right: 8px;
  font-size: 32px;
}
.ContainerTitle:before {
  content: '\f0f6';
  font-size: 24px;
}
.DownloadThis:before {
  content: '\f019';
}
.ViewThis:before {
  content: '\f06e';
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet" />
<div class="ContainerTitle">SYLLABUS:
  <div class="DownloadThis"></div>
  <div class="ViewThis"></div>
  <div class="showHint"></div>
</div>
<div class="ContainerTitle">NOTE:
  <div class="DownloadThis"></div>
  <div class="ViewThis"></div>
  <p class="showHint"></p>
</div>

当我使用此代码将鼠标悬停在所需的类上时,我想在 showHint 类的帮助下显示提示

.ViewThis:hover .showHint:after{content:'*Hint: View Online';}
.DownloadThis:hover .showHint:after{content:'*Hint: Download';}

但是,出于某种原因,它不起作用!

I assume it is because I messed up the priority in CSS setting.



我希望它实现与此类似的效果(但将鼠标悬停在 .DownloadThis.ViewThis 上。 如果不同的通用消息 当我将鼠标悬停在 .ContainerTitle 上时出现,这也很棒!):

.ContainerTitle {
  background: #fff;
  float: left;
  padding: 15px;
  padding-left: 15px;
  border: solid 1px #000;
  font-size: Large;
  color: #000;
  text-align: left;
  cursor: pointer;
  transition: all .1s ease-out;
}
.DownloadThis,
.ViewThis {
  display: inline-block;
  padding: 20px;
  padding-bottom: 10px;
  border: solid 1px #fff;
  width: 40px;
}
.showHint {
  margin-top: 20px;
  background: #f1f1f1;
}
.ContainerTitle:hover .showHint:after {
  content: '*Hint: I want to display text like this!';
}
.DownloadThis {
  background: #1D9C73;
  color: #fff;
}
.ViewThis {
  background: #7D529E;
  color: #fff;
}
.DownloadThis:hover,
.ViewThis:hover {
  border: solid 1px #000;
  background: #fff;
}
.DownloadThis:hover {
  color: #1D9C73;
}
.ViewThis:hover {
  color: #7D529E;
}
.ContainerTitle:before,
.DownloadThis:before,
.ViewThis:before {
  font-family: FontAwesome;
  font-style: normal;
  font-weight: normal;
  text-decoration: inherit;
  padding-left: 6px;
  padding-right: 8px;
  font-size: 32px;
}
.ContainerTitle:before {
  content: '\f0f6';
  font-size: 24px;
}
.DownloadThis:before {
  content: '\f019';
}
.ViewThis:before {
  content: '\f06e';
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet" />
<div class="ContainerTitle">SYLLABUS:
  <div class="DownloadThis"></div>
  <div class="ViewThis"></div>
  <div class="showHint"></div>
</div>
<div class="ContainerTitle">NOTE:
  <div class="DownloadThis"></div>
  <div class="ViewThis"></div>
  <p class="showHint"></p>
</div>

Also, I would like to know why the hover on .ContainerTitle would work while it fails to work on .DownloadThis and .ViewThis ?

Is the answer related to the fact that both .DownloadThis and .ViewThis are chid of .ContainerTitle? If so how does this type of relation affect containers? I request this information so that I will not repeat the same mistake in the future.

最佳答案

为此,您需要下一个兄弟选择器和一般兄弟选择器。了解一下 here .

.ContainerTitle {
  background: #fff;
  float: left;
  padding: 15px;
  padding-left: 15px;
  border: solid 1px #000;
  font-size: Large;
  color: #000;
  text-align: left;
  cursor: pointer;
  transition: all .1s ease-out;
}
.DownloadThis,
.ViewThis {
  display: inline-block;
  padding: 20px;
  padding-bottom: 10px;
  border: solid 1px #fff;
  width: 40px;
}
/*PROBLEM BELOW HERE HELP!!! */

.showHint {
  margin-top: 20px;
  background: #f1f1f1;
}
.ViewThis:hover + .showHint:after {
  content: '*Hint: View Online';
}
.DownloadThis:hover ~ .showHint:after {
  content: '*Hint: Download';
}
/*PROBLEM ABOVE HERE HELP!!! */

.DownloadThis {
  background: #1D9C73;
  color: #fff;
}
.ViewThis {
  background: #7D529E;
  color: #fff;
}
.DownloadThis:hover,
.ViewThis:hover {
  border: solid 1px #000;
  background: #fff;
}
.DownloadThis:hover {
  color: #1D9C73;
}
.ViewThis:hover {
  color: #7D529E;
}
.ContainerTitle:before,
.DownloadThis:before,
.ViewThis:before {
  font-family: FontAwesome;
  font-style: normal;
  font-weight: normal;
  text-decoration: inherit;
  padding-left: 6px;
  padding-right: 8px;
  font-size: 32px;
}
.ContainerTitle:before {
  content: '\f0f6';
  font-size: 24px;
}
.DownloadThis:before {
  content: '\f019';
}
.ViewThis:before {
  content: '\f06e';
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet" />
<div class="ContainerTitle">SYLLABUS:
  <div class="DownloadThis"></div>
  <div class="ViewThis"></div>
  <div class="showHint"></div>
</div>
<div class="ContainerTitle">NOTE:
  <div class="DownloadThis"></div>
  <div class="ViewThis"></div>
  <p class="showHint"></p>
</div>

关于CSS 优先级搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36693325/

相关文章:

html - 设置 HTML 表格中单行的样式

javascript - * { 字体系列 : 'Lato' } with js

css - 超链接悬停时指定div的样式

javascript - jQuery 类菜单显示不正确

html - css 边框会完全改变 div 大小吗?

javascript - Flexbox:一旦另一个 flex 元素的宽度为 0,如何包装一个 flex 元素?

jquery - 使用 jquery 从特定列表中删除背景图像

html - 在编写响应式设计时,对同一页面元素使用多个标记是否被认为是可接受的做法?

jquery - Internet Explorer 中的 html 输入按钮不会出现换行符

javascript - 灯箱只显示第一张图片