为了正确的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类的帮助下显示Hint
.ViewThis:hover .showHint:after{content:'*Hint: View Online';}
.DownloadThis:hover .showHint:after{content:'*Hint: Download';}
但是,出于某种原因,无效!
我认为这是因为我搞砸了CSS设置中的优先级。
我希望它能达到与此相似的效果(但将鼠标悬停在 .DownloadThis 和 .ViewThis。 如果不同的通用消息< / strong>当我将鼠标悬停在 .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>
另外,我想知道为什么悬浮在.ContainerTitle上会无法正常工作.DownloadThis和.ViewThis?
答案是否与以下事实有关:.DownloadThis和.ViewThis是.ContainerTitle的chid?如果是这样,这种关系如何影响容器?我要求提供这些信息,以便将来不再重复同样的错误。
答案 0 :(得分:1)
它不起作用,因为.showHint
不是.ViewThis
的后代。您需要使用general sibling selector (~
):
.ViewThis:hover ~ .showHint:after{content:'*Hint: View Online';}
.DownloadThis:hover ~ .showHint:after{content:'*Hint: Download';}
示例:
.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';
}
&#13;
<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>
&#13;
答案 1 :(得分:1)
你需要下一个兄弟和一般兄弟选择器。阅读它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';
}
&#13;
<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>
&#13;
答案 2 :(得分:0)
它不起作用,因为.showHint
不是.DownloadThis
或.ViewThis
的孩子。
使用波浪号(普通兄弟)选择器,然后选择.showHint
范围内的下一个(和所有).ContainerTitle
元素。
.ViewThis:hover ~ .showHint:after {
content: '*Hint: View Online';
}
.DownloadThis:hover ~ .showHint:after {
content: '*Hint: Download';
}
查看我的demo