Top HTML Questions For Interview Preparation

  1. What are HTML tags and HTML Elements?

    HTML Tags: Tags are the starting and ending parts of an HTML element. They begin with < symbol and end with > symbol. Whatever is written inside < and > are called tags.
    Syntax :

     <b> </b>
    

    HTML elements: Elements enclose the contents in between the tags. They consist of some kind of structure or expression. It generally consists of a start tag, content, and an end tag.
    Syntax:

     <b>This is the content.</b>
    
  2. What is the difference between <link> tag and <a> (anchor) tag in html ?

    <link> tag: The <link> tag is used in the head section of an HTML document to link external resources to the HTML document. It's commonly used to link CSS stylesheets. Here is a sample use of the <link> tag:
    Syntax:

     <link rel="stylesheet" href="styles.css">
    

    <a> tag: The <a> tag, also known as the anchor tag, is used to create hyperlinks that can lead to other webpages, different sections of the same webpage, or to download files. Here is a sample use of the <a> tag:
    Syntax:

     <a href="https://www.google.com">Visit Google</a>
    
  3. What are the different kinds of HTML Elements according to their display properties ?

    Block-level Elements: These elements take up the full width available and form a block. They always start on a new line. Examples include <div>, <p>, <h1> to <h6>, <ol>, <ul>, etc.

    Inline Elements: These elements only take up as much width as necessary and do not start on a new line. Examples include <span>, <a>, <img>, <strong>, <em>, etc.

    Inline-block Elements: Elements with display: inline-block are like inline elements, but they can have a width and height. That means you can allow them to take up as much space as they need, like inline elements, but also explicitly dictate their size.

    None: This value for the display property makes the element and its content disappear from the page entirely. It doesn't take any space, as if the element doesn't exist. This is often used with JavaScript to hide and show elements.

    Flex: This value sets an element to be a block-level flex container, enabling a flex context for all its direct children. This is useful for building components where child elements need to be laid out in a line or as a series of lines.

    Grid: Similar to flex, but for more complex, two-dimensional layouts. It allows you to define layout control for elements at both row and column level at the same time.

  4. What are Attributes in HTML ?

    Need:

    To specify additional information and properties of HTML elements for creating interactive and unique web pages.

    What is it?

    Attributes in HTML are properties in start tags, providing extra details about HTML elements, usually in name/value pairs like name="value".

    How is it used?

    To set properties like the src in <img> tag.

    To control behavior, e.g., the disabled attribute in input fields.

    For styling, using attributes like width and height.

  5. What is an iframe tag in HTML ?

    Need:

    In web development, there are instances where we need to display content from another document or include an external webpage within our own HTML document. This could involve embedding a YouTube video, a Google Map, or even an entire webpage. The HTML <iframe> tag is designed to address this need.

    What is it?:

    The <iframe> is an HTML element used to embed another document, such as an HTML webpage, or other resources within the current HTML document.

    How is it used?:

    Embedding Documents or Webpages: The src attribute specifies the URL of the document or webpage to be embedded.

    Setting Dimensions: You can control the size of the <iframe> using height and width attributes.

  6. What is the difference between block, inline and inline-block elements ?

    Block-level Elements:

    What is it:

    A block-level element always takes up the full width available (stretches out to the left and right as far as it can). Two commonly used block elements are: <p> and <div> .

    How it is used:

    Use block-level elements to create sections, headings, paragraphs, lists, and other structural elements. Some of the commonly used html elements are: <div>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<p>,<ul>,<header>,<footer>,<nav>,<section>

    Where is it used:

    Block-level elements are used for content that needs to be visually separated and structured.

    Inline Elements:

    What is it:

    An inline element does not start on a new line and it only takes up as much width as necessary. The <span> element is an inline container used to mark up a part of a text, or a part of a document.

    How it is used:

    Use inline elements to wrap text or elements which need to be on the same line within a paragraph or sentence.

    Where is it used:

    Inline elements are commonly used for text-level elements like links (<a>), emphasis (<em>), strong emphasis (<strong>), spans (<span>), and other similar elements.

    Inline-Block Elements:

    What is it:

    HTML elements that behave like inline elements but still allow setting width, height, and vertical alignment.

    How it is used:

    Use the CSS display: inline-block; property to make block-level elements behave as inline-block elements.

    Where is it used:

    Inline-block elements are useful when you need to position elements side by side while retaining the ability to control their dimensions and vertical alignment. They are commonly used in navigation menus, buttons, and image galleries.

  7. What is the difference between div and span tag in HTML ?

    Intro line:

    Both the <div> and <span> tags in HTML are used for grouping and applying styles to elements, but they're used in different contexts due to their distinct behaviors in the document flow.

    Comparison and contrast points:

    Display Property:

    <div> Tag: It's a block-level element, meaning it starts on a new line and takes up the full width available.

    <span> Tag: It's an inline element that only takes up as much width as necessary and doesn't force a new line.

    Usage Context:

    <div> Tag: Used for larger groupings of data, like sections or blocks of content.

    <span> Tag: Typically used for smaller chunks of HTML, such as a piece of text within a paragraph.

    Effect on Parent Element:

    <div> Tag: Being a block-level element, it can cause its parent element to expand vertically.

    <span> Tag: As an inline element, it doesn't impact the layout of the parent element.

  8. What is the difference between semantic and non-semantic tags in HTML ?

    Intro line:

    Both semantic and non-semantic HTML tags are used for structuring and displaying content on the web.

    Comparison and contrast points:

    Meaningfulness:

    Semantic Tags: These tags provide additional information about the content they wrap, both to the browser and the developer. Examples include <header>, <footer>, <article>, and <section>.

    Non-semantic Tags: These tags provide no additional information about their content. Examples include <div> and <span>.

    Accessibility and SEO:

    Semantic Tags: They improve accessibility for screen readers and search engine optimization (SEO), as they provide context about the type of content contained.

    Non-semantic Tags: They offer no such benefits, as they provide no context or meaning to their content.

    Usage:

    Semantic Tags: These are used when the content's context should be understood by the browser and developer. For example, <nav> is used for navigation links.

    Non-semantic Tags: These are used when the content's context doesn't need to be understood by the browser, often serving as containers for styling and scripting.

  9. How:

    Linking CSS and JavaScript files in HTML is possible using the <link> tag for CSS and the <script> tag for JavaScript.

    Linking CSS: In the <head> section of your HTML document, use the <link> tag with the rel="stylesheet" attribute and the href attribute pointing to the CSS file. For example, <link rel="stylesheet" href="styles.css">.

    Linking JavaScript: Before the closing </body> tag, use the <script> tag with the src attribute pointing to the JavaScript file. For example, <script src="script.js"></script>.

    Things to keep in mind while doing so:

    Location of Scripts: Linking JavaScript files at the end of the HTML document, right before the </body> tag, helps improve the loading performance of the webpage, as it allows the HTML content to load first.