Interview - HTML

Difference between bold and strong tag.

<strong>

  • The <strong> tag is used to define text with strong importance.
  • The text inside this tag is bold.
  • It supports the Global attributes.
  • It supports the Event attributes.
  • Its supported browsers are -: Chrome , Microsoft Edge , Safari , Firefox , Opera Mini.


<b>

  • The bold (<b>) tag specifies bold text without any extra importance.
  • Its makes any text bold.
  • It supports the Event attributes.
  • It supports the Global attributes.
  • Its supported browsers are -: Chrome , Microsoft Edge , Safari , Firefox , Opera Mini.

Difference between class and id attribute.

id

The id attribute is a unique identifier that is used to specify the document. It is used by CSS and JavaScript to perform a certain task for a unique element. In CSS, the id attribute is written using the # symbol followed by id.


class

The class attribute is used to specify one or more class names for an HTML element. The class attribute can be used on any HTML element. The class name can be used by CSS and JavaScript to perform certain tasks for elements with the specified class name. The class name in CSS stylesheet using . symbol.


Difference between id and class attribute: The only difference between them is that id is unique in a page and can only apply to at most one element, while class selector can apply to multiple elements. 

Difference between i tag and em tag

<i>

  • It is one of the element of HTML which is used in formatting HTML texts. It is used to define a text in technical term, alternative mood or voice, a thought, etc.


<em>

  • It is also one of the element of HTML used in formatting texts. It is used to define emphasized text or statements.


By default, the visual result is the same. The main difference between these two tag is that the <em> tag semantically emphasizes on the important word or section of words while <i> tag is just offset text conventionally styled in italic to show alternative mood or voice.

Explain Geolocation API in HTML5

The HTML Geolocation API is used to get the geographical position of a user.


Since this can compromise privacy, the position is not available unless the user approves it.


Geolocation Object - Methods

  • getCurrentPosition() - Returns the current position of the user.
  • watchPosition() - Returns the current position of the user and continues to return updated position as the user moves (like the GPS in a car).
  • clearWatch() - Stops the watchPosition() method.


Return Data

  • coords.latitude - The latitude as a decimal number. (always returned)
  • coords.longitude - The longitude as a decimal number. (always returned)
  • coords.accuracy - The accuracy of position. (always returned)
  • coords.altitude - The altitude in meters above the mean sea level. (returned if available)
  • coords.altitudeAccuracy - The altitude accuracy of position. (returned if available)
  • coords.heading - The heading as degrees clockwise from North. (returned if available)
  • coords.speed - The speed in meters per second. (returned if available)
  • timestamp - The date/time of the response. (returned if available)


Handling Errors and Rejections

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML = "An unknown error occurred."
      break;
  }
}


Example

<script>
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition, showError);
  } else { 
    alert("Geolocation is not supported by this browser.");
  }
}


function showPosition(position) {
  alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
}


function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      alert("User denied the request for Geolocation.");
      break;
    case error.POSITION_UNAVAILABLE:
      alert("Location information is unavailable.");
      break;
    case error.TIMEOUT:
      alert("The request to get user location timed out.");
      break;
    case error.UNKNOWN_ERROR:
      alert("An unknown error occurred.");
      break;
  }
}
</script>

Explain HTML5 Graphics

SVG (Scalable Vector Graphics)

  • These are images created by a markup language that are reusable, simple, high-quality standalone images that can be exported and imported as well.
  • They are cross-browser friendly and can be used both on the client-side and server-side of the application.
  • They can be manipulated to create animations, hybrid images, etc. by editing the markup language or by editing using a stylesheet.
  • Files come with a .svg extension.


PNG (Portable Network Graphics)

  • They are portable, static and lossless with proper indexed-color control.
  • Files come with a .png or .PNG extension.
  • Cross-browser friendly and have streaming capabilities.


JPG or JPEG (Joint Photographic Experts Group)

  • Lossy compressed with an adjustable degree of compression.
  • Used mainly with digital photography and can achieve a compression of 10:1.
  • Files come with a .jpg or jpeg extension.


CSS (Cascading Style Sheets)

  • This is a type of language mainly used for designing and HTML and SVG elements by using code.
  • They are scalable and give more space for creativity to the user.
  • Files usually come with a .css extension.


Canvas API

  • Has no DOM and uses vector based methods to create objects, graphics and shapes.
  • Canvas API applications can be standalone or integrated with HTML or SVG.
  • Widely used for games
  • Client-side scripting with native modern browser support.


WebCGM (Web Computer Graphics Metafile)

  • Very similar to SVG when it comes to graphical features.
  • CGM is the ISO standard for vector image definition.
  • Used in aeronautics, automotive engineering, technical illustration, etc.
  • They are not widely used traditionally. More modern approaches have been to use SVGs, Canvas, etc.

Explain meta tags

The <meta> tag defines metadata about an HTML document. Metadata is data (information) about data.


<meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the document, and viewport settings.


Metadata will not be displayed on the page, but is machine parsable.


Metadata is used by browsers (how to display content or reload page), search engines (keywords), and other web services.


