Should css class names like 'floatleft' that directly describe the attached style be avoided?

后端 未结 20 2158
谎友^
谎友^ 2020-12-13 08:32

Lots of websites use class names like floatleft, clearfloat, alignright, small, center etc that describe the

20条回答
  •  旧时难觅i
    2020-12-13 09:14

    I think this is where old meets new in web technologies. From times past, it has been difficult to unobtrusively render an outstanding web experience. These class names mostly came in handy when websites were changing webmasters to aid them in understanding the code. It served its cause well but with the new technologies of this day and age, I think this is slowly dying out - infact, it should be dead.

    The question we should ask is, "Do we need to create a new class for every new innovative design that could pass as a template?". I do not think so. The markup on a site should do what it is meant for - markup. The class names used in the markup should be descriptive of the content and not its looks. The stylesheets - on the other hand - should be able to select elements on a document based on the information in the markup, and style them.

    I want to relate this to the Rails association naming convention. Consider this...

    class Post < ActiveRecord::Base
        has_one  :personifyable
        has_many :personifications, :through => :personifyable
        has_many :taggables
        has_many :tags, :through => taggables
        belongs_to :destroyers
    end
    

    Obviously, this is not a real model; it is something I am using to drive a point. Consider the case of a deeply nested dependency. These names will grow ridiculous - if they aren't already (i.e. in CSS,

    or something of the like)

    Now consider the case where you have different principles. Different developers and designers may - if not 'most definitely will' - have different reasons for using a specific naming convention. How would this affect refactoring time. I will leave that to your imagination. Also, if your business partner notices a new trend with sites themes that attracts traffic - more technically, assume this business partner has performed some experimental A/B testing and come up with some specs - you don't want to change the contents of the whole stack (ie HTML and CSS and possibly JS pages) to implement this new style.

    In conclusion, keep styling hints out of your markup. Unobtrusively interact with the document to style and manipulate it. Sass gives you a fine way of styling a site while having your CSS mock your markup. jQuery is another awesome UJS library. HTML5 gives you methods too that make the markup more flexible and yields more information to CSS and JS.

提交回复
热议问题