Alex Rivera | Logout

div.classname in css file

Asked 2011-08-23T19:33:33.357
17

I have seen some developers write

HTML:

<div class="test"> some content </div>

CSS:

div.test {
  width:100px.
}

What is the purpose of doing div.className instead of just .className.

Does this mean this style will be applied only when the class is applied to a div.

So, <span class='test'>content</span> will have no effect of 'test' with the css above? If that is the case, is that best practice? This is almost like style overriding for different type of elements, mixing styles with rules!

Edit
Report

1 Answer

1

Prefixing a class with an element name confines that set of styles to the specified element type.

.test { font-weight:bold; }
span.test { color:#fff; }
div.test { color:#000; }

In this case, all elements using the class will be bold, the font for SPANS will be white, and the font for DIVS will be black.

answered 2011-08-23T19:36:02.840

Your Answer