No, it is not considered a negative practice. Understanding the distinction between concepts is crucial in this context.
Typically, tags' styling serves the purpose of defining or resetting styles at a fundamental level for your website. This is commonly done to reset styles for cross-browser compatibility. Tools like Normalize.CSS are often used for this task.
When working with frameworks such as Bootstrap or Foundation, they come with their own set of styles. At this point, classes are usually utilized for better organization and reusability.
From here, you have the option to either adopt the framework's styling or create your own unique style.
It is still advisable to use classes for styling purposes. Consider the following HTML structure:
<div class="profile">
<img class="avatar-url" src="..." />
<p>John</p>
</div>
and
.profile p {
font-size: 16px;
}
While this works fine when "name" is the only element within the profile block, if you need to add "title" in the future, you may encounter issues.
<div class="profile">
<img class="avatar-url" src="..." />
<span class="title">Mr. </span><span class="name">John</span>
</div>
This change could lead to problems such as:
- Your CSS will be affected since there is no longer a
<p>
tag present.
- You would need to adjust your CSS to accommodate the new structure which could cause issues in terms of scalability and control.
- By forcing the name to use a
<p>
tag, you risk breaking your layout if properties defined for one tag need to be applied to another, especially concerning display attributes like inline or block.
In addition, using styles with classes enhances code readability. It is much clearer to read something like .list .item
compared to .list div
.
Regarding CSS specificity calculation, tags have the lowest priority. Therefore, to override a defined class like .btn
, it is necessary to use .btn
at minimum.