0

For proper CSS priority setting, I searched around a bit and found nothing similar of sort. I vaguely remember someone posting about CSS priority in the comments,a long time ago, but I do not recall at all. Hence, I'm asking this question - for one part, to get my answer - and to archive this answer for future reference for others(also inclusive of myself incase this happens again).

Here's my snippet:

.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>

I want to show Hint with the help of showHint class when I hover over the desired classes with this code

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

but, for some reason, it is not working!

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



I want it to achieve an effect similar to this(but while hovering over .DownloadThis and .ViewThis . If a different generic message appears when I hover over .ContainerTitle that'd be awesome too!):

.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.

3 Answers 3

1

You need the next sibling and general sibling selector for this. Read about it 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>

Sign up to request clarification or add additional context in comments.

Comments

1

It isn't working because .showHint is not a descendant of .ViewThis. You need to use the general sibling selector (~):

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

Example:

.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>

3 Comments

This won't work for .DownloadThis, it will only work for .ViewThis.
Yes I've just realised you have two different elements. See update
Yeah I noticed, I was just a bit too quick
0

It isn't working because .showHint is NOT a child of .DownloadThis or .ViewThis.

Using the tilde (general sibling) selector, this will then select the next (and all) .showHint elements within the scope of .ContainerTitle.

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

Check out my demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.