Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe that using CSS classes as hooks for JavaScript can indeed be a bad practice. The reason I say this is because this approach intermingles your CSS and JavaScript and can often lead to unnecessary confusion.</p> <p>Let me explain: Right now I am working on adding new templates to an existing system which uses dynamic user controlled admin settings to control CSS properties (page width, font sizes, colors, etc). These settings can also affect JS properties (slideshow options, delay between slides, etc.).</p> <p>The problem is that since so many CSS classes were used as JavaScript hooks, I am having a terribly hard time determining which classes are used for styles and which ones are used as JS hooks, and which ones are used for both! As a result, when doing a new template, it would be very difficult to start from scratch with new markup as I would be leaving out various important classes for both CSS and JS functionality.</p> <p>The task would be much easier if CSS classes were ONLY used for styling, and other JS hooks were only used for JS. I envision this being accomplished with HTML5 data attributes, which would look something like this:</p> <pre><code>&lt;a href="#" class="button" data-lightbox="true" data-slide-delay="250"&gt;my link&lt;/a&gt; </code></pre> <p>By using the HTML5 data attributes for JavaScript hooks and the class attribute for CSS styling, we can be sure that all classes are ONLY related to CSS, and anything JavaScript related simply gets its custom attribute.</p> <p>Another option I have used is prefixing any css class with <code>js-</code> that is ever referenced by the JavaScript. That way you would know which classes you could remove safely for styling and which ones had to remain for existing functionality.</p> <pre><code>&lt;a href="/mylink" class="my-style-class my-other-style-class js-my-hook-1 js-myhook-2"&gt; My link &lt;/a&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload