Jaso2970
Jaso2970

Reputation: 147

Is good practice to apply a style to the html tag?

I need to put an image background for the whole page. I use to do this applying the style to the body tag.

Just wondering if ss good practice to put a style to the html tag

Upvotes: 3

Views: 3015

Answers (5)

GolezTrol
GolezTrol

Reputation: 116110

Sure. Actually, the html tag can be omitted in html5, so if you have it, you can sure use it for styling if you will. It has hardly any other purpose, so if it saves you from having to add an extra div, I think you should.

Upvotes: 1

Jordan Clark
Jordan Clark

Reputation: 772

Yes, you can apply style to the HTML element. What's more, it doesn't even have to exist in your original HTML document (as is allowed in HTML5), e.g. this code below is fine:

<!DOCTYPE html>
<title></title>
<style>
html {
  /* ... CSS properties go here ... */
}
</style>

The technical reason for this is because the <HTML> element is defined in the W3C specs as an implied element - basically user-agents must assume it is there, and all good UAs will append it to the DOM when rendering the web page.

Abu's answer, with respect, although in the context he is talking about is correct, is a misunderstanding of the question. Abu is referring to applying an inline STYLE attribute to the HTML element within the HTML document itself. I believe this question, on the other hand, is referring to using the html {} selector in an external CSS style sheet.

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15644

No its not recommended to use style tags inside HTML as styling should be taken care by CSS.

You shouls avoid it unless there requires a specific scenario where you want to dynamically set the style for some part.

But in that dynamic case also, I would recommend to create a class level style inside a CSS and then just add that class to the element while creation so that the required styles are applied.

Upvotes: -2

uruk
uruk

Reputation: 1306

I normally add the height-property to the HTML-element, in order to make the background-image as large as possible. Don't forget to set the body's height aswell:

html {
    height:100%;
}

body {
    height:100%;
    background:#000 url(your-image.png);
}

Upvotes: 0

Manish Doshi
Manish Doshi

Reputation: 1193

Yea nothing wrong with it.You can put style to html tag.

Reference: http://www.w3schools.com/TAGS/tag_style.asp

http://www.w3.org/TR/REC-html40/present/styles.html#edef-STYLE

Upvotes: 5

Related Questions