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
Answer link
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.
0
Answer link
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:
-
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. -
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>
-
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>
-
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.
-
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
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.