0

If there is a simple google search for this question, I apologise now, but I'm unsure of which keywords I'm looking to use. I'm decent with CSS but I think I'm yet to utilise the full power of the language and also completely eradicate code redundancy.

I've come across this code (any similar snippets many other times):

<p class="comment smallertext center-align">

What CSS would this be referring to? In terms of the comment, smallertext and center-align all being spaced.

Thank you in advance for any replies and guidance!

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
sark9012
  • 5,485
  • 18
  • 61
  • 99
  • This isn’t really about CSS at all: the construct is just HTML. It is then a different matter how you use class selectors in CSS in cases like this. – Jukka K. Korpela Oct 17 '13 at 12:35

4 Answers4

5

It means that element has all of the following classes: comment, smallertext and center-align. In HTML, spaces separate multiple class names in a single attribute.

In CSS, you can target this element using one or more of .comment, .smallertext or .center-align, either separately or together. Having three separate rules and a single element that has all three classes, all three rules will apply:

.comment {}
.smallertext {}
.center-align {}

You can also combine them together, if necessary, to apply styles specific to elements having all three classes:

.comment.smallertext.center-align {}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
1

The code shown in the example links to 3 different css class selectors:

.comment {
}

.smallertext {
}

.center-align {
}

So instead of making lots of non-reusable css selectors, you split them up into lots of small ones that provide 1 functionality that will most likely be used for lots of different parts of your websites. In that example you have one for comments, one for smaller text and one for center aligning text.

Adam Rivers
  • 1,065
  • 7
  • 13
0

It's a way of defining multiple classes to a single element. In this case it would match classes comment, smallertext and center-align.

walther
  • 13,466
  • 5
  • 41
  • 67
0

Try this short explanation... Multiple classes can make it easier to add special effects to elements without having to create a whole new style for that element.

DKanavikar
  • 31
  • 5