2

At this time I build a new website with Ruby on Rails. As the site becomes bigger and bigger, my css rules are getting messy. So I thought a way to improve my css code readability and avoid mistakes:

I have one global css.scss (SASS, something like LESS) file for all the pages that resets and handles global elements such as header and footer. For every other page I have a dedicated css file. For instance, for the about page I have an about.css.scss file. In the about_page.css.scss file the code looks like:

.about_page { .othersubclass { . . } } 

while in the 'about.erb' page I have the following:

<div class="about_page"> <div class="othersubclass"> . . . </div> </div> 

Is this a good or a bad technique? Is there any known technique that faces this problem?

(btw RoR concatenates all css files into 1 file thus there is no problem with extra http requests)

1

2 Answers 2

2

As per: http://betterfrontend.com/guides/stylesheets/

It's generally recommended to have a stylesheet per a major section of the site.

You can then compile it all within application.css.scss (all the @import's in here).

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

1 Comment

Nice project (betterfrontend)! Will wander around :)
0

It's perfectly fine adding a unique CSS class/id to the root element of each page on your website (in fact many CMS do that) but it's sounds like an overkill to create a separate CSS file for each page, usually there isn't a lot of page-specific CSS.

Since you mentioned that your build process concatenates all your CSS into a single file, it doesn't matter in the production environment and is only a question of code organization. If it fits in your project architecture (you have per-page separation of other resources) then I guess it's OK, but personally it'd go with a more coarse-grained division, for example as Daniel Fischer suggested.

Just think if it's convenient to maintain many (tens? hundreds?) little css files (0 - 50 lines each?) in your case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.