Is using HTML5 tags as CSS3 switches a good practice?

Asked

Viewed 417 times

4

Use the names of tags HTML5 as most CSS selectors is a good practice? Be it because I’m not creative to give names to ids or classes, or by wanting to make HTML code as clean as possible, leaving only the structure and semantics of tags.

Since my apprenticeship, I have always used and abused tags because of its semantics, for example in the place of a <div id="cabecalho"> use only one <header> and things like that (<footer>, <main>, <section>, <aside> and so on...).

In some forums and posts on name sites (Caelum), I read something that implied that practicing this makes your CSS less versatile and with great chances of its early maintenance, as it is totally stuck to the HTML structure.

I always have to use classes and ids, always choose to tags where possible or if there is a middle ground?

  • 3

    ""name sites" in this case do not know if it is a good thing, mainly because it seems to me that your reasoning is being sensible, and the site at least doubtful (now, it may be that they have said something else and have been misunderstood, just reading the same original). Obviously if you have one <header> it is unnecessary to create a #cabecalho no reason. Go for the simplest, shortest, and most objective. Only complicate if you need to. Almost always you will be able to get the site in order with few classes and Ids. Usually who fills with random identification on everything that is tag does not dominate the subject.

  • ah yes, here is the link (http://blog.caelum.com.br/seu-codigo-css-pode-ser-mais-limpo-flexivel-e-reusable/) of a read see what you think, but this is the idea, be the simplest, so I was wise.

  • 2

    I’ve wondered the first phrase "Good programmers learn good code practices since they were little children". I’m not going to say good or bad about the site here, just for ethical reasons, but I think people get a lot of cake recipes when they should explain the real reasons for doing it that way or that. And most important: there is no solution that fits all cases, so the important thing is to understand (as you did, asking to know more) than to buy ready-made ideas from others that we do not know for sure who they are.

2 answers

6


Objectively it does not have to use. The use is only necessary when it has a technical reason, has a purpose in why it there. To better understand have some readings:

The id is when you need a specific stylization of that element. And the class is for any group of elements, often it would be everyone who uses a certain tag, there is no need to use.

So I go in the middle, don’t use until you need to use. There’s no way to style just one element by tag, unless you’re sure you’ll never have another element with the same tag, there is risk. It is very common that the style applies to all elements of that tag, at least within that area of the document. Example:

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

In CSS you only need:

#produtos h3 { ... }

And there’s no confusion with others h3 of the document outside this div.

I put in the Github for future reference.

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

The only case I could possibly use id or class where it can be resolved with the tag is when you’re going to do something for third parties to style as they please. Still, I’d think long and hard before doing it and try to avoid it. You can still let people style it by tags, but it will not always be so practical.

I do not like the source who consulted this, I think there is much wrong there or with opinions complicate too much the development.

There is an important principle which is YAGNI. Don’t do anything until you need to. Just make sure it’s easy to change if you need to.

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

  • Dude, I really liked your answer, and I get it, I’m not gonna take much more of this stuff that these sources are gonna be talking about from now on. I also think like you, only use when necessary !

0

No. It’s not good practice. It’s a mistake. Do not style by tags.

You may for example need 2 in the same document, then stylize:

header {

}

You’ll have to "fight" and overwrite unnecessary things. Always style by class or id, but never by tag name.

When you style by the name of the tag vc is doing a "global css reset", in which all the tags of that name will have that style. And you can yes, have more than one , , on the same page, and each have a different positioning and style than the other.

  • 3

    If you have these problems, you shouldn’t even be messing with CSS in the first place. Knowing how to solve ambiguities and using selectors is a basic need for anyone who gets into styling, and identifications should only be used when they really make semantic sense. Look at the other answer. Of course, on homemade websites, spending 400 extra bytes for no reason with redundant style makes no difference, but in any more serious application the use only in the right context prevails.

  • Honestly, I wouldn’t repeat certain tags in my code, and for these I asked, I know this, that if I just play tag repeats and such, but the question I put is, how long are ids and classes really needed, and I think I’ve figured out what the middle ground is in this

  • In case I have <span class="boxTitulo"><h2>Titulo</h2></span> for example, in my css I would style: span.boxTitulo h2{ font-size: 18px; } and would not interfere with any other <h2> on the page. This would be to avoid putting out id and class at all!

  • I suggest reading CSS architectures, such as SMACSS https://smacss.com/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.