Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Michal Broniewicz
Michal Broniewicz
6,681 Points

:target not working after adding another background.

Hello there.

In website im currently working on I want my nav to higlight divs with ID given. Everything was working perfectly fine but when I change those Div's background the target is still scrolling my website down but what was written in :target doesnt apply.

Here is what I want my target to do.

:target {
  background: #E6E6E6;
  color: black;
  margin-bottom: 20px;
}

Its working perfectly fine but background is not changing when i add background property here:

#general,
#item,
#contact {
  border-radius: 10px;
  max-width: 40%;
  margin: 2% 4%;
  float: left;
  padding-left: 2%;
  padding-bottom: 2%;
  background-color: #CF8D56;
}

How can I fix that?

1 Answer

Codin - Codesmite
Codin - Codesmite
8,600 Points

Looked into this as I was unsure myself and it is due to CSS specificity.

Defining a rule via element ID has a higher priority then a pseudo-selector such as :target. So the background-color rule in #contact element has a higher priority then the background-color rule in the :target pseudo-selector.

The following code should fix the problem (adding !important will overide any other style rules targeted at the element):

#contact {
  border-radius: 10px;
  max-width: 40%;
  margin: 2% 4%;
  float: left;
  padding-left: 2%;
  padding-bottom: 2%;  
  background-color: #CF8D56;
}

:target {
  background-color: #E6E6E6 !important;
  color: black;
  margin-bottom: 20px;
}

Codepen: http://codepen.io/anon/pen/JdjLRv

Michal Broniewicz
Michal Broniewicz
6,681 Points

Thats great tip! Thanks the !important fixed it and i guess i will use it in future.