Menu
CoddyTech

HTML Basics Interview Questions

Document structure, elements and attributes, lists, tables, links and entities, as asked in campus placements and first rounds.

20 questions
  1. What is HTML?

    HTML (HyperText Markup Language) describes the structure and meaning of a web page. The browser parses it into the DOM; CSS styles it and JavaScript adds behavior.

    Follow-up: is HTML a programming language? No, it has no variables, conditions or loops.

    <h1>Orders</h1>
    <p>You have <strong>3</strong> open orders. <a href="/orders">View them</a>.</p>
    
  2. What is the difference between a tag, an element and an attribute in HTML?

    A tag is the markup in angle brackets (<a>), an element is everything from the start tag to the end tag including the content, and an attribute is a name and value on the start tag (href="/docs"). After parsing, tags are gone: the DOM is a tree of elements.

  3. What is <!DOCTYPE html> and why is it needed?

    <!DOCTYPE html> tells the browser to render in standards mode. Without it, browsers fall back to quirks mode, which imitates old browser bugs, so the same CSS can lay out differently. It is a declaration, not a tag; the doctype page shows the old HTML 4 forms.

  4. What is the basic structure of an HTML document?

    A doctype, an <html> root, a <head> with metadata that is not displayed, and a <body> with the visible content.

    HTML
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>My first page</title>
    </head>
    <body>
      <h1>Hello</h1>
      <p>This text is inside the body, so it is visible.</p>
    </body>
    </html>

    Mention lang on <html>, <meta charset="utf-8"> early in the head, and the viewport tag, without which phones render the page at desktop width.

  5. What is the difference between <head> and <header>?

    <head> holds metadata (title, meta tags, stylesheets, scripts), is never displayed, and appears once. <header> is visible introductory content for the page or a section, such as a logo, title and navigation, and a page can have several.

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

    A block element starts on a new line and takes the full width (<div>, <p>); an inline element flows inside text and is as wide as its content (<span>, <a>).

    HTML
    <!DOCTYPE html>
    <style>
      div, p { background: #dbeafe; }
      span, a { background: #fde68a; }
      span { width: 300px; height: 80px; } /* ignored: span is inline */
    </style>
    <div>A div takes the whole line.</div>
    <p>A paragraph with <span>a span</span> and <a href="#">a link</a> that stay in the text.</p>

    The trap: width, height and vertical margins do not apply to inline text elements such as <span> and <a>. The CSS display property changes the type.

  7. What is the difference between <div> and <span>?

    Both are generic containers with no meaning: <div> is block-level and groups layout, <span> is inline and wraps text. Use them only when no semantic element such as <nav> or <button> fits, because those tell screen readers what the content is. More on the span page.

  8. What are void (empty) elements in HTML?

    Void elements cannot have content, so they have no end tag: <img>, <br>, <hr>, <input>, <meta>, <link>, <source> and a few others.

    Follow-up: what about <br />? The slash is allowed and ignored. On other elements it means nothing: <div /> opens a div, and everything after it ends up inside.

  9. What is the difference between id and class?

    An id must be unique and identifies one element; a class can be shared by many elements, and an element can have several.

    HTML
    <!DOCTYPE html>
    <style>
      .tag { background: #e0e7ff; padding: 2px 8px; border-radius: 4px; }
      #featured { outline: 2px solid #f59e0b; }
    </style>
    <p>
      <span class="tag">html</span>
      <span class="tag" id="featured">css</span>
      <span class="tag">javascript</span>
    </p>
    <a href="#featured">Jump to #featured</a>

    Ids are targets for fragment links, <label for> and getElementById. Use classes for styling, since id selectors have higher specificity. See class selectors.

  10. What are the new features in HTML5?

    Semantic elements (<header>, <nav>, <main>, <article>), native <audio> and <video>, <canvas>, new input types with built-in validation, data-* attributes, APIs such as Web Storage and Web Workers, and the short doctype.

    Follow-up: is there an HTML6? No. HTML is now a Living Standard, updated continuously.

  11. What is the alt attribute on an image, and when should it be empty?

    alt is the image's text alternative, read by screen readers, shown when the image fails to load, and used by search engines. Describe what the image shows or does in its context.

    <img src="chart.png" alt="Sales grew from 40 to 65 orders a day in March">
    <a href="/"><img src="logo.svg" alt="Coddy home"></a>
    <img src="divider.png" alt="">
    

    Use alt="" for decorative images. Leaving alt out is worse: some screen readers then read the file name.

  12. What types of lists are there in HTML?

    Three: <ul> for unordered items, <ol> for steps or rankings, and <dl> for term and description pairs.

    HTML
    <!DOCTYPE html>
    <ul>
      <li>Milk</li>
      <li>Bread</li>
    </ul>
    <ol start="3">
      <li>Third step</li>
      <li>Fourth step</li>
    </ol>
    <dl>
      <dt>HTML</dt>
      <dd>Structure of the page</dd>
      <dt>CSS</dt>
      <dd>How it looks</dd>
    </dl>

    Only <li> (plus <script> and <template>) may be a direct child of <ul> or <ol>, so a nested list goes inside an <li>. More on the lists page.

  13. How do you create a table in HTML, and what do colspan and rowspan do?

    A <table> holds rows (<tr>) of header cells (<th>) and data cells (<td>), optionally grouped in <thead> and <tbody>. colspan stretches a cell across columns, rowspan across rows.

    HTML
    <!DOCTYPE html>
    <style>
      table { border-collapse: collapse; }
      th, td { border: 1px solid #94a3b8; padding: 4px 10px; }
    </style>
    <table>
      <caption>Timetable</caption>
      <thead>
        <tr><th scope="col">Day</th><th scope="col">9:00</th><th scope="col">10:00</th></tr>
      </thead>
      <tbody>
        <tr><th scope="row">Mon</th><td colspan="2">Lab (two hours)</td></tr>
        <tr><th scope="row">Tue</th><td>Maths</td><td rowspan="2">Physics</td></tr>
        <tr><th scope="row">Wed</th><td>English</td></tr>
      </tbody>
    </table>

    Use tables for data, never for layout. See colspan and rowspan.

  14. What are HTML entities, and when do you need them?

    Entities are codes for characters that would be read as markup or are hard to type, like &lt;, &amp;, &nbsp; or &#x20AC;.

    HTML
    <!DOCTYPE html>
    <p>To start a paragraph, write &lt;p&gt;.</p>
    <p>Tom &amp; Jerry &copy; 1940, price 10&nbsp;&#x20AC;</p>

    On a UTF-8 page only <, & and quotes inside attribute values need escaping. Escaping user input this way is the basis of XSS prevention.

  15. How do you write a comment in HTML, and are comments visible to users?

    Write <!-- text -->. Comments are not rendered but are sent with the page, so anyone can read them in View Source; never put keys or internal notes there. They cannot be nested: the first --> ends the comment.

  16. What are the ways to add CSS to an HTML page?

    Three: an inline style attribute, an internal <style> element, and an external stylesheet linked with <link rel="stylesheet">.

    <head>
      <link rel="stylesheet" href="styles.css">  <!-- external -->
      <style>p { line-height: 1.6; }</style>      <!-- internal -->
    </head>
    <body>
      <p style="color: crimson;">Inline</p>       <!-- inline -->
    </body>
    

    Use external stylesheets on real sites, since the browser caches them. Inline styles beat stylesheet rules unless a rule uses !important. See external CSS.

  17. Is HTML case-sensitive?

    Tag and attribute names are not; the parser lowercases them. Many attribute values are: id="Main" and id="main" are different ids, and class names are compared exactly (quirks mode makes CSS class and id matching case-insensitive). Write everything lowercase.

  18. What is the difference between HTML and XHTML?

    XHTML is HTML in XML syntax, read by a parser that stops at the first error; the HTML parser repairs mistakes. Only a page served as application/xhtml+xml gets the XML parser, so text/html is HTML however it is written.

    HTMLXHTML
    NamesCase-insensitiveLowercase only
    Void elements<br><br />
    End tagsSome optionalAlways required
  19. What are boolean attributes, and what does disabled="false" do?

    A boolean attribute is true when present and false when absent, whatever its value. So disabled="false" still disables the element.

    <button disabled>Disabled</button>
    <button disabled="false">Still disabled</button>
    <button>Enabled</button>
    

    To turn one off, remove the attribute (removeAttribute("disabled")) or set the property button.disabled = false. Others: checked, required, readonly, multiple, open.

Coddy programming languages illustration

Learn to code with Coddy

Get started