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 CSS Foundations Getting Started with CSS Adding CSS to a Page

Sachin Bhargava
Sachin Bhargava
1,467 Points

Why is it important to add @import 'css/more-styles.css'; at the top of the page for proper functionality?

Why is it important to add @import 'css/more-styles.css'; at the top of the stylesheet, before all the coding in that stylesheet? What happens if I add it at the end of the stylesheet?

2 Answers

Elijah Gartin
Elijah Gartin
13,182 Points

With CSS, it applies the rules from start to finish. The rule that gets applied last is the one that will apply last.

Importing is similar to just doing another css file call in your header so it's important you're keeping track of the order of your stylesheets. You also have to keep the css specificity rules in mind.

The difference between importing at the top and importing at the bottom will simply be a matter of who overrides who. For example, if you are using some sort of theme or css framework that has most of styles already preset, but you want to change the color of the header, you would first import the framework's css file, and then add your own custom css underneath that since css reads from right to left, top to bottom.

Let's say you love everything about the framework you're using except you want to change the color of your header element. Here's an example of how you would use the method of overriding while ensuring that you import all the other styles you have no problem with.

/*Filename: more-styles.css*/

h1{
color: black;
}
.... etc

Now you have your custom styles file that you can override that color while maintaining the styles you already "imported"

/*  Filename: custom-styles.css */

@import 'css/more-styles.css';

/*----Custom Styles Below this Line----*/
h1 {
color: green;
}

Let me know if you have any further questions or need clarification.

Thanks and good luck!

If you put it in a different part of the css file, it will just not work. Even a comment line before the @import will break things.

Hi Sachin,

Guil explains this at 11:45 in the video if you want to review that.