- HTML — The Head Element
- The HTML Element
- The HTML Element
- Example
- The HTML Element
- Example
- The HTML Element
- Example
- The HTML Element
- Examples
- Example
- Setting The Viewport
- The HTML Element
- Example
- The HTML Element
- Example
- Chapter Summary
- HTML head Elements
- How To Add CSS
- Three Ways to Insert CSS
- External CSS
- Example
- This is a heading
- «mystyle.css»
- Internal CSS
- Example
- This is a heading
- Inline CSS
- Example
- This is a heading This is a paragraph.
- Multiple Style Sheets
- Example
- Example
- Cascading Order
- : The Style Information element
- Try it
- Attributes
- Deprecated attributes
- Examples
- A simple stylesheet
- Result
- Multiple style elements
- Result
- Including a media query
- Result
- Technical summary
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
HTML — The Head Element
The HTML element is a container for the following elements: , , , , , and .
The HTML Element
The element is a container for metadata (data about data) and is placed between the tag and the tag.
HTML metadata is data about the HTML document. Metadata is not displayed.
Metadata typically define the document title, character set, styles, scripts, and other meta information.
The HTML Element
The element defines the title of the document. The title must be text-only, and it is shown in the browser’s title bar or in the page’s tab.
The element is required in HTML documents!
The content of a page title is very important for search engine optimization (SEO)! The page title is used by search engine algorithms to decide the order when listing pages in search results.
- defines a title in the browser toolbar
- provides a title for the page when it is added to favorites
- displays a title for the page in search engine-results
So, try to make the title as accurate and meaningful as possible!
Example
The content of the document.
The HTML Element
The element is used to define style information for a single HTML page:
Example
The HTML Element
The element defines the relationship between the current document and an external resource.
The tag is most often used to link to external style sheets:
Example
Tip: To learn all about CSS, visit our CSS Tutorial.
The HTML Element
The element is typically used to specify the character set, page description, keywords, author of the document, and viewport settings.
The metadata will not be displayed on the page, but is used by browsers (how to display content or reload page), by search engines (keywords), and other web services.
Examples
Define the character set used:
Define keywords for search engines:
Define a description of your web page:
Define the author of a page:
Refresh document every 30 seconds:
Setting the viewport to make your website look good on all devices:
Example
Setting The Viewport
The viewport is the user’s visible area of a web page. It varies with the device — it will be smaller on a mobile phone than on a computer screen.
You should include the following element in all your web pages:
This gives the browser instructions on how to control the page’s dimensions and scaling.
The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.
Here is an example of a web page without the viewport meta tag, and the same web page with the viewport meta tag:
Tip: If you are browsing this page with a phone or a tablet, you can click on the two links below to see the difference.
The HTML Element
The element is used to define client-side JavaScripts.
The following JavaScript writes «Hello JavaScript!» into an HTML element with >
Example
Tip: To learn all about JavaScript, visit our JavaScript Tutorial.
The HTML Element
The element specifies the base URL and/or target for all relative URLs in a page.
The tag must have either an href or a target attribute present, or both.
There can only be one single element in a document!
Example
Specify a default URL and a default target for all links on a page:
Chapter Summary
- The element is a container for metadata (data about data)
- The element is placed between the tag and the tag
- The element is required and it defines the title of the document
- The element is used to define style information for a single document
- The tag is most often used to link to external style sheets
- The element is typically used to specify the character set, page description, keywords, author of the document, and viewport settings
- The element is used to define client-side JavaScripts
- The element specifies the base URL and/or target for all relative URLs in a page
HTML head Elements
Tag | Description |
---|---|
Defines information about the document | |
Defines the title of a document | |
Defines a default address or a default target for all links on a page | |
Defines the relationship between a document and an external resource | |
Defines metadata about an HTML document | |
Defines a client-side script | |
Defines style information for a document |
For a complete list of all available HTML tags, visit our HTML Tag Reference.
How To Add CSS
When a browser reads a style sheet, it will format the HTML document according to the information in the style sheet.
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
External CSS
With an external style sheet, you can change the look of an entire website by changing just one file!
Each HTML page must include a reference to the external style sheet file inside the element, inside the head section.
Example
External styles are defined within the element, inside the section of an HTML page:
This is a heading
This is a paragraph.
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
Here is how the «mystyle.css» file looks:
«mystyle.css»
body <
background-color: lightblue;
>
h1 color: navy;
margin-left: 20px;
>
Note: Do not add a space between the property value (20) and the unit (px):
Incorrect (space): margin-left: 20 px;
Correct (no space): margin-left: 20px;
Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the element, inside the head section.
Example
Internal styles are defined within the element, inside the section of an HTML page:
This is a heading
This is a paragraph.
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.
Example
Inline styles are defined within the «style» attribute of the relevant element:
This is a heading
This is a paragraph.
Tip: An inline style loses many of the advantages of a style sheet (by mixing content with presentation). Use this method sparingly.
Multiple Style Sheets
If some properties have been defined for the same selector (element) in different style sheets, the value from the last read style sheet will be used.
Assume that an external style sheet has the following style for the element:
Then, assume that an internal style sheet also has the following style for the element:
Example
If the internal style is defined after the link to the external style sheet, the elements will be «orange»:
Example
However, if the internal style is defined before the link to the external style sheet, the elements will be «navy»:
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will «cascade» into a new «virtual» style sheet by the following rules, where number one has the highest priority:
- Inline style (inside an HTML element)
- External and internal style sheets (in the head section)
- Browser default
So, an inline style has the highest priority, and will override external and internal styles and browser defaults.
Ever heard about W3Schools Spaces? Here you can create your own website, or save code snippets for later use, for free.
: The Style Information element
The HTML element contains style information for a document, or part of a document. It contains CSS, which is applied to the contents of the document containing the element.
Try it
If you include multiple and elements in your document, they will be applied to the DOM in the order they are included in the document — make sure you include them in the correct order, to avoid unexpected cascade issues.
In the same manner as elements, elements can include media attributes that contain media queries, allowing you to selectively apply internal stylesheets to your document depending on media features such as viewport width.
Attributes
This element includes the global attributes.
This attribute defines which media the style should be applied to. Its value is a media query, which defaults to all if the attribute is missing.
A cryptographic nonce (number used once) used to allow inline styles in a style-src Content-Security-Policy. The server must generate a unique nonce value each time it transmits a policy. It is critical to provide a nonce that cannot be guessed as bypassing a resource’s policy is otherwise trivial.
This attribute specifies alternative style sheet sets.
This attribute explicitly indicates that certain operations should be blocked on the fetching of critical subresources. @import -ed stylesheets are generally considered as critical subresources, whereas background-image and fonts are not.
Deprecated attributes
This attribute should not be provided: if it is, the only permitted values are the empty string or a case-insensitive match for text/css .
Examples
A simple stylesheet
In the following example, we apply a very simple stylesheet to a document:
doctype html> html lang="en-US"> head> meta charset="UTF-8" /> title>Test pagetitle> style> p color: red; > style> head> body> p>This is my paragraph.p> body> html>
Result
Multiple style elements
In this example we’ve included two elements — notice how the conflicting declarations in the later element override those in the earlier one, if they have equal specificity.
doctype html> html lang="en-US"> head> meta charset="UTF-8" /> title>Test pagetitle> style> p color: white; background-color: blue; padding: 5px; border: 1px solid black; > style> style> p color: blue; background-color: yellow; > style> head> body> p>This is my paragraph.p> body> html>
Result
Including a media query
In this example we build on the previous one, including a media attribute on the second element so it is only applied when the viewport is less than 500px in width.
doctype html> html lang="en-US"> head> meta charset="UTF-8" /> title>Test pagetitle> style> p color: white; background-color: blue; padding: 5px; border: 1px solid black; > style> style media="all and (max-width: 500px)"> p color: blue; background-color: yellow; > style> head> body> p>This is my paragraph.p> body> html>
Result
Technical summary
Content categories | Metadata content, and if the scoped attribute is present: flow content. |
---|---|
Permitted content | Text content matching the type attribute, that is text/css . |
Tag omission | Neither tag is omissible. |
Permitted parents | Any element that accepts metadata content. |
Implicit ARIA role | No corresponding role |
Permitted ARIA roles | No role permitted |
DOM interface | HTMLStyleElement |
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.