views:

199

answers:

6

See section /* Common Classes */ of this page.

http://webdesign.about.com/od/css/a/master_stylesht_2.htm

are these css classes good, to use in any project? in terms of semantic?

/* Common Classes */

.clear { clear: both; }

.floatLeft { float: left; }

.floatRight { float: right; }

.textLeft { text-align: left; }

.textRight { text-align: right; }

.textCenter { text-align: center; }

.textJustify { text-align: justify; }

.blockCenter { display: block; margin-left: auto; margin-right: auto; } /* remember to set width */

.bold { font-weight: bold; }

.italic { font-style: italic; }

.underline { text-decoration: underline; }

.noindent { margin-left: 0; padding-left: 0; }

.nomargin { margin: 0; }

.nopadding { padding: 0; }

.nobullet { list-style: none; list-style-image: none; }
+1  A: 

One point that I would like to suggest is, when you are extending these just make sure that you just use verbs instead of using any adjectives as names for the classes and you should be good!

Edit:

I agree with others point of class names representing what it is used for, not exactly what it does.

Mahesh Velaga
+10  A: 

No, not really.

Preferrably a class name should describe what you use it for, not exactly what it does.

If you for example name a class "bluebold" and then decide that you want the text to be red and italic, you either have to create a new class and change it everywhere it's used, or you end up with a class name that no longer fits.

Guffa
+1 semantics is about meaning, not representation, although I am always skeptical about using this semantic meaning for other purposes, like in class-based microformats.
Stefano Borini
but normally we use these classes for multiple purpose. and in class groups like. `<p class="bluehold noindent>` then?
metal-gear-solid
It would make more sense to call those classes "MainFrameHeader" or "MainFrameHeaderSubTitle" or something that describes what it is, rather than what it should look like. This answer is spot on the mark, as soon as you change something you're in trouble.
SLC
@SLC but in my example `.noindent` will be always `{ margin-left: 0; padding-left: 0; }`. no need to change
metal-gear-solid
… and then you end up writing class="noindent" on half the start tags in the document. Use class names that describe groups of elements, then write CSS to match it. If you're adding a description of presentation to the HTML then you're doing it wrong.
David Dorward
@metal-gear-solid: If you use CSS like that, you could just skip the style sheet and write inline style attributes. You are missing the whole point of using classes. Check out http://www.csszengarden.com/ for an example of how different style sheets can make a page look completely different without changing the html code at all.
Guffa
@Guffa - I agree with your point. but in http://www.csszengarden.com/ content is always fixed and we are free to make any style/design. but this is not a case with Clients
metal-gear-solid
@metal-gear-solid: Still, the principle is the same. The class names in the html code should describe what the content is, not how it should look. Then you get the true separation of content and layout that is most of the point of using a style sheet.
Guffa
@metal It's likely you will want a combination of styles, such as a header no indent and bold, a subheader no intent and italic, etc. and if you use that method you will end up with every element in your html having tons of different CSS attributes, which is essentially the same as doing it inline (as Guffa pointed out). The idea is (usually) to have one CSS tag for each element in your HTML so you can style and customise it with relative ease.
SLC
@SLC - but almost every popular CSS frameworks use multiple classes method. `<div class="span-6 prepend-3 last">` http://www.blueprintcss.org/tests/parts/grid.html
metal-gear-solid
@metal-gear-solid: Yes, elements can very well use several classes, but you don't want a class for every single style so that you end up having to writing stuff like `<div class="clear floatLeft textLeft width200 height100 marginleft10 marginbottom20 bold fontSize14 paddingLeft20 paddingRight40 backgroundColorVeryLightGray colorDarkRed">`.
Guffa
@Guffa - you are right but my aim was not like this
metal-gear-solid
A: 

The name should convey the purpose and how you will use it.

Hanseh
there is no issue. the question is not about issues, is about good practices.
takpar
+7  A: 

No. They are not good choices. The whole point of css and in particular about the concept of class is to describe "what" something represents, not "how" it should appear. What something means (i.e. its semantics) and how it appears (i.e. its presentation) are two separated concepts. The fact that something is, say, a menu does not change if you decide to show it blue on light blue with one stylesheet and high contrast black on white on another stylesheet made for colorblind people.