There is a method to let web designers take control over the viewport (the user's visible area of a web page), through the <meta> tag.


Attributes

  • charset - Specifies the character encoding for the HTML document. (Value: character_set)
  • content - Specifies the value associated with the http-equiv or name attribute. (Value: text)
  • http-equiv - Provides an HTTP header for the information/value of the content attribute. (Value: content-security-policy, content-type, default-style, refresh)
  • name - Specifies a name for the metadata. (Value: application-name, author, description, generator, keywords, viewport)


Example

Define keywords for search engines:

<meta name="keywords" content="HTML, CSS, JavaScript">


Define a description of your web page:

<meta name="description" content="Free Web tutorials for HTML and CSS">


Define the author of a page:

<meta name="author" content="John Doe">


Refresh document every 30 seconds:

<meta http-equiv="refresh" content="30">


Setting the viewport to make your website look good on all devices:

<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • The width=device-width part sets the width of the page to follow the screen-width of the device. (which will vary depending on the device)
  • The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.

Explain new form input types in HTML5.

Input Type attributes

  • color - This input type allows the user to select a color from a color picker.
  • date - This input type allows the user to select a date from a drop-down calendar. (not supported by the Internet Explorer and Safari browsers.)
  • time - This input type allows the user to enter a time. (not supported by the Internet Explorer and Safari browsers.)
  • datetime - This input type allows the user to select date and time along with timezone.
  • datetime-local - This input type allows the user to select both local date and time. (not supported by Firefox, Safari, and Internet Explorer browsers.)
  • week - This input type allows the user to select week and year from the drop-down calendar. (not supported by Firefox, Safari, and Internet Explorer browsers.)
  • email - This input type allows the user to enter an e-mail address.
  • month - This input type allows the user to select a month and year from a drop-down calendar. (not supported by Firefox, Safari and Internet Explorer browsers.)
  • number - This input type allows the user to enter a numerical value.
  • range - This input type allows the user to enter a numerical value within a specified range.
  • search - This input type allows the user to enter a search string within the input field.
  • tel - This input type allows the user to enter a telephone number. (not supported by any browser.)
  • url - This input type allows the user to enter the URL.


Explain the concept of web storage in HTML5

With web storage, web applications can store data locally within the user's browser.


Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.


Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.


Web storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.



HTML Web Storage Objects

HTML web storage provides two objects for storing data on the client:

  • window.localStorage - stores data with no expiration date.
  • window.sessionStorage - stores data for one session. (data is lost when the browser tab is closed)

Before using web storage, check browser support for localStorage and sessionStorage:

if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
} else {
  // Sorry! No Web Storage support..
}


The localStorage Object

The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.


Example:

// Store
localStorage.setItem("lastname", "Smith");

// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");


The sessionStorage Object

The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.


Example:

if (sessionStorage.clickcount) {
  sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
  sessionStorage.clickcount = 1;
}

document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";

Explain the difference between div and span

<div>

  • The <div> tag is a block level element.
  • It is best to attach it to a section of a web page.
  • It accepts align attribute.
  • This tag should be used to wrap a section, for highlighting that section.


<span>

  • The <span> tag is an inline element.
  • It is best to attach a CSS to a small section of a line in a web page.
  • It does not accept align attribute.
  • This tag should be used to wrap any specific word that you want to highlight in your webpage.

Explain whether an article element can have section elements and vice-versa

It's entirely acceptable to nest them either way. Although the document outline does not distinguish between a <section> and an <article>, from a semantic point of view they are two different things. That's the whole point of introducing them as two distinct semantic elements.


Use the <section> if your page consists of multiple articles.


Use the <article> when you have an article that's comprehensive enough to contain multiple sections.


You can even combine them both if using both fits your content, such that your markup looks like this.

<section><h1>section article?</h1>
  <article><h1>art 1</h1>
    <section><h1>sec 1.1</h1></section>
    <section><h1>sec 1.2</h1></section>
    <section><h1>sec 1.3</h1></section>
  </article>
  <article><h1>art 2</h1>
    <section><h1>sec 2.1</h1></section>
    <section><h1>sec 2.2</h1></section>
    <section><h1>sec 2.3</h1></section>
  </article>
  <article><h1>art 3</h1>
    <section><h1>sec 3.1</h1></section>
    <section><h1>sec 3.2</h1></section>
    <section><h1>sec 3.3</h1></section>
  </article>
</section>

How HTML5 doctype is different from normal html doctype ?

<!DOCTYPE html> is a DECLARATION. It tells browsers what version of HTML are you using, which helps them to properly render the elements on webpage.


HTML tag have three functions:

  1. It tells the browser that this is an HTML document.
  2. It represents the root of an HTML document.
  3. It is the container for all other HTML elements. (except for the <!DOCTYPE> tag)

How section tag is different from div tag ?

<div>

  • It is called as division tag.
  • It represents its child elements and doesn’t have a special meaning.
  • It doesn’t have any specific meaning.
  • Use <div> element for style purposes in webpage.


<section>

  • It is called as section tag.
  • It represents its child elements and has a special meaning.
  • It describes its meaning in the web page.
  • Use <section> during requirements of headers or footers or any other section of documents.