Author: saqibkhan

  • CSS Syntax

    Understanding CSS Syntax

    A CSS stylesheet consists of a set of rules that are interpreted by the web browser and then applied to the corresponding elements such as paragraphs, headings, etc. in the document.

    A CSS rule have two main parts, a selector and one or more declarations:

    CSS Selector Syntax Illustration

    The selector specifies which element or elements in the HTML page the CSS rule applies to.

    Whereas, the declarations within the block determines how the elements are formatted on a webpage. Each declaration consists of a property and a value separated by a colon (:) and ending with a semicolon (;), and the declaration groups are surrounded by curly braces {}.

    The property is the style attribute you want to change; they could be font, color, background, etc. Each property has a value, for example color property can have value either blue or #0000FF etc.

    h1 {color:blue; text-align:center;}

    To make the CSS more readable, you can put one declaration on each line, like this:

    Example

    h1 {
    
    color: blue;
    text-align: center;
    }

    In the example above h1 is a selector, color and text-align are the CSS properties, and the given blue and center are the corresponding values of these properties.

    Note: A CSS declaration always ends with a semicolon “;“, and the declaration groups are always surrounded by the curly brackets “{}“.


    Writing Comments in CSS

    Comments are usually added with the purpose of making the source code easier to understand. It may help other developer (or you in the future when you edit the source code) to understand what you were trying to do with the CSS. Comments are significant to programmers but ignored by browsers.

    A CSS comment begins with /*, and ends with */, as shown in the example below:

    Example

    /* This is a CSS comment */
    h1 {
    
    color: blue;
    text-align: center;
    } /* This is a multi-line CSS comment that spans across more than one line */ p {
    font-size: 18px;
    text-transform: uppercase;
    }

    You can also comment out part of your CSS code for debugging purpose, as shown here:

    Example

    h1 {
    
    color: blue;
    text-align: center;
    } /* p {
    font-size: 18px;
    text-transform: uppercase;
    } */

    Case Sensitivity in CSS

    CSS property names and many values are not case-sensitive. Whereas, CSS selectors are usually case-sensitive, for instance, the class selector .maincontent is not the same as .mainContent.

    Therefore, to be on safer side, you should assume that all components of CSS rules are case-sensitive.

    You will learn about the various types of CSS selectors in the next chapter.

  • CSS Getting Started

    Getting Started with CSS

    In this tutorial you’ll learn how easy it is to add style and formatting information to the web pages using CSS. But, before we begin, make sure that you have some working knowledge of HTML.

    If you’re just starting out in the world of web development, start learning from here »

    Without further ado, let’s get started with the Cascading Style Sheets (CSS).

    Including CSS in HTML Documents

    CSS can either be attached as a separate document or embedded in the HTML document itself. There are three methods of including CSS in an HTML document:

    • Inline styles — Using the style attribute in the HTML start tag.
    • Embedded styles — Using the <style> element in the head section of a document.
    • External style sheets — Using the <link> element, pointing to an external CSS file.

    In this tutorial we will cover all these three methods for inserting CSS one by one.

    Note: The inline styles have the highest priority, and the external style sheets have the lowest. It means if you specify styles for an element in both embedded and external style sheets, the conflicting style rules in the embedded style sheet would override the external style sheet.

    Inline Styles

    Inline styles are used to apply the unique style rules to an element by putting the CSS rules directly into the start tag. It can be attached to an element using the style attribute.

    The style attribute includes a series of CSS property and value pairs. Each "property: value" pair is separated by a semicolon (;), just as you would write into an embedded or external style sheets. But it needs to be all in one line i.e. no line break after the semicolon, as shown here:

    Example

    <h1 style="color:red; font-size:30px;">This is a heading</h1>
    <p style="color:green; font-size:22px;">This is a paragraph.</p>
    <div style="color:blue; font-size:14px;">This is some text content.</div>

    Using the inline styles are generally considered as a bad practice. As style rules are embedded directly inside the HTML tag, it causes the presentation to become mixed with the content of the document; which makes the code hard to maintain and negates the purpose of using CSS.

    Note: It’s become impossible to style pseudo-elements and pseudo-classes with inline styles. You should, therefore, avoid the use of style attributes in your code. Using external style sheets is the preferred way to add styles to the HTML documents.


    Embedded Style Sheets

    Embedded or internal style sheets only affect the document they are embedded in.

    Embedded style sheets are defined in the <head> section of an HTML document using the <style> element. You can define any number of <style> elements in an HTML document but they must appear between the <head> and </head> tags. Let’s take a look at an example:

    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;title&gt;My HTML Document&lt;/title&gt;
    &lt;style&gt;
        body { background-color: YellowGreen; }
        p { color: #fff; }
    &lt;/style&gt;
    </head> <body>
    &lt;h1&gt;This is a heading&lt;/h1&gt;
    &lt;p&gt;This is a paragraph of text.&lt;/p&gt;
    </body> </html>

    Tip: The type attribute of the <style> and <link> tag (i.e. type="text/css") defines the language of the style sheet. This attribute is purely informative. You can omit this since CSS is the standard and default style sheet language in HTML5.


    External Style Sheets

    An external style sheet is ideal when the style is applied to many pages of the website.

    An external style sheet holds all the style rules in a separate document that you can link from any HTML file on your site. External style sheets are the most flexible because with an external style sheet, you can change the look of an entire website by changing just one file.

    You can attach external style sheets in two ways — linking and importing.

    Linking External Style Sheets

    Before linking, we need to create a style sheet first. Let’s open your favorite code editor and create a new file. Now type the following CSS code inside this file and save it as “style.css”.

    Example

    body {
    
    background: lightyellow;
    font: 18px Arial, sans-serif;
    } h1 {
    color: orange;
    }

    An external style sheet can be linked to an HTML document using the <link> tag. The <link> tag goes inside the <head> section, as you can see in the following example:

    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;title&gt;My HTML Document&lt;/title&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;
    </head> <body>
    &lt;h1&gt;This is a heading&lt;/h1&gt;
    &lt;p&gt;This is a paragraph of text.&lt;/p&gt;
    </body> </html>

    Tip: Among all the three methods, using external style sheet is the best method for defining and applying styles to the HTML documents. As you can clearly see with external style sheets, the affected HTML file require minimal changes in the markup.

    Importing External Style Sheets

    The @import rule is another way of loading an external style sheet. The @import statement instructs the browser to load an external style sheet and use its styles.

    You can use it in two ways. The simplest is within the header of your document. Note that, other CSS rules may still be included in the <style> element. Here’s an example:

    Example

    <style>
    
    @import url("css/style.css");
    p {
        color: blue;
        font-size: 16px;
    }
    </style>

    Similarly, you can use the @import rule to import a style sheet within another style sheet.

    Example

    @import url("css/layout.css");
    @import url("css/color.css");
    body {
    
    color: blue;
    font-size: 14px;
    }
  • CSS Tutorial

    CSS stands for Cascading Style Sheets. CSS is a standard style sheet language used for describing the presentation (i.e. the layout and formatting) of the web pages.

    Prior to CSS, nearly all of the presentational attributes of HTML documents were contained within the HTML markup (specifically inside the HTML tags); all the font colors, background styles, element alignments, borders and sizes had to be explicitly described within the HTML.

    As a result, development of the large websites became a long and expensive process, since the style information were repeatedly added to every single page of the website.

    To solve this problem CSS was introduced in 1996 by the World Wide Web Consortium (W3C), which also maintains its standard. CSS was designed to enable the separation of presentation and content. Now web designers can move the formatting information of the web pages to a separate style sheet which results in considerably simpler HTML markup, and better maintainability.

    CSS3 is the latest version of the CSS specification. CSS3 adds several new styling features and improvements to enhance the web presentation capabilities.

    Note: Our CSS tutorial will help you to learn the fundamentals of the latest CSS3 language, from the basic to advanced topics step-by-step. If you’re a beginner, start with the basic section and gradually move forward by learning a little bit every day.


    What You Can Do with CSS

    There are lot more things you can do with CSS.

    • You can easily apply same style rules on multiple elements.
    • You can control the presentation of multiple pages of a website with a single style sheet.
    • You can present the same page differently on different devices.
    • You can style dynamic states of elements such as hover, focus, etc. that isn’t possible otherwise.
    • You can change the position of an element on a web page without changing the markup.
    • You can alter the display of existing HTML elements.
    • You can transform elements like scale, rotate, skew, etc. in 2D or 3D space.
    • You can create animations and transitions effects without using any JavaScript.
    • You can create print friendly version of your web pages.

    The list does not end here, there are many other interesting things that you can do with CSS. You will learn about all of them in detail in upcoming chapters.


    Advantages of Using CSS

    The biggest advantage of CSS is that it allows the separation of style and layout from the content of the document. Here are some more advantages, why one should start using CSS?

    • CSS Save Lots of Time — CSS gives lots of flexibility to set the style properties of an element. You can write CSS once; and then the same code can be applied to the groups of HTML elements, and can also be reused in multiple HTML pages.
    • Easy Maintenance — CSS provides an easy means to update the formatting of the documents, and to maintain the consistency across multiple documents. Because the content of the entire set of web pages can be easily controlled using one or more style sheets.
    • Pages Load Faster — CSS enables multiple pages to share the formatting information, which reduces complexity and repetition in the structural contents of the documents. It significantly reduces the file transfer size, which results in a faster page loading.
    • Superior Styles to HTML — CSS has much wider presentation capabilities than HTML and provide much better control over the layout of your web pages. So you can give far better look to your web pages in comparison to the HTML presentational elements and attributes.
    • Multiple Device Compatibility — CSS also allows web pages to be optimized for more than one type of device or media. Using CSS the same HTML document can be presented in different viewing styles for different rendering devices such as desktop, cell phones, etc.

    Tip: Now most of the HTML attributes are being deprecated and it’s not recommended to use. So it’s a good idea to use as much CSS as possible to increase the adaptability your website and make them compatible to future browsers, as well.


    What This Tutorial Covers

    This CSS tutorial series covers all the fundamentals of CSS, including the idea of selectors, methods of setting colors and backgrounds, way of formatting fonts and text, styling UI elements such as hyperlinks, lists, tables, etc. as well as the concept of CSS box model, and so on.

    Once you’re comfortable with the basics, you’ll move on to next level that explains the way of setting dimension and alignment of elements, methods for positioning elements on a web page, using image sprites, as well as the concept of relative and absolute units, visual formatting model, display and visibility, layers, pseudo classes and elements, media dependent style sheets, and so on.

    Finally, you’ll explore some advanced features introduced in CSS3 like gradient colors, drop shadow effect, 2D and 3D transforms, alpha transparency, as well as the method of creating transition and animation effect, flex layouts, filter effect, the concept of media queries, and more.

    Tip: Every chapter in this tutorial contains lots of real-world examples that you can try and test using an online editor. These examples will help you to better understand the concept or topic. It also contains smart workarounds as well as useful tips and important notes.

  • HTML JavaScript

    A Script is a small program which is used with HTML to make web pages more attractive, dynamic and interactive, such as an alert popup window on mouse click. Currently, the most popular scripting language is JavaScript used for websites.

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <body>  
    
    <h1>JavaScript Date and Time example</h1>  
    
    <button type="button"  
    
    onclick="document.getElementById('demo').innerHTML = Date()">  
    
    Click me to display Date and Time.</button>  
    
    <p id="demo"></p>  
    
    </body>  
    
    </html>

    HTML <script> Tag

    The HTML <script> tag is used to specify a client-side script. It may be an internal or external JavaScript which contains scripting statements, hence we can place <script> tag within <body> or <head> section.

    It is mainly used to manipulate images, form validation and change content dynamically. JavaScript uses document.getElementById() method to select an HTML element.

    Example:

    
    
    1. <!DOCTYPE html>  
    2. <html>  
    3. <body>  
    4. <h2>Use JavaScript to Change Text</h2>  
    5. <p id="demo"></p>  
    6. <script>  
    7. document.getElementById("demo").innerHTML = "Hello JavaTpoint";  
    8. </script>  
    9. </body>  
    10. </html>

    HTML events with JavaScript

    An event is something which user does, or browser does such as mouse click or page loading are examples of events, and JavaScript comes in the role if we want something to happen on these events.

    HTML provides event handler attributes which work with JavaScript code and can perform some action on an event.

    Syntax:

    <element event = "JS code">   

    Example:

    <input type="button" value="Click" onclick="alert('Hi, how are you')"> 

    Output:

    Click Event Example

    Click on the button and you csn see a pop-up window with a message

    HTML can have following events such as:

    • Form events: reset, submit, etc.
    • Select events: text field, text area, etc.
    • Focus event: focus, blur, etc.
    • Mouse events: select, mouseup, mousemove, mousedown, click, dblclick, etc.

    Following are the list for Window event attributes:

    Event Event NameHandler NameOccurs when
    onBlurblurWhen form input loses focus
    onClickclickWhen the user clicks on a form element or a link
    onSubmitsubmitWhen user submits a form to the server.
    onLoadloadWhen page loads in a browser.
    onFocusfocusWhen user focuses on an input field.
    onSelectselectWhen user selects the form input filed.

    Note: You will learn more about JavaScript Events in our JavaScript tutorial.

    Let’s see what JavaScript can do:

    1) JavaScript can change HTML content.

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <body>  
    
    <p>JavaScript can change the content of an HTML element:</p>  
    
    <button type="button" onclick="myFunction()">Click Me!</button>  
    
    <p id="demo"></p>  
    
    <script>  
    
    function myFunction() {   
    
        document.getElementById("demo").innerHTML = "Hello JavaTpoint!";  
    
    }  
    
    </script>  
    
    </body>  
    
    </html>

    2) JavaScript can change HTML style

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <body>  
    
    <p id="demo">JavaScript can change the style of an HTML element.</p>  
    
    <script>  
    
    function myFunction() {  
    
        document.getElementById("demo").style.fontSize = "25px";   
    
        document.getElementById("demo").style.color = "brown";  
    
        document.getElementById("demo").style.backgroundColor = "lightgreen";         
    
    }  
    
    </script>  
    
    <button type="button" onclick="myFunction()">Click Me!</button>  
    
    </body>  
    
    </html>

    3) JavaScript can change HTML attributes.

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <body>  
    
    <script>  
    
    function light(sw) {  
    
        var pic;  
    
        if (sw == 0) {  
    
            pic = "pic_lightoff.png"  
    
        } else {  
    
            pic = "pic_lighton.png"  
    
        }  
    
        document.getElementById('myImage').src = pic;  
    
    }  
    
    </script>  
    
    <img id="myImage" src="pic_lightoff.png" width="100" height="180">  
    
    <p>  
    
    <button type="button" onclick="light(1)">Light On</button>  
    
    <button type="button" onclick="light(0)">Light Off</button>  
    
    </p>  
    
    </body>  
    
    </html>

    Use External Script

    Suppose, you have various HTML files which should have same script, then we can put our JavaScript code in separate file and can call in HTML file. Save JavaScript external files using .js extension.

    Note: Do not add <script> tag in the external file, and provide the complete path where you have put the JS file.

    Syntax:

    1. <script type=”text/javascript” src=”URL “></script>  

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
       <head>  
    
        <script type="text/javascript" src="external.js"></script>  
    
        </head>  
    
         <body>  
    
          <h2>External JavaScript Example</h2>  
    
           <form onsubmit="fun()">  
    
             <label>Enter your name:</label><br>  
    
          <input type="text" name="uname" id="frm1"><br>  
    
           <label>Enter your Email-address:</label><br>    
    
          <input type="email" name="email"><br>  
    
          <input type="submit">  
    
      </form>  
    
     </body>  
    
     </html>

    JavaScript code:

    function fun() {  
    
           var x = document.getElementById("frm1").value;  
    
            alert("Hi"+" "+x+ "you have successfully submitted the details");  
    
        }

    Output:

    HTML JavaScript

    HTML <noscript> Tag

    HTML <noscript> tag is used to write disabled script in the browser. The text written within <noscript></noscript> tag is not displayed on the browser.

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <body>  
    
    <p id="demo"></p>  
    
    <script>  
    
    document.getElementById("demo").innerHTML = "Hello JavaScript!";  
    
    </script>  
    
    <noscript>This text is not visible in the browser.</noscript>  
    
    </body>  
    
    </html>
  • HTML iframes

    HTML Iframe is used to display a nested webpage (a webpage within a webpage). The HTML <iframe> tag defines an inline frame, hence it is also called as an Inline frame.

    An HTML iframe embeds another document within the current HTML document in the rectangular region.

    The webpage content and iframe contents can interact with each other using JavaScript.

    Iframe Syntax

    An HTML iframe is defined with the <iframe> tag:

    <iframe src="URL"></iframe>  

    Here, “src” attribute specifies the web address (URL) of the inline frame page.

    Set Width and Height of iframe

    You can set the width and height of iframe by using “width” and “height” attributes. By default, the attributes values are specified in pixels but you can also set them in percent. i.e. 50%, 60% etc.

    Example: (Pixels)

    <!DOCTYPE html>    
    
    <html>    
    
    <body>    
    
    <h2>HTML Iframes example</h2>    
    
    <p>Use the height and width attributes to specify the size of the iframe:</p>    
    
    <iframe src="https://www.javatpoint.com/" height="300" width="400"></iframe>    
    
    </body>    
    
    </html>

    Example: (Percentage)

    <!DOCTYPE html>    
    
    <html>    
    
    <body>    
    
    <h2>HTML Iframes</h2>    
    
    <p>You can use the height and width attributes to specify the size of the iframe:</p>    
    
    <iframe src="https://www.javatpoint.com/" height="50%" width="70%"></iframe>    
    
    </body>    
    
    </html>

    You can also use CSS to set the height and width of the iframe.

    Example:

    
    
    1. <!DOCTYPE html>    
    2. <html>    
    3. <body>    
    4. <h2>HTML Iframes</h2>    
    5. <p>Use the CSS height and width properties to specify the size of the iframe:</p>    
    6. <iframe src="https://www.javatpoint.com/" style="height:300px;width:400px"></iframe>    
    7. </body>    
    8. </html> 

    Remove the border of iframe

    By default, an iframe contains a border around it. You can remove the border by using <style> attribute and CSS border property.

    Example:

    
    
    1. <!DOCTYPE html>    
    2. <html>    
    3. <body>    
    4. <h2>Remove the Iframe Border</h2>    
    5. <p>This iframe example doesn't have any border</p>     
    6. <iframe src="https://www.javatpoint.com/" style="border:none;"></iframe>    
    7. </body>    
    8. </html> 

    You can also change the size, color, style of the iframe’s border.

    Example:

    <!DOCTYPE html>    
    
    <html>    
    
    <body>    
    
    <h2>Custom Iframe Border</h2>    
    
    <iframe src="https://www.javatpoint.com/" style="border:2px solid tomato;"></iframe>    
    
    </body>    
    
    </html>

    Iframe Target for a link

    You can set a target frame for a link by using iframe. Your specified target attribute of the link must refer to the name attribute of the iframe.

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <body>  
    
      
    
    <h2>Iframe - Target for a Link</h2>  
    
    <iframe height="300px" width="100%" src="new.html" name="iframe_a"></iframe>  
    
    <p><a href="https://www.javatpoint.com" target="iframe_a">JavaTpoint.com</a></p>  
    
    <p>The name of iframe and link target must have same value else link will not open as a frame. </p>  
    
      
    
    </body>  
    
    </html>
    HTML iframes

    new.hmtl output code:

    <!DOCTYPE html> <html> <head> <style> p{ font-size: 50px; color: red;} </style> </head> <body style="background-color: #c7f15e;"> <p>This is a link below the ifarme click on link to open new iframe. </p> </body> </html>

    Embed YouTube video using iframe

    You can also add a YouTube video on your webpage using the <iframe> tag. The attached video will be played at your webpage and you can also set height, width, autoplay, and many more properties for the video.

    Following are some steps to add YouTube video on your webpage:

    • Goto YouTube video which you want to embed.
    • Click on SHARE ➦ under the video.
    • Click on Embed <> option.
    • Copy HTML code.
    • Paste the code in your HTML file
    • Change height, width, and other properties (as per requirement).

    Example:

    1. <iframe width=”550″ height=”315″ src=”https://www.youtube.com/embed/JHq3pL4cdy4″ frameborder=”0″ allow=”accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture” allowfullscreen style=”padding:20px;”></iframe>  
    2.         <iframe width=”550″ height=”315″ src=”https://www.youtube.com/embed/O5hShUO6wxs” frameborder=”0″ allow=”accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture” style=”padding:20px;”>></iframe> 

    Output:

    HTML iframes

    Attributes of <iframe>

    Attribute nameValueDescription
    allowfullscreenIf true then that frame can be opened in full screen.
    heightPixelsIt defines the height of the embedded iframe, and the default height is 150 px.
    nametextIt gives the name to the iframe. The name attribute is important if you want to create a link in one frame.
    frameborder1 or 0It defines whether iframe should have a border or not. (Not supported in HTML5).
    WidthPixelsIt defines the width of embedded frame, and default width is 300 px.
    srcURLThe src attribute is used to give the path name or file name which content to be loaded into iframe.
    sandbox
    This attribute is used to apply extra restrictions for the content of the frame
    allow-formsIt allows submission of the form if this keyword is not used then form submission is blocked.
    allow-popupsIt will enable popups, and if not applied then no popup will open.
    allow-scriptsIt will enable the script to run.
    allow-same-originIf this keyword is used then the embedded resource will be treated as downloaded from the same source.
    srcdocThe srcdoc attribute is used to show the HTML content in the inline iframe. It overrides the src attribute (if a browser supports).
    scrolling
    It indicates that browser should provide a scroll bar for the iframe or not. (Not supported in HTML5)
    autoScrollbar only shows if the content of iframe is larger than its dimensions.
    yesAlways shows scroll bar for the iframe.
    noNever shows scrollbar for the iframe.
  • HTML Id Attribute

    The id attribute is used to specify the unique ID for an element of the HTML document. It allocates the unique identifier which is used by the CSS and the JavaScript for performing certain tasks.

    Note: In the Cascading Style sheet (CSS), we can easily select an element with the specific id by using the # symbol followed by id.

    Note: JavaScript can access an element with the given ID by using the getElementById() method.

    Syntax

    <tag id="value">  

    Example 1: The following example describes how to use the id attribute in CSS document:

    <!DOCTYPE html>  
    
    <html>  
    
    <head>  
    
    <title>  
    
    Example of Id attribute in CSS  
    
    </title>  
    
    <style>  
    
    #Cars {  
    
    padding: 40px;  
    
    background-color: lightblue;  
    
    color: black;      
    
    text-align: center;  
    
    }   
    
      
    
    #Bikes  
    
    {  
    
    padding: 50px;  
    
    background-color: lightGreen;  
    
    text-align: center;  
    
    }  
    
    </style>  
    
    </head>  
    
    <body>  
    
    <p> Use CSS to style an element with the id: </p>  
    
    <h1 id="Cars"> Cars </h1>  
    
    <h1 id="Bikes"> Bikes </h1>  
    
    </body>  
    
    </html>

    Output:

    HTML Id Attribute

    Example 2: The following example describes how to use the ID attribute in JavaScript.

    <!DOCTYPE html>  
    
    <html>   
    
    <head>   
    
    <title> Date Attribute </title>   
    
    <script>   
    
    function viewdate() {   
    
    var x = document.getElementById("dob").value;   
    
    document.getElementById("demo").innerHTML = x;   
    
    </script>   
    
    </head>   
    
    <body>   
    
    Employee Name: <input type="text" placeholder="Your Good name"/>   
    
    <br>  
    
    <br>  
    
    Date of Joining:   
    
    <input type="date" id="dob">  
    
    <br>   
    
    <button onclick="viewdate()"> Submit   
    
    </button>   
    
    <br>  
    
    <h2 id="demo"> </h2>   
    
    </body>   
    
    </html>

    Output:

    HTML Id Attribute
  • HTML style using CSS

    Let’s suppose we have created our web page using a simple HTML code, and we want something which can present our page in a correct format, and visibly attractive. So to do this, we can style our web page with CSS (Cascading Stylesheet) properties.

    CSS is used to apply the style in the web page which is made up of HTML elements. It describes the look of the webpage.

    CSS provides various style properties such as background color, padding, margin, border-color, and many more, to style a webpage.

    Each property in CSS has a name-value pair, and each property is separated by a semicolon (;).

    Note: In this chapter, we have given a small overview of CSS. You will learn everything in depth about CSS in our CSS tutorial.

    Example:

    <body style="text-align: center;">  
    
          <h2 style="color: red;">Welcome to javaTpoint</h2>  
    
          <p style="color: blue; font-size: 25px; font-style: italic ;">This is a great website to learn technologies in very simple way. </p>  
    
    </body>

    In the above example, we have used a style attribute to provide some styling format to our code.

    Output:

    Welcome to javaTpoint

    This is a great website to learn technologies in very simple way.


    Three ways to apply CSS

    To use CSS with HTML document, there are three ways:

    • Inline CSS: Define CSS properties using style attribute in the HTML elements.
    • Internal or Embedded CSS: Define CSS using <style> tag in <head> section.
    • External CSS: Define all CSS property in a separate .css file, and then include the file with HTML file using tag in section.

    Inline CSS:

    Inline CSS is used to apply CSS in a single element. It can apply style uniquely in each element.

    To apply inline CSS, you need to use style attribute within HTML element. We can use as many properties as we want, but each property should be separated by a semicolon (;).

    Example:

    <h3 style="color: red;  
    
                font-style: italic;  
    
                text-align: center;  
    
                font-size: 50px;  
    
                padding-top: 25px;">Learning HTML using Inline CSS</h3>

    Output:

    Learning HTML using Inline CSS


    Internal CSS:

    An Internal stylesheets contains the CSS properties for a webpage in <head> section of HTML document. To use Internal CSS, we can use class and id attributes.

    We can use internal CSS to apply a style for a single HTML page.

    Example:

    
    
    1. <!DOCTYPE html>  
    2. <html>  
    3. <head>  
    4.                   <style>  
    5.       /*Internal CSS using element name*/  
    6.             body{background-color:lavender;  
    7.              text-align: center;}  
    8.              h2{font-style: italic;  
    9.               font-size: 30px;  
    10.               color: #f08080;}  
    11.             p{font-size: 20px;}  
    12.         /*Internal CSS using class name*/  
    13.             .blue{color: blue;}  
    14.             .red{color: red;}  
    15.             .green{color: green;}  
    16.       </style>  
    17.     </head>  
    18.   <body>  
    19.    <h2>Learning HTML with internal CSS</h2>  
    20.     <p class="blue">This is a blue color paragraph</p>  
    21.     <p class="red">This is a red color paragraph</p>  
    22.     <p class="green">This is a green color paragraph</p>  
    23.   </body>  
    24. </html>

    Note: In the above example, we have used a class attribute which you will learn in the next chapter.


    External CSS:

    An external CSS contains a separate CSS file which only contains style code using the class name, id name, tag name, etc. We can use this CSS file in any HTML file by including it in HTML file using <link> tag.

    If we have multiple HTML pages for an application and which use similar CSS, then we can use external CSS.

    There are two files need to create to apply external CSS

    • First, create the HTML file
    • Create a CSS file and save it using the .css extension (This file only will only contain the styling code.)
    • Link the CSS file in your HTML file using tag in header section of HTML document.

    Example:

    <!DOCTYPE html>  
    
    <html>  
    
    <head>  
    
        <link rel="stylesheet" type="text/css" href="style.css">  
    
        </head>  
    
      <body>  
    
       <h2>Learning HTML with External CSS</h2>  
    
        <p class="blue">This is a blue color paragraph</p>  
    
        <p class="red">This is a red color paragraph</p>  
    
        <p class="green">This is a green color paragraph</p>  
    
      </body>  
    
    </html>

    CSS file:

    body{
    background-color:lavender;
    text-align: center;
    }
    h2{
    font-style: italic;
    size: 30px;
    color: #f08080;
    }
    p{
    font-size: 20px;
    }
    .blue{
    color: blue;
    }
    .red{
    color: red;
    }
    .green{
    color: green;
    }

    Commonly used CSS properties:

    Properties-nameSyntaxDescription
    background-colorbackground-color:red;It defines the background color of that element.
    colorcolor: lightgreen;It defines the color of text of an element
    paddingpadding: 20px;It defines the space between content and the border.
    marginmargin: 30px; margin-left:It creates space around an element.
    font-familyfont-family: cursive;Font-family defines a font for a particular element.
    Font-sizefont-size: 50px;Font-size defines a font size for a particular element.
    text-aligntext-align: left;It is used to align the text in a selected position.
  • HTML form Attribute

    HTML <form> element attributes

    In HTML there are various attributes available for <form> element which are given below:

    HTML action attribute

    The action attribute of <form> element defines the process to be performed on form when form is submitted, or it is a URI to process the form information.

    The action attribute value defines the web page where information proceed. It can be .php, .jsp, .asp, etc. or any URL where you want to process your form.

    Note: If action attribute value is blank then form will be processed to the same page.

    Example:

    <form action="action.html" method="post">  
    
    <label>User Name:</label><br>  
    
    <input type="text" name="name"><br><br>  
    
    <label>User Password</label><br>  
    
    <input type="password" name="pass"><br><br>  
    
     <input type="submit">  
    
       </form>

    Output:

    Demo of action attribute of form element

    User Name:

    User Password

    It will redirect to a new page “action.html” when you click on submit button


    HTML method attribute

    The method attribute defines the HTTP method which browser used to submit the form. The possible values of method attribute can be:

    • post: We can use the post value of method attribute when we want to process the sensitive data as it does not display the submitted data in URL.

    Example:

    <form action="action.html" method="post">  
    • get: The get value of method attribute is default value while submitting the form. But this is not secure as it displays data in URL after submitting the form.

    AD

    Example:

    1. <form action=”action.html” method=”get”>  

    When submitting the data, it will display the entered data in the form of:

    1. file:///D:/HTML/action.html?name=JavaTPoint&pass=123  

    HTML target attribute

    The target attribute defines where to open the response after submitting the form. The following are the keywords used with the target attribute.

    • _self: If we use _self as an attribute value, then the response will display in current page only.

    Example:

    1. <form action=”action.html” method=”get” target=”_self”>  
    • _blank: If we use _blank as an attribute it will load the response in a new page.

    Example:

    <form action="action.html" method="get" target="_blank">  

    HTML autocomplete attribute

    The HTML autocomplete attribute is a newly added attribute of HTML5 which enables an input field to complete automatically. It can have two values “on” and “off” which enables autocomplete either ON or OFF. The default value of autocomplete attribute is “on”.

    Example:

    <form action="action.html" method="get" autocomplete="on">  

    Example:

    <form action="action.html" method="get" autocomplete="off">  

    Note: it can be used with <form> element and <input> element both.


    HTML enctype attribute

    The HTML enctype attribute defines the encoding type of form-content while submitting the form to the server. The possible values of enctype can be:

    • application/x-www-form-urlencoded: It is default encoding type if the enctype attribute is not included in the form. All characters are encoded before submitting the form.

    Example:

    1. <form action=”action.html” method=”post” enctype=”application/x-www-form-urlencoded” >  
    • multipart/form-data: It does not encode any character. It is used when our form contains file-upload controls.

    Example:

    1. <form action=”action.html” method=”post” enctype=”multipart/form-data”>  
    • text/plain (HTML5): In this encoding type only space are encoded into + symbol and no any other special character encoded.

    Example:

    <form action="action.html" method="post" enctype="text/plain" >  

    HTML novalidate attribute HTML5

    The novalidate attribute is newly added Boolean attribute of HTML5. If we apply this attribute in form then it does not perform any type of validation and submit the form.

    Example:

    <form action = "action.html" method = "get" novalidate>  

    Output:

    Fill the form

    Enter name:

    Enter age:

    Enter email:

    Try to change the form detials with novalidate atttribute and without novalidate attribute and see the difference.


    HTML <input> element attribute

    HTML name attribute

    The HTML name attribute defines the name of an input element. The name and value attribute are included in HTTP request when we submit the form.

    Note: One should not omit the name attribute as when we submit the form the HTTP request includes both name-value pair and if name is not available it will not process that input field.

    Example:

    <form action = "action.html" method = "get">  
    
             Enter name:<br><input type="name" name="uname"><br>  
    
             Enter age:<br><input type="number" name="age"><br>  
    
             Enter email:<br><input type="email"><br>  
    
             <input type="submit" value="Submit">  
    
          </form>

    Output:

    Fill the form

    Enter name:

    Enter age:

    Enter email:

    Note: If you will not use name attribute in any input field, then that input field will not be submitted, when submit the form.

    Click on submit and see the URL where email is not included in HTTP request as we have not used name attribute in the email input field


    HTML value attribute

    The HTML value attribute defines the initial value or default value of an input field.

    Example:

    <form>  
    
            <label>Enter your Name</label><br>  
    
            <input type="text" name="uname" value="Enter Name"><br><br>  
    
            <label>Enter your Email-address</label><br>  
    
            <input type="text" name="uname" value="Enter email"><br><br>  
    
              <label>Enter your password</label><br>  
    
            <input type="password" name="pass" value=""><br><br>  
    
            <input type="submit" value="login">  
    
       </form>

    Output:

    Fill the form

    Enter your Name

    Enter your Email-address

    Enter your password

    Note: In password input filed the value attribute will always unclear


    HTML required attribute HTML5

    HTML required is a Boolean attribute which specifies that user must fill that filed before submitting the form.

    Example:

    <form>  
    
            <label>Enter your Email-address</label><br>  
    
            <input type="text" name="uname" required><br><br>  
    
             <label>Enter your password</label><br>  
    
            <input type="password" name="pass"><br><br>  
    
            <input type="submit" value="login">  
    
       </form>

    Output:

    Fill the form

    Enter your Email-address

    Enter your password

    If you will try to submit the form without completing email field then it will give an error pop up.


    HTML autofocus attribute HTML5

    The autofocus is a Boolean attribute which enables a field automatically focused when a webpage loads.

    Example:

    
    
    1. <form>  
    2.         <label>Enter your Email-address</label><br>  
    3.         <input type="text" name="uname" autofocus><br><br>  
    4.          <label>Enter your password</label><br>  
    5.         <input type="password" name="pass"><br><br>  
    6.         <input type="submit" value="login">  
    7.    </form>

    HTML placeholder attribute HTML5

    The placeholder attribute specifies a text within an input field which informs the user about the expected input of that filed.

    The placeholder attribute can be used with text, password, email, and URL values.

    When the user enters the value, the placeholder will be automatically removed.

    Example:

    
    
    1. <form>  
    2.         <label>Enter your name</label><br>  
    3.         <input type="text" name="uname" placeholder="Your name"><br><br>  
    4.             <label>Enter your Email address</label><br>  
    5.         <input type="email" name="email" placeholder="[email protected]"><br><br>  
    6.             <label>Enter your password</label><br>  
    7.         <input type="password" name="pass" placeholder="your password"><br><br>  
    8.         <input type="submit" value="login">  
    9.     </form>

    Output:

    Registration form

    Enter your name

    Enter your Email address

    Enter your password


    HTML disabled attribute

    The HTML disabled attribute when applied then it disable that input field. The disabled field does not allow the user to interact with that field.

    The disabled input filed does not receive click events, and these input value will not be sent to the server when submitting the form.

    Example:

    <input type="text" name="uname" disabled><br><br>  

    Output:

    Registration form

    Enter User name

    Enter your Email address

    Enter your password


    HTML size attribute

    The size attribute controls the size of the input field in typed characters.

    Example:

    
    
    1. <label>Account holder name</label><br>  
    2.         <input type="text" name="uname" size="40" required><br><br>  
    3.         <label>Account number</label><br>  
    4.         <input type="text" name="an" size="30" required><br><br>  
    5.         <label>CVV</label><br>  
    6.         <input type="text" name="cvv"  size="1" required><br><br>

    Output:

    Registration form with disbaled attribute

    Account holder name

    Account number

    CVV


    HTML form attribute

    HTML form attribute allows a user to specify an input filed outside the form but remains the part of the parent form.

    Example:

    
    
    1. User email: <br><input type="email" name="email"  form="fcontrol"  required><br>  
    2.          <input type="submit" form="fcontrol">

    Output:User Name:

    User password:

    The email field is outside the form but still it will remain part of the formUser email:

  • HTML Form Input Types

    In HTML <input type=” “> is an important element of HTML form. The “type” attribute of input element can be various types, which defines information field. Such as <input type=”text” name=”name”> gives a text box.

    Following is a list of all types of <input> element of HTML.

    type=” “Description
    textDefines a one-line text input field
    passwordDefines a one-line password input field
    submitDefines a submit button to submit the form to server
    resetDefines a reset button to reset all values in the form.
    radioDefines a radio button which allows select one option.
    checkboxDefines checkboxes which allow select multiple options form.
    buttonDefines a simple push button, which can be programmed to perform a task on an event.
    fileDefines to select the file from device storage.
    imageDefines a graphical submit button.

    HTML5 added new types on <input> element. Following is the list of types of elements of HTML5

    type=” “Description
    colorDefines an input field with a specific color.
    dateDefines an input field for selection of date.
    datetime-localDefines an input field for entering a date without time zone.
    emailDefines an input field for entering an email address.
    monthDefines a control with month and year, without time zone.
    numberDefines an input field to enter a number.
    urlDefines a field for entering URL
    weekDefines a field to enter the date with week-year, without time zone.
    searchDefines a single line text field for entering a search string.
    telDefines an input field for entering the telephone number.

    Following is the description about types of <input> element with examples.

    1. <input type=”text”>:

    <input> element of type “text” are used to define a single-line input text field.

    Example:

    <form>  
    
        <label>Enter first name</label><br>  
    
        <input type="text" name="firstname"><br>  
    
        <label>Enter last name</label><br>  
    
        <input type="text" name="lastname"><br>  
    
        <p><strong>Note:</strong>The default maximum cahracter lenght is 20.</p>  
    
    </form>

    Output:

    Input “text” type:

    The “text”field defines a sinlge line input text field.Enter first name

    Enter last name

    Note:The default maximum cahracter lenght is 20.


    2. <input type=”password”>:

    The <input> element of type “password” allow a user to enter the password securely in a webpage. The entered text in password filed converted into “*” or “.”, so that it cannot be read by another user.

    Example:

    <form>  
    
        <label>Enter User name</label><br>  
    
        <input type="text" name="firstname"><br>  
    
        <label>Enter Password</label><br>  
    
        <input type="Password" name="password"><br>  
    
        <br><input type="submit" value="submit">  
    
    </form>

    Output:

    Input “password” type:

    The “password”field defines a sinlge line input password field to enter the password securely.Enter User name

    Enter Password


    3. <input type=”submit”>:

    The <input> element of type “submit” defines a submit button to submit the form to the server when the “click” event occurs.

    Example:

    <form action="https://www.javatpoint.com/html-tutorial">  
    
        <label>Enter User name</label><br>  
    
        <input type="text" name="firstname"><br>  
    
        <label>Enter Password</label><br>  
    
        <input type="Password" name="password"><br>  
    
        <br><input type="submit" value="submit">  
    
    </form>

    Output:

    Input “submit” type:

    Enter User name

    Enter Password

    After clicking on submit button, this will submit the form to server and will redirect the page to action value.We will learn about “action” attribute in later chapters


    4. <input type=”reset”>:

    The <input> type “reset” is also defined as a button but when the user performs a click event, it by default reset the all inputted values.

    Example:

    
    
    1. <form>  
    2.     <label>User id: </label>  
    3.      <input type="text" name="user-id" value="user">  
    4.               <label>Password: </label>  
    5.      <input type="password" name="pass" value="pass"><br><br>   
    6.      <input type="submit" value="login">  
    7.       <input type="reset" value="Reset">  
    8. </form>

    Output:

    Input “reset” type:

    User id:  Password: 

    Try to change the input values of user id and password, then when you click on reset, it will reset input fields with default values.


    5. <input type=”radio”>:

    The <input> type “radio” defines the radio buttons, which allow choosing an option between a set of related options. At a time only one radio button option can be selected at a time.

    Example:

    
    
    1. <form>  
    2.   <p>Kindly Select your favorite color</p>  
    3.   <input type="radio" name="color" value="red"> Red <br>  
    4.   <input type="radio" name="color" value="blue"> blue <br>  
    5.   <input type="radio" name="color" value="green">green <br>  
    6.   <input type="radio" name="color" value="pink">pink <br>  
    7.   <input type="submit" value="submit">  
    8. </form>

    Output:

    Input “radio” type

    Kindly Select your favorite color Red
     blue
    green
    pink


    6. <input type=”checkbox”>:

    The <input> type “checkbox” are displayed as square boxes which can be checked or unchecked to select the choices from the given options.

    Note: The “radio” buttons are similar to checkboxes, but there is an important difference between both types: radio buttons allow the user to select only one option at a time, whereas checkbox allows a user to select zero to multiple options at a time.

    Example:

    <form>   
    
          <label>Enter your Name:</label>  
    
          <input type="text" name="name">  
    
          <p>Kindly Select your favourite sports</p>  
    
          <input type="checkbox" name="sport1" value="cricket">Cricket<br>  
    
          <input type="checkbox" name="sport2" value="tennis">Tennis<br>  
    
          <input type="checkbox" name="sport3" value="football">Football<br>  
    
          <input type="checkbox" name="sport4" value="baseball">Baseball<br>  
    
          <input type="checkbox" name="sport5" value="badminton">Badminton<br><br>  
    
          <input type="submit" value="submit">  
    
      </form>

    Output:

    Input “checkbox” type

    Registration Form

    Enter your Name: 

    Kindly Select your favorite sportsCricket
    Tennis
    Football
    Baseball
    Badminton


    7. <input type=”button”>:

    The <input> type “button” defines a simple push button, which can be programmed to control a functionally on any event such as, click event.

    Note: It mainly works with JavaScript.

    Example:

    <form>  
    
         <input type="button" value="Clcik me " onclick="alert('you are learning HTML')">  
    
    </form>

    Output:

    Input “button” type.

    Click the button to see the result:

    Note: In the above example we have used the “alert” of JS, which you will learn in our JS tutorial. It is used to show a pop window.


    8. <input type=”file”>:

    The <input> element with type “file” is used to select one or more files from user device storage. Once you select the file, and after submission, this file can be uploaded to the server with the help of JS code and file API.

    Example:

    <form>  
    
         <label>Select file to upload:</label>  
    
         <input type="file" name="newfile">  
    
         <input type="submit" value="submit">  
    
    </form>

    Output:

    Input “file” type.

    We can choose any type of file until we do not specify it! The selected file will appear at next to “choose file” optionSelect file to upload:  


    9. <input type=”image”>:

    The <input> type “image” is used to represent a submit button in the form of image.

    Example:

    
    
    1. <!DOCTYPE html>  
    2. <html>  
    3. <body>  
    4. <h2>Input "image" type.</h2>  
    5. <p>We can create an image as submit button</p>  
    6.   <form>  
    7.     <label>User id:</label><br>  
    8.      <input type="text" name="name"><br><br>  
    9.      <input type="image" alt="Submit" src="login.png"  width="100px">  
    10.   </form>  
    11.   
    12.  </body>  
    13. </html> 

    HTML5 newly added <input> types element

    1. <input type=”color”>:

    The <input> type “color” is used to define an input field which contains a colour. It allows a user to specify the colour by the visual colour interface on a browser.

    Note: The “color” type only supports color value in hexadecimal format, and the default value is #000000 (black).

    Example:

    
    
    1. <form>  
    2.     Pick your Favorite color: <br><br>  
    3.     <input type="color" name="upclick" value="#a52a2a"> Upclick<br><br>  
    4.     <input type="color" name="downclick" value="#f5f5dc"> Downclick  
    5. </form>

    Output:

    Input “color” types:

    Pick your Favorite color:

     Up-click

     Down-click

    Note:The default value of “color” type is #000000 (black). It only supports color value in hexadecimal format.


    2. <input type=”date”>:

    The <input> element of type “date” generates an input field, which allows a user to input the date in a given format. A user can enter the date by text field or by date picker interface.

    Example:

    <form>  
    
        Select Start and End Date: <br><br>  
    
          <input type="date" name="Startdate"> Start date:<br><br>  
    
          <input type="date" name="Enddate"> End date:<br><br>  
    
         <input type="submit">  
    
    </form>

    Output:

    Input “date” type

    Select Start and End Date:

     Start date:

     End date:


    3. <input type=”datetime-local”>:

    The <input> element of type “datetime-local” creates input filed which allow a user to select the date as well as local time in the hour and minute without time zone information.

    Example:

    <form>  
    
        <label>  
    
          Select the meeting schedule: <br><br>  
    
          Select date & time: <input type="datetime-local" name="meetingdate"> <br><br>  
    
        </label>  
    
          <input type="submit">  
    
    </form>

    Output:

    Input “datetime-local” type

    Select the meeting schedule:

    Select date & time: 


    4. <input type=”email”>:

    The <input> type “email” creates an input filed which allow a user to enter the e-mail address with pattern validation. The multiple attributes allow a user to enter more than one email address.

    Example:

    
    
    1. <form>  
    2.          <label><b>Enter your Email-address</b></label>  
    3.         <input type="email" name="email" required>  
    4.         <input type="submit">  
    5.          <p><strong>Note:</strong>User can also enter multiple email addresses separating by comma or whitespace as following: </p>  
    6.          <label><b>Enter multiple Email-addresses</b></label>  
    7.          <input type="email" name="email"  multiple>  
    8.         <input type="submit">  
    9. </form> 

    Output:

    Input “email” type

    Enter your Email-address

    Note:User can also enter multiple email addresses separating by comma or whitespace as following:Enter multiple Email-addresses


    5. <input type=”month”>:

    The <input> type “month” creates an input field which allows a user to easily enter month and year in the format of “MM, YYYY” where MM defines month value, and YYYY defines the year value. New

    Example:

    <form>  
    
        <label>Enter your Birth Month-year: </label>  
    
        <input type="month" name="newMonth">  
    
        <input type="submit">  
    
    </form>

    Output:

    Input “month” type:

    Enter your Birth Month-year:  


    6. <input type=”number”>:

    The <input> element type number creates input filed which allows a user to enter the numeric value. You can also restrict to enter a minimum and maximum value using min and max attribute.

    Example:

    <form>  
    
        <label>Enter your age: </label>  
    
        <input type="number" name="num" min="50" max="80">  
    
         <input type="submit">  
    
    </form>

    Output:

    Input “number” type

    Enter your age:  

    Note:It will allow to enter number in range of 50-80. If you want to enter number other than range, it will show an error.


    7. <input type=”url”>:

    The <input> element of type “url” creates an input filed which enables user to enter the URL.

    Example:

    
    
    1. <form>  
    2.     <label>Enter your website URL: </label>  
    3.     <input type="url" name="website" placeholder="http://example.com"><br>  
    4.     <input type="submit" value="send data">  
    5. </form>

    Output:

    Input “url” type

    Enter your website URL: 


    8. <input type=”week”>:

    The <input> type week creates an input field which allows a user to select a week and year form the drop-down calendar without time zone.

    Example:

    
    
    1. <form>  
    2.     <label><b>Select your best week of year:</b></label><br><br>  
    3.     <input type="week" name="bestweek">  
    4.     <input type="submit" value="Send data">  
    5.  </form>

    Output:

    Input “week” type

    Select your best week of year:


    9. <input type=”search”>:

    The <input> type “search” creates an input filed which allows a user to enter a search string. These are functionally symmetrical to the text input type, but may be styled differently.

    Example:

    <form>  
    
        <label>Search here:</label>  
    
        <input type="search" name="q">  
    
        <input type="submit" value="search">  
    
    </form>

    Output:

    Input “search” type

    Search here:  


    10. <input type=”tel”>:

    The <input> element of type ?tel? creates an input filed to enter the telephone number. The “tel” type does not have default validation such as email, because telephone number pattern can vary worldwide.

    Example:

    <form>  
    
          <label><b>Enter your Telephone Number(in format of xxx-xxx-xxxx):</b></label>  
    
          <input type="tel" name="telephone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" required>  
    
          <input type="submit"><br><br>  
    
       </form>

    Output:

    Input “tel” type

    Enter your Telephone Number(in format of xxx-xxx-xxxx):

    Note: Here we are using two attributes that are “pattern” and”required” which will allow user to enter the number in given format and it is required to enter the number in input field.

  • HTML Form

    An HTML form is a section of a document which contains controls such as text fields, password fields, checkboxes, radio buttons, submit button, menus etc.

    An HTML form facilitates the user to enter data that is to be sent to the server for processing such as name, email address, password, phone number, etc. .


    Why use HTML Form

    HTML forms are required if you want to collect some data from of the site visitor.

    For example: If a user want to purchase some items on internet, he/she must fill the form such as shipping address and credit/debit card details so that item can be sent to the given address.


    HTML Form Syntax

    
    
    1. <form action="server url" method="get|post">  
    2.   //input controls e.g. textfield, textarea, radiobutton, button  
    3. </form> 


    HTML Form Tags

    Let’s see the list of HTML 5 form tags.

    TagDescription
    <form>It defines an HTML form to enter inputs by the used side.
    <input>It defines an input control.
    <textarea>It defines a multi-line input control.
    <label>It defines a label for an input element.
    <fieldset>It groups the related element in a form.
    <legend>It defines a caption for a <fieldset> element.
    <select>It defines a drop-down list.
    <optgroup>It defines a group of related options in a drop-down list.
    <option>It defines an option in a drop-down list.
    <button>It defines a clickable button.

    HTML 5 Form Tags

    Let’s see the list of HTML 5 form tags.

    TagDescription
    <datalist>It specifies a list of pre-defined options for input control.
    <keygen>It defines a key-pair generator field for forms.
    <output>It defines the result of a calculation.

    HTML <form> element

    The HTML <form> element provide a document section to take input from user. It provides various interactive controls for submitting information to web server such as text field, text area, password field, etc.

    Note: The <form> element does not itself create a form but it is container to contain all required form elements, such as <input>, <label>, etc.

    Syntax:

    
    
    1. <form>  
    2. //Form elements  
    3. </form>  

    HTML <input> element

    The HTML <input> element is fundamental form element. It is used to create form fields, to take input from user. We can apply different input filed to gather different information form user. Following is the example to show the simple text input.

    Example:

    
    
    1. <body>  
    2.   <form>  
    3.      Enter your name  <br>  
    4.     <input type="text" name="username">  
    5.   </form>  
    6. </body> 

    Output:

    HTML Form

    HTML TextField Control

    The type=”text” attribute of input tag creates textfield control also known as single line textfield control. The name attribute is optional, but it is required for the server side component such as JSP, ASP, PHP etc.

    
    
    1. <form>  
    2.     First Name: <input type="text" name="firstname"/> <br/>  
    3.     Last Name:  <input type="text" name="lastname"/> <br/>  
    4.  </form> 

    Output:

    HTML TextField Control

    Note: If you will omit ‘name’ attribute then the text filed input will not be submitted to server.


    HTML <textarea> tag in form

    The <textarea> tag in HTML is used to insert multiple-line text in a form. The size of <textarea> can be specify either using “rows” or “cols” attribute or by CSS.

    Example:

    
    
    1. <!DOCTYPE html>  
    2. <html>  
    3. <head>  
    4.     <title>Form in HTML</title>  
    5. </head>  
    6. <body>  
    7.   <form>  
    8.         Enter your address:<br>  
    9.       <textarea rows="2" cols="20"></textarea>  
    10.   </form>  
    11. </body>  
    12. </html>

    Output:

    HTML textarea tag in form

    Label Tag in Form

    It is considered better to have label in form. As it makes the code parser/browser/user friendly.

    If you click on the label tag, it will focus on the text control. To do so, you need to have for attribute in label tag that must be same as id attribute of input tag.

    NOTE: It is good to use <label> tag with form, although it is optional but if you will use it, then it will provide a focus when you tap or click on label tag. It is more worthy with touchscreens.

    <form>  
    
        <label for="firstname">First Name: </label> <br/>  
    
                  <input type="text" id="firstname" name="firstname"/> <br/>  
    
       <label for="lastname">Last Name: </label>  
    
                  <input type="text" id="lastname" name="lastname"/> <br/>  
    
     </form>

    Output:

    HTML Label Tag in Form

    HTML Password Field Control

    The password is not visible to the user in password field control.

    <form>  
    
        <label for="password">Password: </label>  
    
                  <input type="password" id="password" name="password"/> <br/>  
    
    </form>

    Output:

    HTML Password Field Control

    HTML 5 Email Field Control

    The email field in new in HTML 5. It validates the text for correct email address. You must use @ and . in this field.

    
    
    1. <form>  
    2.     <label for="email">Email: </label>  
    3.               <input type="email" id="email" name="email"/> <br/>  
    4. </form>

    It will display in browser like below:

    HTML 5 Email Field Control

    Note: If we will not enter the correct email, it will display error like:

    HTML 5 Email Field Control

    Radio Button Control

    The radio button is used to select one option from multiple options. It is used for selection of gender, quiz questions etc.

    If you use one name for all the radio buttons, only one radio button can be selected at a time.

    Using radio buttons for multiple options, you can only choose a single option at a time.

    <form>  
    
        <label for="gender">Gender: </label>  
    
                  <input type="radio" id="gender" name="gender" value="male"/>Male  
    
                  <input type="radio" id="gender" name="gender" value="female"/>Female <br/>  
    
    </form>
    Radio Button Control

    Checkbox Control

    The checkbox control is used to check multiple options from given checkboxes.

    <form>  
    
    Hobby:<br>  
    
                  <input type="checkbox" id="cricket" name="cricket" value="cricket"/>  
    
                     <label for="cricket">Cricket</label> <br>  
    
                  <input type="checkbox" id="football" name="football" value="football"/>  
    
                     <label for="football">Football</label> <br>  
    
                  <input type="checkbox" id="hockey" name="hockey" value="hockey"/>  
    
                     <label for="hockey">Hockey</label>  
    
    </form>

    Note: These are similar to radio button except it can choose multiple options at a time and radio button can select one button at a time, and its display.

    Output:

    Checkbox Control

    Submit button control

    HTML <input type=”submit”> are used to add a submit button on web page. When user clicks on submit button, then form get submit to the server.

    Syntax:

    <input type="submit" value="submit">  

    The type = submit , specifying that it is a submit button

    The value attribute can be anything which we write on button on web page.

    The name attribute can be omit here.

    Example:

    <form>  
    
        <label for="name">Enter name</label><br>  
    
        <input type="text" id="name" name="name"><br>  
    
        <label for="pass">Enter Password</label><br>  
    
        <input type="Password" id="pass" name="pass"><br>  
    
        <input type="submit" value="submit">  
    
    </form>

    Output:

    Submit button control

    HTML <fieldset> element:

    The <fieldset> element in HTML is used to group the related information of a form. This element is used with <legend> element which provide caption for the grouped elements.

    Example:

    <form>  
    
         <fieldset>  
    
          <legend>User Information:</legend>  
    
        <label for="name">Enter name</label><br>  
    
    <input type="text" id="name" name="name"><br>  
    
    <label for="pass">Enter Password</label><br>  
    
    <input type="Password" id="pass" name="pass"><br>  
    
    <input type="submit" value="submit">  
    
    </fieldset>  
    
    lt;/form>

    Output:

    HTML fieldset element

    HTML Form Example

    Following is the example for a simple form of registration.

    
    
    1. <!DOCTYPE html>  
    2.  <html>  
    3.  <head>  
    4.   <title>Form in HTML</title>  
    5. </head>  
    6.  <body>  
    7.      <h2>Registration form</h2>  
    8.     <form>  
    9.      <fieldset>  
    10.         <legend>User personal information</legend>  
    11.         <label>Enter your full name</label><br>  
    12.         <input type="text" name="name"><br>  
    13.          <label>Enter your email</label><br>  
    14.          <input type="email" name="email"><br>  
    15.          <label>Enter your password</label><br>  
    16.          <input type="password" name="pass"><br>  
    17.          <label>confirm your password</label><br>  
    18.          <input type="password" name="pass"><br>  
    19.          <br><label>Enter your gender</label><br>  
    20.          <input type="radio" id="gender" name="gender" value="male"/>Male  <br>  
    21.          <input type="radio" id="gender" name="gender" value="female"/>Female <br/>    
    22.          <input type="radio" id="gender" name="gender" value="others"/>others <br/>   
    23.           <br>Enter your Address:<br>  
    24.          <textarea></textarea><br>  
    25.          <input type="submit" value="sign-up">  
    26.      </fieldset>  
    27.   </form>  
    28.  </body>  
    29. </html> 

    Output:

    HTML Form Example

    HTML Form Example

    Let’s see a simple example of creating HTML form.

    <form action="#">  
    
    <table>  
    
    <tr>  
    
        <td class="tdLabel"><label for="register_name" class="label">Enter name:</label></td>  
    
        <td><input type="text" name="name" value="" id="register_name" style="width:160px"/></td>  
    
    </tr>  
    
    <tr>  
    
        <td class="tdLabel"><label for="register_password" class="label">Enter password:</label></td>  
    
        <td><input type="password" name="password" id="register_password" style="width:160px"/></td>  
    
    </tr>  
    
    <tr>  
    
        <td class="tdLabel"><label for="register_email" class="label">Enter Email:</label></td>  
    
        <td  
    
    ><input type="email" name="email" value="" id="register_email" style="width:160px"/></td>  
    
    </tr>  
    
    <tr>  
    
        <td class="tdLabel"><label for="register_gender" class="label">Enter Gender:</label></td>  
    
        <td>  
    
    <input type="radio" name="gender" id="register_gendermale" value="male"/>  
    
    <label for="register_gendermale">male</label>  
    
    <input type="radio" name="gender" id="register_genderfemale" value="female"/>  
    
    <label for="register_genderfemale">female</label>  
    
        </td>  
    
    </tr>  
    
    <tr>  
    
        <td class="tdLabel"><label for="register_country" class="label">Select Country:</label></td>  
    
        <td><select name="country" id="register_country" style="width:160px">  
    
        <option value="india">india</option>  
    
        <option value="pakistan">pakistan</option>  
    
        <option value="africa">africa</option>  
    
        <option value="china">china</option>  
    
        <option value="other">other</option>  
    
    </select>  
    
    </td>  
    
    </tr>  
    
    <tr>  
    
        <td colspan="2"><div align="right"><input type="submit" id="register_0" value="register"/>  
    
    </div></td>  
    
    </tr>  
    
    </table>  
    
    </form>