If you give class a presentation meaning, changing how a document appears would require changes in the web page html, defeating the whole point of having CSS as a technology specifically designed to provide and encapsulate presentation. To prevent this, the alternative would be to end up having classes whose names do not represent reasonable information (e.g. class called "bluefont" which actually contains a color:red directive). Hence, having "bluefont" as a name is totally arbitrary, and here becomes desynchronized with the actual content. It could have been a random string "abgewdgbcv", but then it's better to choose something that is unrelated to presentation and conveys meaning: its associated semantics.

And we close the circle: it's the whole point of classes. See also this document at W3.

Stefano Borini
+1 for the reference
takpar
but `.bold { font-weight: bold; }` will be same always. because we will use same classes in every project and will not change in these properties.
metal-gear-solid
the point is not if you will have `.bold { font-weight: bold; }` going to become `.bold { font-decoration: italic; }`. The point is that if you want something to change from bold to italic, you will have to hunt it down in the html, but presentation is a matter of the stylesheet. If you have to touch the html to deal with presentation, it means that you are doing it wrong.
Stefano Borini
@Stefano - Ok i got your point. but whenever client need any change in design or in content then we will have to edit something in HTML. and in CMS i can change class name at one place and that will be changed in whole site.
metal-gear-solid
Do you drive stick ? the fact that you can put a gear in without pressing the clutch thanks to the synchronizer does not mean that you should not press the clutch. If the CMS is helping you doing something, so better for you, but you are still misusing the concept. Eventually, your gearbox will break down.
Stefano Borini
@Stefano Borini - OK. After read ur answer and comments . i'm thinkiong i should not use these type of readymade classes. i should only make classes when i need to make. and those classes names should based on describe "what" something represents, not "how" it should appear. so for bold text `.important` would be good
metal-gear-solid
For bold text, `important` is generally a pretty poor choice. The CSS spec warns against this: """ Note. CSS gives so much power to the "class" attribute, that authors could conceivably design their own "document language" based on elements with almost no associated presentation (such as DIV and SPAN in HTML) and assigning style information through the "class" attribute. Authors should avoid this practice since the structural elements of a document language often have recognized and accepted meanings and author-defined classes may not. """. You should probably use a `<strong>` element instead.
David Dorward
@David : very important point, thank you. There are semantically meaningful tags in html, and you should use them when appropriate.
Stefano Borini
@david - I was talking about when we use bold text just for presentation. `<strong>` is related to screen reader.and it has speial meaning than `<b>` and i'm talking about .important as a replacement of `<b>`
metal-gear-solid
@stefano - when i give image inside <p> to float right side i always use `class="floatRight"` is this class name not semantic if no then what would be the appropriate name
metal-gear-solid
@metal-gear-solid — `<strong>` is not related to screen readers, it is just another bit of markup, one which, unlike `<b>`, has semantic meaning. If you want to make something bold because it is `.important` then you almost certainly want to emphasize it. It is *very*, **very** rare that anything is done *just for presentation* (I grew out of my "Wouldn't it be great if every other letter in the entire poster was a different colour!" phase a very long time ago).
David Dorward
@metal-gear-solid : it depends. You can use something like "supporting" for floating images, and "central" for critical, central images that require a text break and then the image. Of course, you have to be practical, and floatRight can be ok, but it should be the exception, not the rule. Remember you can use two or more classes at the same time in the same tag, combining their effect.
Stefano Borini
A: 

some recomendations:

  1. .floatLeft --> .float-left: no camel cased.

  2. .bold --> .important name should tell the goal no showing how to do it

  3. .nobullet --> ul.nobullet is better to be most specified to avoid conflict with other css.

takpar
A: 

Common CSS classes are way too granular and promote classitis problem. Pseudo selectors can mitigate the problem to some extent. Assuming a new website is being designed I would do the following:

*{
margin:0;
padding:0
}

li {
list-style: none; 
list-style-image: none;
}

The rest are difficult to address, floatLeft and floatRight are to be defined by the layout,

<div id="main">
<div class="searchPanel">
</div>
<div class="resultsPanel">
</div>
</div>

The CSS ideally should look like ( layout driven) #main searchPanel { float: left; } #main resultsPanel { float: right; }

Hope you get the idea. I however, face problems with bold/underlined text. Underlined text is indicative of ugly design. Users tend to confuse such with hyper-links

questzen