I am learning HTML and CSS. Some teachers say that you should use lots of divs and classes, while others say the opposite, advising not to use too many divs and classes. Which one should I use?
examples:
<section class="section-pricing">
<div class="container">
<span class="subheading">Pricing</span>
<h2 class="heading-secondary">Eating well without breaking the bank</h2>
</div>
</section>
VS
<section class="section-pricing">
<div class="container">
<span>Pricing</span>
<h2>Eating well without breaking the bank</h2>
</div>
</section>
3
Answers
Use divs to contain things for styling and coding around. There should be a happy medium with it.
Like using a container div on every element is not clean if that container is useless. You can always use
ID's
on things instead of using lots of divs.ID's
are also good for coding around depending on what you are doing.i´m learning it too with freecodecamp and the practice projects are amazing. I can say , for my part, that it depends on your needs…not everything needs id´s or class´s… well, id´s are unique identifier, on the other hand classes can reassign as much as you want – practical for repeating html tags like divs, p and so on – if you need to…
for me is the bottom one more eye pleasant
Since this is an opinion question, I’d start by suggesting that different people will have different opinions.
I’m more of a minimalist, so I only put in things I need, which might be just a section with a <p>aragraph element to contain some text.
However, when I need to do special formatting, I start adding div to help organize the page, and (as needed) classes to help define CSS that will make the elements look nicer.
If you know you’ll have a complex page, it probably makes sense to start adding class attributes to most things since you will probably need it as you find similar things that share the same styles.