How style tag is working inside body tag?
Because style
contains style information for a document, or part of a document. so no matter where you have the h1
(in this case), although you shouldn't have inside head
because it is invalid HTML , it will be styled. check more info here
The
<style>
element can be included inside the<head>
or<body>
of the
document, and the styles will still be applied, however it is
recommended that you include your styles in the<head>
for
organizational purposes — it is a lot better to separate your content
from your presentation as much as possible. Even better, put your
styles in external stylesheets and apply them using<link>
elements.
How can I insert CSS styles inside body independent of specific elements?
Under the premise, you don't care about invalid HTML in relation of <style>
inside <body>
(which is not that uncommon), you can assign unique identifier i.e.:
<style>
.my-class-1 {
color: gold;
}
</style>
<div class="my-class-1">Fragment Content</div>
<style>
.my-class-2 {
color: tomato;
}
</style>
<div class="my-class-2">Fragment Content</div>
<div class="my-fragment-1">
<style>
.my-fragment-1 .my-class {
color: teal;
}
</style>
<div class="my-class">Fragment Content</div>
</div>
<div class="my-fragment-2">
<style>
.my-fragment-2 .my-class {
color: hotpink;
}
</style>
<div class="my-class">Fragment Content</div>
</div>
<style id="my-style-1">
#my-style-1 + div {
color: orangered;
}
</style>
<div>Fragment Content</div>
<style id="my-style-2">
#my-style-2 + div {
color: indigo;
}
</style>
<div>Fragment Content</div>
style tag within body and W3 Standards
From the front page of the document you linked to:
This document has been discontinued and is only made available for historical purposes. For an up to date reference on HTML elements (and more) please consult Web Platform Docs.
Some draft versions of HTML 5 allowed style elements in the body under certain conditions.
There were various problems with the design of that functionality and it did not make it into the final version of HTML 5.
Is safe to put html style tag into body tags coding html emails?
It seems client dependent. Check out a similar answer here.
Some clients will support the use of styling in the body, but generally not in the head. Since there is really no way to incorporate css, it makes sense that this would be the case.
HTML 5 body tag to change color
I suggest you do some research about basic css.
You can achieve that with this.
<html>
<head>
<style>
body {
background: red;
}
</style>
<title>Body bgcolor Attribute example</title>
</head>
<body>
<h1>This webpage has colored background.</h1>
</body>
</html>
Related Topics
How to Vertically Align Text Inside a Flexbox
Scraping HTML Tables into R Data Frames Using the Xml Package
Html Encoding Issues - "Â" Character Showing Up Instead of "&Nbsp;"
Why Does Styling the Background of the Body Element Affect the Entire Screen
Does Form Data Still Transfer If the Input Tag Has No Name
Display a Div Width 100% With Margins
Remove White Space Above and Below Large Text in an Inline-Block Element
Using Flex Order Property to Re-Arrange Items For Desktop and Mobile Views
How to Show ≪Div≫ Tag Literally in ≪Code≫/≪Pre≫ Tag
Why Does Ie9 Switch to Compatibility Mode on My Website
Difference Between Id and Class in Css, and When Should I Use Them
Alternate Table Row Color Using Css
Html5 Canvas Drawimage: How to Apply Antialiasing
How to Get This CSS Text-Decoration Override to Work
Onclick on Option Tag Not Working on Ie and Chrome
How to Use Content-Disposition For Force a File to Download to the Hard Drive