Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS (Stylesheet) organization and colors

Tags:

css

I just finished a medium sized web site and one thing I noticed about my css organization was that I have a lot of hard coded colour values throughout. This obviously isn't great for maintainability. Generally, when I design a site I pick 3-5 main colours for a theme. I end up setting some default values for paragraphs, links, etc... at the beginning of my main css, but some components will change the colour (like the legend tag for example) and require me to restyle with the colour I wanted. How do you avoid this? I was thinking of creating separate rules for each colour and just use those when I need to restyle.

i.e.

.color1 {
    color: #3d444d;
}
like image 743
neuroguy123 Avatar asked Mar 02 '23 07:03

neuroguy123


2 Answers

One thing I've done here is break out my palette declarations from other style/layout markup, grouping commonly-colored items in lists, e.g.

h1 { 
 padding...
 margin...
 font-family...
}

p {
 ...
}

code {
 ...
}

/* time passes */

/* these elements are semantically grouped by color in the design */
h1, p, code { 
 color: #ff0000;
}

On preview, JeeBee's suggestion is a logical extension of this: if it makes sense to handle your color declarations (and, of course, this can apply to other style issues, though color has the unique properties of not changing layout), you might consider pushing it out to a separate css file, yeah. This makes it easier to hot-swap color-only thematic variations, too, by just targeting one or another colorxxx.css profile as your include.

like image 59
Josh Millard Avatar answered May 09 '23 01:05

Josh Millard


That's exactly what you should do.

The more centralized you can make your css, the easier it will be to make changes in the future. And let's be serious, you will want to change colors in the future.

You should almost never hard-code any css into your html, it should all be in the css.

Also, something I have started doing more often is to layer your css classes on eachother to make it even easier to change colors once... represent everywhere.

Sample (random color) css:

.main_text {color:#444444;}
.secondary_text{color:#765123;}
.main_color {background:#343434;}
.secondary_color {background:#765sda;}

Then some markup, notice how I am using the colors layer with otehr classes, that way I can just change ONE css class:

<body class='main_text'>
  <div class='main_color secondary_text'>
    <span class='secondary color main_text'>bla bla bla</span>
  </div>
  <div class='main_color secondary_text>
    You get the idea...
  </div>
</body>

Remember... inline css = bad (most of the time)

like image 29
naspinski Avatar answered May 09 '23 01:05

naspinski