If the choice were there, my first preference would be classes / other selectors. However, there are situations where inline styles are the only way to go. In other situations, just the CSS class alone takes too much work, and other types of CSS selectors make more sense there.
Suppose you had to strip a zebra to indicate a list or table. Instead of applying a specific class to each alternate element or row, you could simply use selectors to do the job. This will keep the code simple, but it will not use CSS classes. To illustrate three ways :
Using only a class
.alternate { background-color: #CCC; } <ul> <li>first</li> <li class="alternate">second</li> <li>third</li> <li class="alternate">fourth</li> </ul>
Using classes + structural selectors
.striped :nth-child(2n) { background-color: #CCC; } <ul class="striped"> <li>first</li> <li>second</li> <li>third</li> <li>fourth</li> </ul>
Using Inline Styles
<ul> <li>first</li> <li style="background-color: #CCC">second</li> <li>third</li> <li style="background-color: #CCC">fourth</li> </ul>
The second way seems the most portable and encapsulated for me. To add or remove stripes from any element in a container, simply add or remove the striped class.
However, there are cases when inline styles not only make sense, but are the only way. When the set of possible values ββis huge, it is foolish to try to make classes in advance for each possible state. For example, a user interface that allows the user to dynamically place certain elements anywhere on the screen by dragging and dropping. The element must be set absolutely or relatively with the actual coordinates, such as:
<div style="position: absolute; top: 20px; left: 49px;">..</div>
Of course, we could use classes for every possible position that the div can take, but this is not recommended. And you can easily understand why:
.pos_20_49 { top: 20px; left: 49px; } .pos_20_50 { top: 20px; left: 50px; }
Anurag Jun 30 '10 at 2:30 2010-06-30 02:30
source share