CSS right in the HTML tag

Asked

Viewed 428 times

0

I have a question today regarding CSS

Sometimes it is more feasible to put CSS code directly in the HTML tag and I wonder if this is a bad practice

How do I proceed with this doubt? It’s really bad? I do it on time and then I move to an external sheet?

Thanks

  • 2

    It depends on the context. If it is a CSS that will only be used on that page, there is no problem. It saves a request including. If it goes to multiple pages, separately you have the benefit of caching, and reuse. In specific situations you can put the CSS in the element itself as well. The important thing is to know the pros and cons. This is the kind of thing that when one does only one way, or does not understand the subject (the follower of good practices), or does not actually work with it to the point of having only done very elementary things. There’s no way to cut through, you have to understand.

  • I didn’t quite understand if the question is about CSS in tags <style>, on the attribute style, or both.

  • It refers to style attributes

3 answers

2

I’d say the ideal is to avoid it when possible. Such "good practice" is like other tools: we do not use the hammer to cut or the pliers to hit nail. For this we have the style sheet, keeping the organization.

But as you said, the inline, depending on the case, can be useful and even inevitable. It is worth remembering that it takes precedence over leaf styles (except the !Mportant). It may be the last resort, even if not ideal, for a problematic design, and then I use it without mercy.

2

This is relative to the amount of code you will insert, if you have a CSS that takes care of the full page design it is best to leave it whole in a minified file and upload it to your pages. But suppose on a specific page you don’t want to use the whole file, and whether just some class or some specific CSS ID, for this case it is better for the performance of your site you insert directly into the HTML these respective classes and Ids.

  • I agree with you, I am trying to acquire the maximum of good practices in all the codes I will do. There is some explanatory website to indicate?

  • I found this information when I was listening to this podcast

1

It depends, if the style you will assign can be reused in some way by another component, ideally you separate into another file, however if you intentionally want that style to be used only for that specific component or only for a specific page, in this case the ideal is to put the style only on the page or even directly on the component.

Browser other questions tagged

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