Technology Web Development

How can I add a `<style>` tag in the `<body>` tag? What is the right way to add a style tag in the body tag? I want to give CSS to a particular post because I don't want to put a lot of CSS in the `<head>` tag of the template. If not, please tell me another way?

2 Answers
2 answers

How can I add a `<style>` tag in the `<body>` tag? What is the right way to add a style tag in the body tag? I want to give CSS to a particular post because I don't want to put a lot of CSS in the `<head>` tag of the template. If not, please tell me another way?

1
If you want to apply css to particular page, then you can add style tag in the html of that page. Style tag works anywhere, so you can add it anywhere you want.

Wrote answer · 8/22/2022
Karma · 1435
0

While it's technically possible to include <style> tags within the <body> of an HTML document, it's strongly discouraged and is not considered best practice. The <style> tag is designed to reside within the <head> section of your HTML document.

Here's why you should avoid placing <style> tags in the <body>:

  • Browser Rendering Behavior: Browsers are optimized to load and process CSS from the <head>. Placing styles in the <body> can lead to rendering delays or reflows as the browser encounters and applies the styles mid-page. This causes a poor user experience as elements may shift around as the page loads.
  • Maintainability: Scattering <style> tags throughout the <body> makes your CSS harder to find, manage, and update. Centralized CSS in the <head> (or in external stylesheets) promotes better organization.
  • Validation Issues: While modern browsers are forgiving, placing <style> tags in the <body> can lead to HTML validation errors.

Better Alternatives:

  1. External Stylesheets: The most common and recommended approach is to link to external CSS files in the <head>.
    Example:
    <head> <link rel="stylesheet" href="styles.css"> </head>
    This approach keeps your HTML clean and allows for easy reuse of styles across multiple pages.
  2. Internal Styles in <head>: If you have a small amount of CSS specific to a single page, you can include it within a <style> tag in the <head>. This is suitable for page-specific styling that doesn't warrant a separate file.
    Example:
    <head> <style> p { color: blue; } </style> </head>
  3. Inline Styles (Use Sparingly): You can apply styles directly to HTML elements using the style attribute. However, this should be used sparingly as it can clutter your HTML and make it harder to maintain styles consistently.
    Example:
    <p >This is a green paragraph.</p>
  4. Specific Post Styling: To style a particular post without adding a lot of CSS to the head, consider these options:
    • Unique Class or ID: Assign a unique class or ID to the post's container element. Then, define CSS rules for that class or ID in your stylesheet (linked in the <head>).
      HTML: <div class="unique-post-123">...post content...</div>
      CSS: .unique-post-123 { background-color: #f0f0f0; padding: 10px; }
    • Conditional CSS Loading: If your backend allows it (e.g., in a CMS like WordPress), you could conditionally load a CSS file specific to that post. This is more complex but can be useful for very large, post-specific style sets.

In summary, avoid using <style> tags in the <body>. Use external stylesheets, internal styles in the <head>, or targeted CSS rules with unique classes/IDs to style your content effectively and maintainably.

Wrote answer · 3/14/2025
Karma · 40

Related Questions

What's a leading front-end development framework?
Write short notes on CSS and XSL.
How do I fix the error "Argument of type 'MonoTypeOperatorFunction<RouterEvent>' is not assignable to parameter of type 'OperatorFunction<Event_2, RouterEvent>'" in Angular?
Is it possible to change the HTML tag of the string in the second column, 'td', to another HTML tag like 'span'?
How to convert आई into %E0%A4%86%E0%A4%88 and how to convert %E0%A4%86%E0%A4%88 into आई? Is there a tool for this? I want to add this for my website post URL like Wikipedia. Are there any disadvantages to doing this?
Can I add CSS in Google Tag Manager (GTM)?
What is the best free, secure, and trusted website to host CSS or JS files as an external stylesheet for implementation on a Blogger website or any website?