Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Conventions / Code Layout Models [closed]

Tags:

Has there been any attempt and creating a formalized method for organizing CSS code? Before I go and make up my own strategy for keeping things readable, I'm wondering what else is out there. Google hasn't been very helpful, as I'm not entirely sure what terms to search for.

I'm thinking more along the lines of indenting/spacing, when to use new lines, naming conventions, etc.

Any ideas?

like image 449
Wilco Avatar asked Nov 21 '08 19:11

Wilco


People also ask

What is CSS naming convention?

Naming rules Names are written in lowercase Latin letters. Words are separated by a hyphen ( - ). The block name defines the namespace for its elements and modifiers. The element name is separated from the block name by a double underscore ( __ ).

How to naming CSS class?

First, edit the question you'd like to affect with CSS and go to the Layout tab. Enter your class name in the CSS Class Name field and click Save Question. Your class name can be anything in plain words.

Which model of CSS is used in connection with design & layout of web pages?

What is the CSS Box Model? The CSS box model is a container that contains multiple properties including borders, margin, padding, and the content itself. It is used to create the design and layout of web pages. According to the CSS box model, the web browser supplies each element as a square prism.


2 Answers

Natalie Down of ClearLeft fame produced a really great slide show covering this topic and more http://natbat.net/2008/Sep/28/css-systems/

Download the PDF as it includes a lot more information than the slide show. I'd recommend this to CSS devs of all skill levels.

like image 56
Andy Ford Avatar answered Oct 05 '22 14:10

Andy Ford


This is all very subjective as per the usual code formatting debates and I do not know of any formalized conventions.

The method I've chosen is to use all lowercase classes and ids with underscores separating words (#page_container for example). I declare all my tag styles first (html, body, ul, etc.), then all classes and ids, sorted alphabetically. Additionally, all the styles defined in each class, id, or tag are defined alphabetically as well. Using this convention makes it easier to track down a particular style.

For formatting, I always compress it as small as possible, but still legible. I put everything on one line with appropriate white space. If you have Visual Studio, you can specify this format and have it automatically formatted this way for you (Set Style to Compact rules under Tools, Options, Text Editor, CSS, Format).

Naming conventions are extremely subjective here, but the thing to keep in mind is to name your elements as their intended purpose, not their styled meaning. For example, if you have a company slogan you want to style in a large, red font name the id #slogan instead of #red_bold.

Here's a full example to give you an idea:

body { background-color: #fff; color: #999; font-family: verdana, arial, helvetica, sans-serif; font-size: 76%; padding: 0; margin: 0; } a { color: #2c5eb4; text-decoration: none; } a:hover { text-decoration: underline; } h1, h2, h3, h4, h5, h6 { color: #f70; font-family: helvetica, verdana, arial, serif; font-weight: bold; margin: 1.2em 0; } h1 { font-size: 2.4em; line-height: 1.2em;  margin-bottom: 0em; margin-top: 0em; } h2 { font-size: 1.7em; } h3 { font-size: 1.4em; } h4 { font-size: 1.2em; font-weight: bold; } h5 { font-size: 1.0em; font-weight: bold; } h6 { font-size: 0.8em; font-weight: bold; } img { border: 0; } li, ol, ul { font-size: 1.0em; line-height: 1.8em; list-style-position: inside; margin-bottom: 0.1em; margin-left: 0; margin-top: 0.2em; } #content { clear: both; margin: 0; margin-top: -4em; } #columns { height: 36em; } #column1, #column2, #column3, #column4 { border-right: 1px solid #dbdbdb; float: left; width: 18%; margin: 0 0.5em; padding: 0 1em; height: 100%; } #column1 { width: 28%; } #column1 input { float: right; } #column1 label { color: #999; float: left; } #column2 a, #column3 a { font-weight: bold; } #column4 { border-right: 0; } #form { margin: 0 2em; } .help_button { float: right; text-align: right; width: 30px; } 
like image 40
cowgod Avatar answered Oct 05 '22 13:10

cowgod