开发者

What is the benefit of making a class that just floats in either direction?

开发者 https://www.devze.com 2023-03-30 03:17 出处:网络
I see this being done on websites (ex: http://www.abookapart.com/): .left { float: left; } <div class=\"col two left\" />

I see this being done on websites (ex: http://www.abookapart.com/):

.left {
  float: left;
}

<div class="col two left" />

Is the above good practice? I thought it was generally frowned upon to create classes that describe style. If I wanted the above div to float right at a later date I would have to go into the markup and change left to right. No better then adding a style attri开发者_如何学编程bute to the element it would seem.


You're right. Most developers avoid class names like "left" or "blue" for that exact reason. Other than the class name, I don't see anything wrong. I would include that float in the "col" class and then just override it with "right" later on if need be.


I've seen it become useful where multiple people are editors for a single website, and you don't give them all full html access.

By setting up classes of styles that they're allowed to use, you can allow them a certain amount of style control without giving them the power to mess everything up.


It's really down to personal/team choice.

I've always preferred this technique as I favor convention over style, I like my concerns separated and I dislike ambiguity.

if I see a div like this:

<div class="clearfix left bordered">
...
</div>

I like the fact that its intent is clearly emphasized and won't make me have to dive into the CSS file to find out what it means.

But based on your example above, if you did go down the more declarative route:

<div class="importantTextDiv">
...
</div>

On the one hand, you have to investigate the CSS file to determine its properties (or run it and inspect the element in your Firebug equivalent) but should you choose to change it, you don't need to make a source change, or change whats emitted from your server side language.

To me, both situations are a pain, but in my experience, changing a div between float:left and float:right is the kind of thing you'd have got finalised before a final deployment anyway.

My answer ? there's no technical reason to prefer one path over another, choose which one feels more comfortable!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号