Category: 01. Tutorials

https://cdn3d.iconscout.com/3d/free/thumb/free-html-3d-icon-png-download-7578018.png

  • HTML – Backgrounds

    The background of a webpage is a layer behind its content, which includes text, images, colors, and various other elements.

    It is an essential part of web design that improves the overall look of a web page as well as the user experience. HTML offers multiple attributes and properties for manipulating the background of elements within a document.

    By default, our webpage background is white in color. We may not like it, but no worries. HTML provides the following two good ways to decorate our webpage background:.

    • HTML Background with Colors
    • HTML Background with Images

    Syntax

    The following are the syntaxes for HTML backgrounds:

    <body background = value><body style="background-color: value;">

    The value can be an English name of color, an RGB value of color, or a hexadecimal value of color.

    Examples of HTML Background

    Following are some example codes that show how to set different styles of background for an HTML document.

    • Setting Color for Background
    • Setting Image as Background
    • Background Repeat and Position
    • Setting Patterned Backgrounds

    Setting Color for Background

    An elementary method to modify the background is by altering its color. The background-color property facilitates the specification of a color for an element’s background. This can be accomplished by incorporating the following style attribute within the opening tag of an HTML element.

    Example

    The following is an example of setting color for background to a DIV:

    <!DOCTYPE html><html lang="en"><head><title>Styled Div Example</title></head><body><div style="background-color: #3498db; "><h1>Welcome to My Website</h1><p>
    
         This is an example of a styled div with a 
         background color and text color.
      &lt;/p&gt;&lt;!-- Additional content goes here --&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Image as Background

    HTML allows us to specify an image as the background of our HTML web page or table. The background and background-image can be used to control the background image of an HTML element, specifically page body and table backgrounds. We simply need to pass the path of an image as a value to both properties as illustrated in the next example. In the below example, the background-image property is assigned to the body of web page.

    Example

    The following is an example of setting an image as a background color; here we are setting an image to the background of the webage with the <body> tag:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>Background Image Example</title></head><body background="/market/public/assets/newDesign/img/logo.svg"><div style="background-color: rgba(255, 255, 255, 0.8); padding: 20px;"><h1>Welcome to My Website</h1><p>
    
         This is an example of setting a background 
         image using HTML attributes.
      &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Background Repeat and Position

    Although you can set an image as the background using just HTML, in order to control it's behavior, like repeating and positioning, we need to use CSS. We recommend watching our CSS background-image for better understanding. CSS offers options for controlling how background images repeat and their positioning. The background-repeat property specifies whether the image should repeat horizontally, vertically, both, or neither. Furthermore, the background-position property empowers developers to determine where the background image should be positioned within the element.

    Example

    The below HTML program creates a webpage with a centered content div having a repeating vertical background pattern from the specified image. The background color of the container is set to white by default, and the text within the container is styled with a black color, creating a visually appealing and cohesive design.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Background Repeat</title><style>
    
      body {
         background-image: 
            url('/market/public/assets/newDesign/img/logo.svg');
         background-repeat: repeat-y;
         background-position: center;
         justify-content: center;
         align-items: center;
         display: flex;
      }
      div{
         background-color: rgba(255, 255, 255, 0.6); 
         padding: 20px;
      }
    </style></head><body><div><h1>Welcome to My Website</h1><p>
         This is an example of setting a background 
         image using HTML attributes.
      &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Patterned Backgrounds

    You might have seen many patterns or transparent backgrounds on various websites. This can simply be achieved by using a patterned image or transparent image in the background. It is suggested that while creating patterns or transparent GIF or PNG images, use the smallest dimensions possible, even as small as 1x1 to avoid slow loading.

    Example

    Here is an example of how to set the background pattern of a table.

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Background Images</title></head><body><!-- Set a table background using pattern --><table background = "/images/pattern1.gif" 
    
         width = "100%" 
         height = "100"&gt;&lt;tr&gt;&lt;td&gt;
         This background is filled up with a pattern image.
      &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;!-- Another example on table background using pattern --&gt;&lt;table background = "/images/pattern2.gif" 
         width = "100%" 
         height = "100"&gt;&lt;tr&gt;&lt;td&gt;
         This background is filled up with a pattern image.
      &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  • HTML – id

    The id is an important keyword in HTML. HTML “id” is an attribute used to uniquely identify an element within a web page. It serves as a label for that element and enables JavaScript and CSS to target it specifically.

    HTML id attribute is defined in the HTML code using the “id” keyword, and the styling is determined in CSS. This identification helps in applying custom styles, making interactive features, and navigating within the webpage with precision. The “id” values must be unique within the document.

    We highly recommend you to use classes to style any element through CSS..

    Syntax for id

    In CSS, you can target the id attribute by using a hash symbol (#) followed by the id name in HTML element. Try not to use id in CSS rather you can use class attribute. Ids are specially created to execute through JavaScript.

    • In HTML:<element class=”highlight”>…</element>
    • In CSS:/* CSS using id Attribute Selector */#highlight{background-color: yellow;color: black;font-weight: bold;}
    • In JavaScript:document.getElementById(‘highlight’)

    Using HTML id Attribute

    HTML ids are essential for managing events, and changing the structure of documents. In order to create interactive and dynamic web pages, it gives developers the ability to target particular parts and provide them specialized behavior and appearance. Rarely it is used to do the styling in CSS.

    Define a id for Styling

    In the following example, we have create two element one is h1 and other is p, and we set id on them as well “header” & “heightlight” but using the “heightlight” is in internal CSS to style our p element. You can use the “header” id in the similar way to style the h1 element.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
      &lt;!-- CSS id attribute Selector Used --&gt;
      #highlight {
         background-color: yellow;
         color: black;
         padding: 5px;
      }
    </style></head><body><!-- Using id attribute in both Element --><h1 id="header">Tutorialspoint</h1><p id="highlight">Simply Easy Learning</p></body></html>

    Using id Attribute through JavaScript

    The ids are frequently used to identify elements for JavaScript functions. For example, you can use a id to target specific elements, like paragraph, and make them interactive through JavaScript. In the following code we have create a button which will trigger a function that will change the display property none to block of a p element. You will see a paragraph.

    Open Compiler

    <!DOCTYPE html><html><head><script>
    
      function showContent() {
         var element = document.getElementById('content');
         if (element.style.display === 'none') {
            element.style.display = 'block';
         } else {
            element.style.display = 'none';
         }
      }
    </script><style>
      .interactive-button {
         background-color: #007bff;
         color: #fff;
         padding: 10px 20px;
         border: none;
         cursor: pointer;
      }
    </style></head><body><button class="interactive-button"
           onclick="showContent()"&gt;Click Me&lt;/button&gt;&lt;p class="content" style="display: none;"&gt;
       This content can be toggled by clicking the button.
    </p></body></html>

    Difference between id and class in HTML

    In HTML, the id attribute uniquely identifies a single element on a page, making it useful for targeting with CSS and JavaScript, and it must be unique within the document. The class attribute, on the other hand, can be applied to multiple elements, allowing for the grouping of elements that share common styles or behaviors.

    <!DOCTYPE html><html lang="en"><head><title>Difference between id and class</title><style>
    
      /* ID selector */
      #header {
         background-color: blue;
         color: white;
         padding: 10px;
      }
      /* Class selector */
      .button {
         background-color: green;
         color: white;
         padding: 5px 10px;
         margin: 5px;
      }
    </style></head><body><!-- Unique ID for the header --><div id="header">
         This is the header
      &lt;/div&gt;&lt;!-- Shared class for buttons --&gt;&lt;div class="button"&gt;
         Button 1
      &lt;/div&gt;&lt;div class="button"&gt;
         Button 2
      &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Things to Remember about id

    • The id attribute should contain at least one character should be there, the starting letter should be a character (a-z) or (A-Z), and the rest of the letters of any type can written even special characters.
    • The id attribute does not contain any spaces.
    • Within the document every id must be unique.

    Valid id Attributes Pattern

    Certain ID Attributes are valid in HTML 5, but not in Cascading Style Sheets. In such cases, it is recommended to go with simple output rather than styled output because certain values that we use for ID may be invalid in CSS.

    Following example demonstrates the use of simple ID attributes.

    Example

    If we execute below code, two div elements will be displayed, one with id attribute (TutorialsPoint Website), and the other one with other id attribute (Html5 Tutorials, CSS Tutorials, JavaScript Tutorials).

    <!DOCTYPE html><html><head><title>Simple Id Attributes</title><style>
    
      /* Remove @ from the code and run the code again */
      #@TP {
         color: #070770;
         text-align: center;
         font-size: 30px;
         font-weight: bold;
      }
      #@TP1 {
         text-align: center;
         font-size: 25px;
      }
    </style></head><body><div id="@TP">
      TutorialsPoint Website
    </div><div id="@TP1">
      Html5 Tutorials, CSS Tutorials, JavaScript Tutorials 
    </div></body></html>

    If we remove the @ symbol from the id's value the it will become avalid id declaration and applied styles will be effected on the HTML Element.

  • Meta Tags

    HTML <meta> tag lets us specify metadata, which is additional important information about a document, in a variety of ways. The META elements can be used to include name and content pairs describing properties of the HTML document, such as author, expiry date, a list of keywords, document author, etc.

    HTML <meta> tag can be used to provide extra information. It’s a self-closing element, meaning it doesn’t require a closing tag but carries information within its attributes. You can include one or more meta tags in your document based on what information you want to keep in your document, but in general, meta tags do not impact the physical appearance of the document, so from the appearance point of view, it does not matter if you include them or not.

    Adding Metadata to Web Pages Using Meta Tags

    You can add metadata to your web pages by placing <meta> tags inside the header of the document, which is represented by the <head> tag.

    The following metadata can be added using the <meta> tag:

    Below, you can check all the examples that are well described with the code for how we should use individuals on our website.

    Specifying Keywords

    You can use the <meta> tag to specify important keywords related to the document, and later these keywords are used by the search engines while indexing your webpage for searching purposes.

    Example

    Following is an example where we are adding “HTML, Meta Tags, and Metadata” as important keywords about the document:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /></head><body><p>Hello HTML5!</p></body></html>

    Document Description

    You can use the <meta> tag to give a short description about the document. This again can be used by various search engines while indexing your webpage for searching purposes.

    Example

    The following example demonstrates how you can define a meta description for a webpage:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /></head><body><p>Hello HTML5!</p></body></html>

    Document Revision Date

    You can use the <meta> tag to give information about the last time the document was updated. This information can be used by various web browsers while refreshing your webpage.

    Example

    The following example demonstrates how you can define a revision date:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /></head><body><p>Hello HTML5!</p></body></html>

    Document Refreshing

    The <meta> tag can be used to specify a duration after which your web page will keep refreshing automatically.

    Example

    If you want your page to keep refreshing after every 5 seconds, then use the following syntax:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /><meta http-equiv="refresh" content="5" /></head><body><p>Hello HTML5!</p></body></html>

    Page Redirection

    You can use the <meta> tag to redirect your page to any other webpage. You can also specify a duration if you want to redirect the page after a certain number of seconds.

    Example

    Following is an example of redirecting the current page to another page after 5 seconds. If you want to redirect the page immediately, then do not specify the content attribute:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /><meta http-equiv="refresh" content="5; url=http://www.tutorialspoint.com" /></head><body><p>Hello HTML5!</p></body></html>

    Setting Cookies

    Cookies are data stored in small text files on your computer, and it is exchanged between a web browser and a web server to keep track of various information based on your web application needs.

    You can use the <meta> tag to store cookies on the client side, and later this information can be used by the Web server to track a site visitor. If you do not include the expiration date and time, the cookie is considered a session cookie and will be deleted when the user exits the browser.

    Example

    Following is an example of redirecting the current page to another page after 5 seconds. If you want to redirect the page immediately, then do not specify the content attribute.

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="revised" content="Tutorialspoint, 3/7/2014" /><meta http-equiv="cookie" content="userid=xyz; expires=Wednesday, 08-Aug-15 23:59:59 GMT;" /></head><body><p>Hello HTML5!</p></body></html>

    Note: You can check the PHPand Cookies tutorial for a complete detail on cookies.

    Setting Author Name

    You can set an author name on a web page using a <meta> tag. Author name be specified by assigning the “author” value to the “name” attribute.

    Example

    The following example demonstrates how you can set an author name:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="author" content="Mahnaz Mohtashim" /></head><body><p>Hello HTML5!</p></body></html>

    Specify Character Set

    You can use the <meta> tag to specify the character set used within the webpage. By default, Web servers and Web browsers use ISO-8859-1 (Latin1) encoding to process Web pages.

    Example

    Following is an example to set UTF-8 encoding:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="author" content="Mahnaz Mohtashim" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><p>Hello HTML5!</p></body></html>

    Example

    To serve the static page with traditional Chinese characters, the webpage must contain a <meta> tag to set Big5 encoding:

    Open Compiler

    <!DOCTYPE html><html><head><title>Meta Tags Example</title><meta name="keywords" content="HTML, Meta Tags, Metadata" /><meta name="description" content="Learning about Meta Tags." /><meta name="author" content="Mahnaz Mohtashim" /><meta http-equiv="Content-Type" content="text/html; charset=Big5" /></head><body><p>Hello HTML5!</p></body></html>

  • HTML Computer Code Elements

    HTML computer code elements (tags) provide unique formatting and text styles for different types of code-related messages, such as keyboard input, preformatted text, code snippets, variables, and sample outputs. The computer code elements are listed as follows:

    • <kbd> Element
    • <pre> Element
    • <code> Element
    • <var> Element
    • <samp> Element

    HTML <kbd> Element

    The <kbd> tag is used to define the keyboard input. Use this when you want the user to type on their keyboard, for example, shortcut keys Ctrl+C for copy, Esc for exit, etc.

    Example

    Let us now see an example to implement the <kbd> tag:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Example of kbd Tag</h2><p>Use the following shortcut keys:</p><p><strong>Cut</strong><kbd>CTRL</kbd>+<kbd>X</kbd></p><p><strong>Copy</strong><kbd>CTRL</kbd>+<kbd>C</kbd></p><p><strong>Paste</strong><kbd>CTRL</kbd>+<kbd>V</kbd></p><p><strong>Undo</strong><kbd>CTRL</kbd>+<kbd>Z</kbd></p></body></html>

    HTML <pre> Element

    The <pre> tag in HTML is used to set preformatted text. The text preserves spaces and line breaks, appearing the same on the web page as in the HTML code.

    Example

    Let us now see an example to implement the <pre> tag:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Example of pre Tag</h2><pre>
      This is a demo text
      and will appear
      in the same format as
      it
      is visible
      here. The pre tag displays
      the text in a fixed-width
      font. It preserves
      both spaces and
      line breaks as you can see
      here.
    </pre></body></html>

    HTML <code> Element

    The <code> tag is used to format code in an HTML document. For example, to write and format Java code properly, use the <code> element.

    Example

    Let us now see an example to implement the <code> element:

    Open Compiler

    <!DOCTYPE html><html><body><h1>Example of code Tag</h1><h2>C++</h2><code>
    
         #include 
         &lt;iostream&gt;&lt;/code&gt;&lt;h2&gt;C&lt;/h2&gt;&lt;code&gt;
         #include 
         &lt;stdio.h&gt;&lt;/code&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    HTML <var> Element

    The <var> tag in HTML is used to display mathematical expressions for calculations.

    Example

    Let us now see an example to implement the <var> tag:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Example of var Tag</h2>
    
      Sample equation  &lt;var&gt;2x&lt;/var&gt; - &lt;var&gt;2z&lt;/var&gt; = &lt;var&gt;3y&lt;/var&gt; + 9
    </body></html>

    HTML <samp> Element

    The <samp> tag is a phrase tag used to format text in an HTML document.

    Example

    Let us now see an example to implement the <samp> element:

    Open Compiler

    <!DOCTYPE html><html><body><h2>Exam Results</h2><p><s>Result would be announced on 6th June.</s></p><samp>New date for results is 7th June.</samp></body></html>

    HTML Computer Code Elements

    Here is the list of the computer code tags along with their descriptions used for defining user input and computer code:

    TagDescription
    <kbd>Defines keyboard input.
    <pre>Displays preformatted text with preserved spaces and line breaks.
    <code>Defines a piece of computer code.
    <var>Represents a variable in programming or math expressions.
    <samp>Defines sample output from a program or device.
  • Phrase Tags

    HTML phrase tags have been designed for specific purposes, though they are displayed in a similar way as other basic tags like <b>, <i>, <pre>, and <tt>. Here we will take you through all the important phrase tags; so let’s start seeing them one by one. Following is the list of phrase tags, some of them are discussed in HTML formatting and quotations.

    HTML Phrase Tags List

    • Emphasized Text – HTML em Tag
    • Marked Text – HTML mark Tag
    • Strong Text – HTML strong Tag
    • Abbreviation Text – HTML abbr Tag
    • Acronym Text – HTML acronym Tag
    • Directed Text – HTML bdo Tag
    • Special Terms – HTML dfn Tag
    • Short Quoting Text – HTML q tag
    • Long Quoting Text – HTML blockquote Tag
    • Citations Text – HTML cite Text
    • Computer Code Text – HTML code Tag
    • Keyboard Text – HTML kbd Text
    • Programming Variables – HTML pre Tag
    • Program Output – HTML samp Tag
    • Address Text – HTMl address Tag

    Below we have used each tags of phrase tags, each tag has it’s default styling few of them accepts some attributes as well.

    Emphasized Text

    Content that is enclosed within the <em>…</em> element is displayed as emphasized text. The <em> element typically renders text in italics, indicating emphasis.

    Example

    Open Compiler

    <!DOCTYPE html><body><p>The following word uses a <em>emphasized</em> typeface.</p></body></html>

    Output

    The following word uses a emphasized typeface.
    

    Marked Text

    Anything that is enclosed within the <mark>…</mark> element is displayed as marked with yellow ink.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following word has been <mark>marked</mark> with yellow.</p></body></html>

    Output

    The following word has been marked with yellow.
    

    Strong Text

    Content that is enclosed within the <strong>…</strong> element is displayed as important text. The <strong> element displays text in a bold font, indicating strong importance.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following word uses a <strong>strong</strong> typeface. </p></body></html>

    Output

    The following word uses a strong typeface.
    

    Abbreviation Text

    You can abbreviate a text by putting it inside opening <abbr> and closing </abbr> tags. If present, the title attribute must contain this full description and nothing else.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>My best friend's name is <abbr title="Abhishek">Abhy</abbr>. </p></body></html>

    Output

    My best friend's name is Abhy.
    

    Acronym Text

    The <acronym> element allows you to indicate that the text between <acronym> and </acronym> tags is an acronym.

    At present, the major browsers do not change the appearance of the content of the <acronym> element.

    The <acronym> element is deprecated in HTML5. Instead, you should use the <abbr> element to define abbreviations, and you can specify the full description using the “title” attribute.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>This chapter covers marking up text in <acronym>XHTML</acronym>. </p></body></html>

    Output

    This chapter covers marking up text in XHTML.
    

    Directed Text

    The <bdo>…</bdo> element stands for Bi-Directional Override, and it is used to override the current text direction.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>This text will go left to right.</p><p><bdo dir="rtl">This text will go right to left.</bdo></p></body></html>

    Output

    This text will go right to left.
    

    Special Terms

    The <dfn>…</dfn> element (or HTML Definition Element) allows you to specify that you are introducing a special term. Its usage is similar to italic words in the midst of a paragraph.

    Typically, you would use the <dfn> element the first time you introduce a key term. Most recent browsers render the content of a <dfn> element in an italic font.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following word is a <dfn>special</dfn> term. </p></body></html>

    Output

    The following word is a special term.
    

    Quoting Text

    When you want to quote a passage from another source, you should put it in between <blockquote>…</blockquote> tags.

    Text inside a <blockquote> element is usually indented from the left and right edges of the surrounding text and sometimes uses an italicized font.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>The following description of XHTML is taken from the W3C Web site:</p><blockquote>XHTML 1.0 is the W3C's first Recommendation for XHTML, following on from earlier work on HTML 4.01, HTML 4.0, HTML 3.2 and HTML 2.0.</blockquote></body></html>

    Output

    The following description of XHTML is taken from the W3C Web site:
    XHTML 1.0 is the W3C's first Recommendation for XHTML, following on 
    from earlier work on HTML 4.01, HTML 4.0, HTML 3.2 and HTML 2.0.
    

    Short Quotations

    The <q>…</q> element is used when you want to add a double quote within a sentence. By using <q>…</q>, you ensure that the enclosed text is presented as a direct quotation, enhancing readability and maintaining proper punctuation in your HTML document.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Amit is in Spain, <q>I think I am wrong</q>. </p></body></html>

    Output

    Amit is in Spain, I think I am wrong.
    

    Text Citations

    If you are quoting a text, you can indicate the source by placing it between an opening <cite>tag and closing </cite> tag.

    As you would expect in a print publication, the content of the <cite> element is rendered in italicized text by default.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>This HTML tutorial is derived from <cite>W3 Standard for HTML</cite>. </p></body></html>

    Output

    This HTML tutorial is derived from W3 Standard for HTML.
    

    Computer Code

    Any programming code to appear on a Web page should be placed inside <code>…</code> tags. Usually the content of the <code> element is presented in a monospaced font, just like the code in most programming books.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Regular text. <code>This is code.</code> Regular text. </p></body></html>

    Output

    Regular text. This is code. Regular text.
    

    Keyboard Text

    When you are talking about computers, if you want to tell a reader to enter some text, you can use the <kbd>…</kbd> element to indicate what should be typed in, as in this example.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Regular text. <kbd>This is inside kbd element</kbd> Regular text. </p></body></html>

    Output

    Regular text. This is inside kbd element Regular text.
    

    Programming Variables

    The <var> element is usually used in conjunction with the <pre> and <code> elements to indicate that the content of that element is a variable.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p><code>document.write(" <var>user-name</var>") </code></p></body></html>

    Output

    document.write(" user-name") 

    Program Output

    The <samp>…</samp> element indicates sample output from a program, and script, etc. Again, it is mainly used when documenting programming or coding concepts.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><p>Result produced by the program is <samp>Hello World!</samp></p></body></html>

    Output

    Result produced by the program is Hello World!
    

    Address Text

    The <address>…</address> element is used to contain any address.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><address>388A, Road No 22, Jubilee Hills - Hyderabad</address></body></html>

    Output

    388A, Road No 22, Jubilee Hills - Hyderabad
  • Iframes

    HTML iframe is an inline frame that allows you to embed another document within the current HTML document. Whenever you want to display another webpage within the webpage, you can use an iframe. 

    Creating iframe (Inline Frame)

    In HTML, the inline frame is defined with the <iframe> tag. This tag creates a rectangular region at a specified place within the HTML document in which the browser can display an external document such as a map or another web page.

    Iframe Syntax

    The following is the syntax to create an inline frame (iframe) in HTML:

    <iframe src="url" title="description"></iframe>

    The src Attribute

    The URL or path of the external document is attached using the src attribute of the <iframe> tag. If the content of the iframe exceeds the specified rectangular region, HTML automatically includes the scrollbars. HTML allows any number of iframes, but it may affect the performance of the website.

    Iframe Example

    The following example demonstrates how you can create an iframe in HTML:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Iframes</title></head><body><p>It is an example of HTML Iframe</p><iframe src="/html/menu.htm"> Sorry your browser does not support inline frames. </iframe></body></html>

    The <iframe> Tag Attributes

    The following table describe the attributes used with the <iframe> tag.

    S.No.Attribute & Description
    1srcThis attribute is used to give the file name that should be loaded in the frame. Its value can be any URL. For example, src=”/html/top_frame.htm” will load an HTML file available in html directory.
    2nameThis attribute allows to give a name to a specific frame. It is used to indicate which frame a document should be loaded into. This is important when you want to create links in one frame that load pages into an another frame, in which case the second frame needs a name to identify itself as the target of the link.
    3heightThis attribute specifies the height of <iframe>. By default it is 150 pixels.
    4widthThis attribute specifies the width of <iframe>. By default it is 300 pixels.
    5allowIt is used to specify the permission policies to access features like microphone and camera.
    6loadingIt specifies the time to load a given iframe.

    Setting Height and Width of Iframes

    You can set the height and width of an HTML iframe by using the height and width attributes of the <iframe> tag.

    Example

    The following example demonstrates how you can set the height and width of an iframe:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Iframes</title></head><body><h2>Example of Setting Height and width of HTML Iframe</h2><iframe src="/index.htm" width="500" height="300"> 
    
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The above code will display the "index.htm" webpage in an iframe with the specified height and width.

    Linking to an Iframe: Target and Name Attributes

    You can use an iframe as a target frame to open a webpage on clicking a link.

    You can create a target iframe for a link (hyperlink) by using the name attribute of the <iframe> tag. The value of the name attribute is used in the target attribute of elements like <form> and <a> to specify the target frame.

    Example

    The following example demonstrates how you can make a target iframe for a hyperlink:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Iframes</title></head><body><h2>Linking to an Iframe: Target and Name Attributes</h2><p>Click on the link below to load the content inside the specified frame...</p><p><a href="/html/html_iframes.htm" target="Iframe">
    
      Iframe Tutorial
      &lt;/a&gt;&lt;/p&gt;&lt;iframe style="background-color: skyblue;" name="Iframe" width="500" height="300"&gt;
    Sorry your browser does not support inline frames.
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    On execution, the above code will generate a link and an Iframe with a sky-blue background. When the link is clicked, its content will open inside the iframe.

    Styling Iframe

    You can also use the style or class attributes to apply the CSS rules on an iframe.

    Example

    The following example demonstrates how you can apply CSS styles to an iframe:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Iframes</title><style type="text/css">
    
      body{
        background-color: #FFF4A3;
      }
      .my_iframe{
        width: 90%;
        height: 180px;
        border: 2px solid #f40;
        padding: 8px;
      }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Example of Styling Iframe&lt;/h2&gt;&lt;iframe src="/index.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Multiple Iframes

    You can embed multiple documents (webpages) within a webpage. HTML allows you to use multiple <iframe> tags in an HTML document.

    Note: Use of multiple iframes may slow down your page loading speed.

    Example

    In the following example, we are embedding three webpages using multiple iframes:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Iframes</title><style type="text/css">
    
      body{
        background-color: #FFF4A3;
      }
      .my_iframe{
        width: 90%;
        height: 180px;
        border: 2px solid #f40;
        padding: 8px;
        margin-bottom: 8px;
      }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Example of Multiple Iframes&lt;/h2&gt;&lt;h3&gt;Index Page&lt;/h3&gt;&lt;iframe src="/index.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;h3&gt;Tutorials Library&lt;/h3&gt;&lt;iframe src="/tutorialslibrary.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;h3&gt;Compilers&lt;/h3&gt;&lt;iframe src="/codingground.htm" class="my_iframe"&gt; 
    Sorry your browser does not support inline frames. 
    &lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Frames

    HTML frames are used to divide your browser window into multiple sections where each section can load a separate HTML document independently. A collection of frames in the browser window is known as a frameset. The window is divided into frames in a similar way the tables are organized: into rows and columns.

    The <frame> tag is no longer recommended as it is not supported by HTML5. Instead of using this tag, we can use the <iframe> or <div> with CSS to achieve the similar effects.

    Syntax

    <frameset rows="50%,50%"><frame name="top" src="link/to/frame1" /><frame name="bottom" src="link/to/frame2" /></frameset>

    Where the rows attribute of frameset defines the division of the window into horizontal sections. In this case, the window is divided into two rows, each taking up 50% of the available height.

    Examples of HTML Frames

    Here are some example codes that illustrate how to manipulate frames in HTML.

    Creating Frames in HTML

    To make frames on a page we use <frameset> tag instead of <body> tag. The <frameset> tag defines how to divide the window into frames. The rows attribute of <frameset> tag defines horizontal frames and cols attribute defines vertical frames. Each frame is indicated by <frame> tag and it defines which HTML document shall open into the frame.

    Following is the example to create three horizontal frames. If your browser does not support frames, then body element is displayed.

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Frames</title></head><frameset rows="10%,80%,10%"><frame name="top" src="/html/top_frame.htm" /><frame name="main" src="/html/main_frame.htm" /><frame name="bottom" src="/html/bottom_frame.htm" /><noframes><body>
    
         Your browser does not support frames.
      &lt;/body&gt;&lt;/noframes&gt;&lt;/frameset&gt;&lt;/html&gt;</pre>

    Creating vertical Frames

    Here we replaced rows attribute by cols and changed their width. This will create all the three frames vertically.

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Frames</title></head><frameset cols="25%,50%,25%"><frame name="left" src="/html/top_frame.htm" /><frame name="center" src="/html/main_frame.htm" /><frame name="right" src="/html/bottom_frame.htm" /><noframes><body>
    
         Your browser does not support frames.
      &lt;/body&gt;&lt;/noframes&gt;&lt;/frameset&gt;&lt;/html&gt;</pre>

    Frame's name and target Attributes

    One of the most popular uses of frames is to place navigation bars in one frame and then load main pages into a separate frame.

    Let's see following example where a test.htm file has following code:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Target Frames</title></head><frameset cols="200, *"><frame src="/html/menu.htm" name="menu_page" /><frame src="/html/main.htm" name="main_page" /><noframes><body>
       Your browser does not support frames.
    </body></noframes></frameset></html>

    Here we have created two columns to fill with two frames. The first frame is 200 pixels wide and will contain the navigation menubar implemented by menu.htm file. The second column fills in remaining space and will contain the main part of the page and it is implemented by main.htm file. For all the three links available in menubar, we have mentioned target frame as main_page, so whenever you click any of the links in menubar, available link will open in main_page.

    Following is the content of menu.htm file

    Open Compiler

    <!DOCTYPE html><html><body bgcolor="#4a7d49"><a href="https://www.google.com" target="main_page">
    
      Google
    </a><br /><br /><a href="https://www.microsoft.com" target="main_page">
      Microsoft
    </a><br /><br /><a href="https://news.bbc.co.uk" target="main_page">
      BBC News
    </a></body></html>

    Following is the content of main.htm file:

    Open Compiler

    <!DOCTYPE html><html><body bgcolor="#b5dcb3"><h3>
    
      This is main page and content from any link 
      will be displayed here.
    </h3><p>
      So now click any link and see the result.
    </p></body></html>

    Attributes of frameset Tag

    Below listed attributes are accepted by frameset tag.

    AttributesDescription
    colsSpecifies how many columns are contained in the frameset and the size of each column. You can specify the width of each column in one of four ways.Absolute values in pixels. For example to create three vertical frames, use cols="100, 500,100".A percentage of the browser window. For example to create three vertical frames, use cols="10%, 80%,10%".Using a wildcard symbol. For example to create three vertical frames, use cols="10%, *,10%". In this case wildcard takes remainder of the window.As relative widths of the browser window. For example to create three vertical frames, use cols="3*,2*,1*". This is an alternative to percentages. You can use relative widths of the browser window. Here the window is divided into sixths: the first column takes up half of the window, the second takes one third, and the third takes one sixth.
    rowsThis attribute works just like the cols attribute and takes the same values, but it is used to specify the rows in the frameset. For example to create two horizontal frames, use rows="10%, 90%". You can specify the height of each row in the same way as explained above for columns.
    borderThis attribute specifies the width of the border of each frame in pixels. For example border="5". A value of zero means no border.
    frameborderThis attribute specifies whether a three-dimensional border should be displayed between frames. This attribute takes value either 1 (yes) or 0 (no). For example frameborder="0" specifies no border.
    framespacingThis attribute specifies the amount of space between frames in a frameset. This can take any integer value. For example framespacing="10" means there should be 10 pixels spacing between each frames.

    HTML <frame> Tag Attributes

    Below listed arrtibutes are accepted by the frame tag.

    AttributeDescription
    srcThis attribute is used to give the file name that should be loaded in the frame. Its value can be any URL. For example, src="/html/top_frame.htm" will load an HTML file available in html directory.
    nameThis attribute allows you to give a name to a frame. It is used to indicate which frame a document should be loaded into. This is especially important when you want to create links in one frame that load pages into an another frame, in which case the second frame needs a name to identify itself as the target of the link.
    frameborderThis attribute specifies whether or not the borders of that frame are shown; it overrides the value given in the frameborder attribute on the <frameset> tag if one is given, and this can take values either 1 (yes) or 0 (no).
    marginwidthThis attribute allows you to specify the width of the space between the left and right of the frame's borders and the frame's content. The value is given in pixels. For example marginwidth="10".
    marginheightThis attribute allows you to specify the height of the space between the top and bottom of the frame's borders and its contents. The value is given in pixels. For example marginheight="10".
    noresizeBy default you can resize any frame by clicking and dragging on the borders of a frame. The noresize attribute prevents a user from being able to resize the frame. For example noresize="noresize".
    scrollingThis attribute controls the appearance of the scrollbars that appear on the frame. This takes values either "yes", "no" or "auto". For example scrolling="no" means it should not have scroll bars.
    longdescThis attribute allows you to provide a link to another page containing a long description of the contents of the frame. For example longdesc="framedescription.htm"

    Disadvantages of Frames

    There are few drawbacks with using frames, so it's never recommended to use frames in your webpages.

    • Some smaller devices cannot cope with frames often because their screen is not big enough to be divided up.
    • Sometimes your page will be displayed differently on different computers due to different screen resolution.
    • The browser's back button might not work as the user hopes.
    • There are still few browsers that do not support frame technology.
  • Image Map

    Image Maps

    HTML image maps are defined by the <map> tag. An image map enables specific areas of an image to be clickable, acting as links to different destinations. This technique is useful for creating complex navigation systems or interactive graphics on a webpage.

    By defining various shapes (rectangles, circles, and polygons) within an image, each with its own associated link, developers can create dynamic and interactive visual content that enhances user engagement and navigation.

    Use of Image Maps

    Image maps are useful for creating complex navigation, interactive diagrams, or engaging visual experiences, enhancing user engagement and interactivity on web pages.

    It is useful to create interactive graphics by defining clickable regions within an image. This allows users to interact with different image parts, triggering specific actions or links.

    HTML <map> Tag

    The <map> tag is used to create a client-side image map, turning specific regions of an image into interactive elements. This allows users to click on different areas of the image, triggering various actions. The <map> element serves as a container for <area> elements, each defining a clickable region with specific attributes.

    Syntax

    The following is the syntax of the <map> tag:

    <map name="world map"><!-- Define your clickable areas here --></map>

    When used in conjunction with the <img> tag, the <map> tag establishes a connection between the image and its associated map. This enables web developers to create dynamic and interactive content by defining distinct clickable zones within an image.

    Defining Areas (Shapes) in Image Maps

    The <area> tag is used within the <map> tag to define clickable regions on an image. It specifies the shape, coordinates, and associated actions for each clickable area.

    Following is the syntax of the <area> tag −

    <area shape="shape_values" coords="coordinates" href="url" alt="Description">

    1. Rectangular Area

    You can define a rectangular shape by assigning the rect value to the shape attribute. The rectangular shape requires coordinates for the top-left and bottom-right corners that you can define in coords attribute.

    Syntax

    <area shape="rect" coords="x1,y1,x2,y2" href="url" alt="Description">
    • x1, y1 − Coordinates of the top-left corner.
    • x2, y2 − Coordinates of the bottom-right corner.

    2. Circular Area

    You can define a circular shape by assigning the circle value to the shape attribute. The circular shape requires center coordinates (xy) and radius (r) that you can define in coords attribute.

    <area shape="circle" coords="x,y,r" href="url" alt="Description">
    • x, y − Coordinates of the circle’s center.
    • r − Radius of the circle.

    3. Polygon Area

    You can define a polygon shape by assigning the poly value to the shape attribute. The polygon shape requires a series of coordinates to form the shape that you can define in coords attribute.

    This can be used to create any shape, whether it be a mango or apple.

    Syntax

    <area shape="poly" coords="x1,y1,x2,y2,..,xn,yn" href="url" alt="Description">

    Where x1, y1,..., xn, yn coordinates form the polygon.

    These shapes enable the creation of interactive image maps, enhancing user engagement by associating distinct actions with specific areas within an image.

    Creating an Image Map in HTML

    To create an image map in HTML, follow these steps with a code example −

    Step 1: Prepare Your Image

    Begin with the image you want to use as an image map. For this example, we’ll use an image file named logo.png.

    Use the usemap attribute in the <img> tag to link the image to the <map> tag by keeping its value to the name attribute of the <map>:

    <img src="/images/logo.png" usemap="#image_map">

    Step 2: Define the Image Map

    Use the <map> tag to define the image map and give it a unique name with the name attribute.

    <map name="image_map"></map>

    Step 3: Define Clickable Areas

    Within the <map> element, define clickable areas using <area> tags. Specify the shape (rectcircle, or poly), coordinates, and the URL to link to.

    <map name="image_map"><area shape="circle" coords="45,32,49" href="index.htm" alt="tutorialspoint"><area shape="rect" coords="245,50,85,9" href="/tutorialslibrary.htm" alt="tutorials_library"></map>

    Repeat Step 3 for each clickable area you want to create within the image. Finally, close the HTML file and save it with the .html extension.

    Example of HTML Image Map

    This example creates an HTML image map where specific areas on the image (logo.png) link to different pages using the <area> tags within the <map> element.

    Open Compiler

    <!DOCTYPE html><html><head><title>Image Map Example</title></head><body><img src="/images/logo.png" usemap="#image_map"><map name="image_map"><!-- Define your clickable areas here --><area shape="circle" coords="45,32,49" href="/index.htm" alt="tutorialspoint"><area shape="rect" coords="245,50,85,9" href="/tutorialslibrary.htm" alt="tutorials_library"></map></body></html>
  • HTML – Images

    HTML images provide visual content for web pages, enhancing user experiences and conveying information. They can be photographs, graphics, icons, or illustrations.

    HTML offers various elements for embedding, manipulating, and controlling images, contributing to the aesthetics and functionality of websites. Understanding image tags, attributes, and responsive design principles is essential for effective web development.

    HTML Image Syntax

    The following is the basic syntax for HTML images:

    <img src="image_path" alt="Alternate text for the image" width="200px" height="150px" />

    Here,

    • src: The src attribute defines the path of the image (image URL).
    • alt: The alt attribute defines the alternate text; if there is a broken link to the image path, the alternate text displays on the webpage.
    • width and height: The width and height attribute define the height and width for the image.

    Insert Image

    You can insert (embed) an image on the webpage using the <img> tag with the src attribute, which is a required attribute to define the image path.

    Note: The <img> tag is an empty tag, which means that it can contain only a list of attributes and has no closing tag.

    Syntax

    Use the following syntax to insert an image using the <img> tag:

    <img src="Image URL" ... attributes-list/>

    Example

    To try the following example, let’s keep our HTML file test.htm and image file test.png in the same directory −

    Open Compiler

    <DOCTYPE html><html><head><title>Example of HTML Image (Insert on the webpage)</title></head><body><h1>Example of HTML Image (Insert on the webpage)</h1><img src="/html/images/test.png" alt="Test Image" /></body></html>

    You can use PNG, JPEG, or GIF image files based on your comfort, but make sure you specify the correct image file name in the src attribute. The image name is always case-sensitive.

    The alt attribute is an optional attribute but recommended as it specifies an alternate text for an image if the image cannot be displayed.

    Set Image Location

    Image location (path) must be clearly defined in the src attribute. You can follow the absolute path, which starts with root directory (/), then directory name (if any), and then image name with its extension.

    Example

    For example, if we have an image named “test.png” and it is stored in the “images” folder, which is in the “html” folder on the root directory. You can simply use an image path like “/html/images/test.png“.

    Open Compiler

    <!DOCTYPE html><html><head><title>Using Image in Webpage</title></head><body><img src="/html/images/test.png" alt="Test Image" /></body></html>

    Set Image Width and Height

    You can set image width and height based on your requirements using width and height attributes. You can specify the width and height of the image in terms of either pixels or a percentage of its actual size.

    Example

    The following example demonstrates how to set the width and height of an image:

    Open Compiler

    <!DOCTYPE html><html><head><title>Set Image Width and Height</title></head><body><p>Setting image width and height</p><img src="/html/images/test.png" alt="Test Image" width="450px" height="200px" /></body></html>

    Bordered Image

    You can specify the border and its thickness in terms of pixels using the border attribute. A thickness of 0 means there is no border around the picture.

    Example

    In the following example, we are specifying a border on an image:

    Open Compiler

    <!DOCTYPE html><html><head><title>Set Image Border</title></head><body><p>Setting image Border</p><img src="/html/images/test.png" alt="Test Image" border="3" /></body></html>

    Image Alignment

    By default, the image will align at the left side of the page, but you can use the align attribute to set it in the center or right.

    Example

    In the following example, we are specifying right align to an image:

    Open Compiler

    <!DOCTYPE html><html><head><title>Set Image Alignment</title></head><body><p>Setting image Alignment</p><img src="/html/images/test.png" alt="Test Image" border="3" align="right" /></body></html>

    Animated Images

    You can also use animated images (having .gif extensions) on the webpages. There is no specific attribute required to show animated images; you can simply set the path of the animated image (.gif) in the src attribute.

    Animated Image

    Example

    The following example demonstrates how you can insert an animated image:

    Open Compiler

    <!DOCTYPE html><html><head><title>Using Animated Images in HTML</title></head><body><h1>Using Animated Images in HTML</h1><img src="/html/images/animated_image.gif" alt="Animated Images"  /></body></html>

    Responsive Images

    You can also make the images responsive, which will automatically adjust their size based on the devices screen size and resolution. The following are the methods to make images responsive:

    1. Using CSS

    Using CSS, you can set the width of the image to 100%, which allows the image to scale proportionally to its parent container.

    <img src="/html/images/test.png" alt="Responsive Image" style="width: 100%; height: auto;"/>

    2. Using the <picture> Tag

    You can also display different images in different sizes or resolutions by using the <picture> tag, which is useful when you want to display different images based on the device.

    <picture><source media="(min-width: 800px)" srcset="image_path_1"><source media="(max-width: 799px)" srcset="image_path_2"><img src="default_image_path.jpg" alt="Responsive Image"></picture>

    Example

    The following example demonstrates how you can define a responsive image to display on the webpage:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Responsive Image Example</title><style>
    
        img {
            width: 100%;
            height: auto;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Responsive Image Example&lt;/h1&gt;&lt;img src="/html/images/test.png" alt="A responsive example image" /&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Supported Image Formats

    The following table shows the supported image formats in the HTML <img> tag:

    Image FormatImage Format NameTransparency SupportAnimation SupportFile Extensions
    JPEG/JPGJoint Photographic Experts GroupNoNo.jpg, .jpeg
    PNGPortable Network GraphicsYesNo.png
    GIFGraphics Interchange FormatYesYes.gif
    SVGScalable Vector GraphicsYesNo.svg
    WebPWeb Picture formatYesYes.webp
    BMPBitmap Image FileNoNo.bmp
    ICOIcon FileYesNo.ico
  • HTML – Colors

    HTML colors are a way of specifying the appearance of web elements. They are very important aspects of web design, as they not only enhance the visual appeal but also influence user behavior. They are also used to evoke emotions and highlight important content.

    In HTML, colors are generally defined for backgrounds, borders, and texts. To specify the colors, the style attribute of HTML elements is used.

    HTML Color Coding Methods

    The following three methods are used to set colors in a web page −

    • Color names − We can specify color names directly, like green, blue, or red.
    • Hex codes − A six-digit code representing the amount of red, green, and blue that makes up the color.
    • Color decimal or percentage values − This value is specified using the rgb() property.

    We will learn these color coding methods in upcoming chapters.

    HTML Color Names

    We can specify a direct color name to set text or background color. W3C has listed 16 basic color names that will validate with an HTML validator, but there are over 200 different color names supported by major browsers.

    Find the full list of the supported color names in HTML:HTML Color Names

    W3C Standard 16 Colors

    The following are the W3C standard color names that are supported by all browsers:

    BlackGraySilverWhite
    YellowLimeAquaFuchsia
    RedGreenBluePurple
    MaroonOliveNavyTeal

    Example

    Here is an example showing how you can use the color names in an HTML document:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Colors by Name</title></head><body text="blue" bgcolor="green"><p>Use different color names for for body and table and see the result.</p><table bgcolor="black"><tr><td><font color="white">This text will appear white on black background.</font></td></tr></table></body></html>

    Browser Safe Colors

    Colors can also be specified using the web-safe colors, which are formed by the combination of red, green, and blue (RGB) hex values.

    Here is a list of 216 colors that are considered the safest and most consistent across different computers. These colors range from the hexadecimal code #000000 (black) to #FFFFFF (white) and are supported by all computers using a 256-color palette.

    0000000000330000660000990000CC0000FF
    0033000033330033660033990033CC0033FF
    0066000066330066660066990066CC0066FF
    0099000099330099660099990099CC0099FF
    00CC0000CC3300CC6600CC9900CCCC00CCFF
    00FF0000FF3300FF6600FF9900FFCC00FFFF
    3300003300333300663300993300CC3300FF
    3333003333333333663333993333CC3333FF
    3366003366333366663366993366CC3366FF
    3399003399333399663399993399CC3399FF
    33CC0033CC3333CC6633CC9933CCCC33CCFF
    33FF0033FF3333FF6633FF9933FFCC33FFFF
    6600006600336600666600996600CC6600FF
    6633006633336633666633996633CC6633FF
    6666006666336666666666996666CC6666FF
    6699006699336699666699996699CC6699FF
    66CC0066CC3366CC6666CC9966CCCC66CCFF
    66FF0066FF3366FF6666FF9966FFCC66FFFF
    9900009900339900669900999900CC9900FF
    9933009933339933669933999933CC9933FF
    9966009966339966669966999966CC9966FF
    9999009999339999669999999999CC9999FF
    99CC0099CC3399CC6699CC9999CCCC99CCFF
    99FF0099FF3399FF6699FF9999FFCC99FFFF
    CC0000CC0033CC0066CC0099CC00CCCC00FF
    CC3300CC3333CC3366CC3399CC33CCCC33FF
    CC6600CC6633CC6666CC6699CC66CCCC66FF
    CC9900CC9933CC9966CC9999CC99CCCC99FF
    CCCC00CCCC33CCCC66CCCC99CCCCCCCCCCFF
    CCFF00CCFF33CCFF66CCFF99CCFFCCCCFFFF
    FF0000FF0033FF0066FF0099FF00CCFF00FF
    FF3300FF3333FF3366FF3399FF33CCFF33FF
    FF6600FF6633FF6666FF6699FF66CCFF66FF
    FF9900FF9933FF9966FF9999FF99CCFF99FF
    FFCC00FFCC33FFCC66FFCC99FFCCCCFFCCFF
    FFFF00FFFF33FFFF66FFFF99FFFFCCFFFFFF

    Setting Text Color

    To set the text color in HTML, we specify the color value of text inside the color property of style attribute. The value of color can be specified using any of the above discussed methods.

    Example

    The following example shows how to set color for a text using color name method:

    Open Compiler

    <!DOCTYPE html><html><head><title>Setting HTML Text Color </title></head><body><h2 style="color: Fuchsia;">Setting Text Color in HTML</h3><p style="color: Teal;">This line is created using paragraph tag and its color is <b>Teal</b>. </p><p style="color: SlateBlue;">This is another line created using paragraph tag and its color is <b>SlateBlue</b>. </p></body></html>

    The above HTML code will generate one heading and two different paragraphs with distinct colors.

    Setting Background Color

    To set the background color for any HTML element, we specify the color value inside the background-color property of the style attribute. The value of color can be specified using any of the above-discussed methods.

    Example

    The following example shows how to set background color for HTML elements using color name method:

    Open Compiler

    <!DOCTYPE html><html><head><title>Background Color </title></head><body><h2 style="background-color: Fuchsia;">Setting Background Color in HTML</h3><p style="background-color: Teal;">This line is created using paragraph tag and its background color is <b>Teal</b>.</p><p style="background-color: SlateBlue;">This is another line created using paragraph tag and its background color is <b>SlateBlue</b>.</p></body></html>

    On executing the above HTML code, it will generate one heading and two different paragraphs with distinct background colors.

    Setting Border Color

    To set the border color for any HTML element, we specify the color value inside the border property of the style attribute. The value of color can be specified using any of the above-discussed methods.

    Example

    In the following example, we are going to set border color for HTML elements using color name method:

    Open Compiler

    <!DOCTYPE html><html><head><title>Border Color </title></head><body><h2 style="border: 2.5px solid Fuchsia;">Setting Border Color in HTML</h3><p style="border: 2.5px dotted Teal;">This line is created using paragraph tag and its border color is <b>Teal</b>. </p><p style="border: 2.5px dashed SlateBlue;">This is another line created using paragraph tag and its border color is <b>SlateBlue</b>. </p></body></html>

    When we execute the above HTML code, it will generate one heading and two different paragraphs with distinct border colors.