gpt4 book ai didi

CSS 优先级搞砸了

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:58 25 4
gpt4 key购买 nike

为了正确设置 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com