问题
I am going to install a banner in the website, which target to run of the entire website. The banner is set to position:fixed
& bottom:0px
so it will be a floating banner at the bottom.
My website structure is a common header file, with many body files which corresponding to different pages.
If I put this DIV in the common header file (the <head>
section), it will automatically achieve the result. However, if I put this DIV in the body files instead (the <body>
section), I have to open the body documents one by one and add the DIV for so many times.
I see that on the Internet, people always don't recommend to put a DIV in <head>
section. May I know the reason? Is there any harm? As I have tested by myself, it seems there's not much difference.
回答1:
If I understand your question, you want to know the reason people say not to put div in < head > tags.
Well, the reason is that by doing that, the user experience will be unpredictable. Many years ago, there were no strict standards and the experience of each web page on different user agents was different. So W3C made more strict standards so that however the page displays on one web browser, it should display the same on a different browser.
Now if you test the correctness of your website by pasting your code on their validator, you will see that it will not pass the tests.
And for your information, < header > section and < head > are different (just in case you want mix them up). You can put divs in < header > as of HTML5, but never in < head >.
Further reading:
- http://www.w3.org/TR/html401/struct/global.html
- http://www.w3schools.com/tags/tag_header.asp
- http://validator.w3.org/#validate_by_input
回答2:
<head>
should never contain <div>
and such is is reserved for metadata like js-scripts, name of the page, loading css ...
If you want to duplicate a part of a website in several pages you could use php
.
You could for example put at the begining of your <body>
:
<?php include ('header.php'); ?>
And put the html of your header in a header.php.
(Note that you will need to run a server like wamp to test your website localy if you use php)
回答3:
A div is not semantic, but that does not mean it is bad to use it...
But now, you want to use it in the head
tag. Well, that is very very bad. The head tag is meant for metadata, not for building elements.
来源:https://stackoverflow.com/questions/26088555/placing-a-div-in-head