Using HTML5 tags as CSS3 selectors is a good practice?

3

Using HTML5 tags names like most CSS selectors is a good practice? Whether it's because I'm not creative to give names to ids or classes, or because I want to leave the HTML code as clean as possible, leaving only the structure and semantics of tags .

Since my learning, I have always used and abused tags because of their semantics, eg instead of a <div id="cabecalho"> use only <header> and things like% and so it goes ...).

On some forums and posts on name sites (Caelum), I have read something that implied that practicing this leaves your CSS less versatile and with a high chance of its maintenance being precocious, stuck to the HTML framework.

I have to always use classes and <footer>, <main>, <section>, <aside> , always opt for tags when possible or if there is a middle ground?

    
asked by anonymous 08.12.2016 / 16:38

2 answers

5

Objectively you do not have to use it. The use is only necessary when he has a technical motive, has a purpose in it for him. To understand better you have some readings:

id is when you need a specific stylization of that element. And the class is for any group of elements, it would often be all that use a given tag , there you do not have to use it.

So I go in the middle, do not use until I need to use. You can not just style an element with tag , unless you're sure you'll never have another element with the same tag , there's risk. It is very common for the style to apply to all elements of that tag , less within that area of the document. Example:

<div id="produtos">
    <section>
        <h3>Brinquedos</h3>

In CSS you only need:

#produtos h3 { ... }

And there is no confusion with other h3 of the document outside this div .

Bacco has already commented on what I think, the simpler, the better.

The only case that might use id or class where it can be solved with tag is when you're going to do something for others to style as you wish. Even so, he would think long before he did and try to avoid it. You can still let people style tags , but it will not always be so practical.

I do not like the finte who consulted this, I think there is too much wrong or with opinions of too complicated development. There is one important principle that is YAGNI . Do not do something until you need it. Just make sure it's easy to change if needed.

I consider it a correct choice. There are those who disagree.

    
08.12.2016 / 17:13
1

No. This is not good practice. It is a mistake. Do not stylize by tags.

You can, for example, need 2 in the same document, so stylize it:

header {

}

You'll have to keep fighting and overriding unnecessary things. Always style by class or id, but never by tag name.

When stylized by tag name you are doing a "css reset global", where all tags in that name will have that style. And you can, have more than one, on the same page, and each one have a positioning and style different from the other.

    
08.12.2016 / 17:13