<style>

The HTML <style> element allows for the inclusion of CSS rules directly in a document, enabling the styling of HTML elements with custom styles defined within the <head> section.

<style> Tag

The <style> tag in HTML is used to include internal CSS within a webpage. It allows adding CSS rules directly inside the HTML document to style its elements.

Syntax

index.html
<style>
  /* CSS rules go here */
</style>

<style> Demo

  • Using the <style> element to add basic CSS styling to an HTML document:
{idx=0}
This is a heading

This is a paragraph.

index.html
<!DOCTYPE html>
<html>
<head>
<style>
  h1 {color: red;}
  p {color: blue;}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

Definition and Usage

  • The <style> tag is used to define CSS styles for a document.
  • It specifies how HTML elements should be displayed in the browser.
  • The <style> element should be placed inside the <head> section of the document.
When a browser reads a style-sheet, it applies the styles to the HTML document. If the same property is defined in multiple stylesheets, the value from the last one read will be used.

Attributes

AttributeValueDescription
mediamedia_queryIndicates the media or device for which the media resource is intended or optimized.
typetext/cssDefines the media type for the <style> tag.

Default CSS Settings

Most browsers will display the <style> element with the following default values:

style.css
style {
  display: none;
}

Conclusion

The <style> tag in HTML allows the inclusion of internal CSS to style a webpage directly within the HTML document. Placed inside the <head> section, it defines how HTML elements should be displayed in the browser. This method of styling is useful for smaller projects or when external stylesheets aren't necessary. It helps control the visual presentation of the page, enhancing the user experience.