Naming convention for assets (images, css, js)?

前端 未结 9 597
孤城傲影
孤城傲影 2021-01-29 20:33

I am still struggling to find a good naming convention for assets like images, js and css files used in my web projects.

So, my current would be:

CSS:

9条回答
  •  遥遥无期
    2021-01-29 20:57

    I tend to avoid anything generic, such as what smdrager suggested. "mysite.main.css" doesn't mean anything at all.

    What is "mysite"?? This one I'm working on? If so then obvious really, but it already has me thinking what it might be and if it is this obvious!

    What is "Main"? The word "Main" has no definition outside the coders knowledge of what is within that css file.

    While ok in certain scenarios, avoid names like "top" or "left" too: "top-nav.css" or "top-main-logo.png".
    You might end up wanting to use the same thing elsewhere, and putting an image in a footer or within the main page content called "top-banner.png" is very confusing!

    I don't see any issue with having a good number of stylesheets to allow for a decent naming convention to portray what css is within the given file.
    How many depends entirely on the size of the site and what it's function(s) are, and how many different blocks are on the site.

    I don't think you need to state "CSS" or "STYLE" in the css filenames at all, as the fact it's in "css" or "styles" folder and has an extension of .css and mainly as these files are only ever called in the area, I know pretty clearly what they are.

    That said, I do this with library, JS and config (etc) files. eg libSomeLibrary.php, or JSSomeScript.php. As PHP and JS files are included or used in various areas within other files, and having info of what the file's main purpose is within the name is useful.

    eg: Seeing the filename require('libContactFormValidation.php'); is useful. I know it's a library file (lib) and from the name what it does.

    For image folders, I usually have images/content-images/ and images/style-images/. I don't think there needs to be any further separation, but again it depends on the project.

    Then each image will be named accordingly to what it is, and again I don't think there's any need for defining the file is an image within the file name. Sizes can be useful, especially for when images have different sizes.

    site-logo-150x150.png
    site-logo-35x35.png
    shop-checkout-button-40x40.png
    shop-remove-item-20x20.png
    etc


    A good rule to follow is: if a new developer came to the files, would they sit scratching their head for hours, or would they likely understand what things do and only need a little time researching (which is unavoidable)?

    As anything like this, however, one of the most important rules to follow is simply constancy!
    Make sure you follow the same logic and patterns thoughout all your naming conventions!
    From simple css file names, to PHP library files to database table and column names.

提交回复
热议问题