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

linking a css document using css, or using :hover to change multiple selectors

is it possible to use a css document to link to another css document? this is a way that I'm trying to solve this problem where I want to use

link1:hover {}

to change

link2{}

4 Answers

Kevin Kenger
Kevin Kenger
32,834 Points

Hey Andrew,

To link multiple style sheets together you can use the @import directive.

Here's an example:

@import url(form.css)

For changing an element when hovering over another element, you can put the element with the :hover pseudo-class first, and then the element you want to affect after it, like this:

#link1:hover #link2 {
    color: red;
}

so with

link1:hover #link2 {

color: red;

} would color:red; apply to #link1, #link2, or both? and is it possible to import form.css like

link1:hover{

@import url(form.css)

} ? thank you for your help.

so with

link1:hover #link2 {

color: red;

} would color:red; apply to #link1, #link2, or both? and is it possible to import form.css like

link1:hover{

@import url(form.css)

} ? thank you for your help.

Kevin Kenger
Kevin Kenger
32,834 Points

The color of #link2 would be modified using that method.

If you want to modify multiple selectors, I'm not sure that you can do that with one rule. You might have to do something like this:

#link1:hover {
    color: red;
}

#link1:hover #link2 {
    color: red;
}

That's not very DRY code though, and if there is a better way that someone knows of, I hope they share it with us! I would love to know that as well.

Now as far as importing a style sheet using a pseudo-class, that won't work. What exactly are you trying to do? It sounds like Javascript might be the best solution for whatever it is you're trying to achieve, but if I knew a little bit more of what you wanted to do exactly, I might be able to give some suggestions.

Cameron Cottle
Cameron Cottle
13,742 Points

One way to make your CSS a little more efficient would probably be:

#link1:hover{
    color: red;
}
#link2{
   color: inherit
}

Since imports must be done at the start of the document you cannot add this inside of a declaration. If you are trying to compartmentalize your code I would recommend taking a look at the Sass courses.

Thanks, I was just hoping to have a single image that expands into a whole grid of images when it is hovered using CSS, because I'm not confident in JS yet and I'm trying to build fast. It looks like that's not going to happen though :). Thanks.