Developers Should Consider the ID and Class Pros and Cons

Developers Should Consider the ID and Class Pros and Cons

When a developer is just getting started, they will have a lot of questions. A new coder recently asked about using IDs versus classes. The seasoned developers who answered had a range of opinions and tips on the matter. One coder noted that classes apply to styling while IDs apply to anchor links and JavaScript coding. Another noted that classes provide a low level of specificity for coding in CSS. Some developers try both as a test just to see what happens.
In many cases, asking a question simply generates more questions. One person asked the original developer about whether or not it is a better practice to use custom attributes for JavaScript binding. This developer noted that an ID limits the developer to one element per page. With classes, it is possible to target a set of elements. It is also important to remember that custom attributes could be an invalid HTML code.·
There are some cons to using both IDs and classes. One developer explained that classes cause a slowdown in the compilation of code. CSS styling is especially affected by slowdowns related to class distinctions. DOM fetching is another action that is slowed by the use of class attributes. Browsers may ignore valid attributes, and browser updates can change what is displayed. What is valid one day on one browser might be invalidated on the next day. Most browsers can ignore an HTML problem, but some will cause an issue in the site's display.
Some developers suggested entirely different options. Bootstrap was one suggestion. It works with fetching multiple DOMs. It is also important to consider search engine algorithms. Some of them deliver a penalty for coding in certain ways. A few developers use both, picking and choosing when to use each one. IDs are often used for things on a page that are unique. Classes can be used for attributes that are repeated. Some developers neither use class nor ID attributes. They instead use dynamic PHP or a block element modifier for CSS when there is already a basic HTML structure in place on the site. For more information click here https://www.reddit.com/r/web_design/comments/9tihws/do_you_guys_use_ids_anymore_or_just_use_classes/.

Web Development