Introduction to HTML
Introduction to HTML Interview with follow-up questions
Interview Question Index
- Question 1: What is HTML and what is it used for?
- Follow up 1 : Can you explain the structure of an HTML document?
- Follow up 2 : What are some common HTML tags and their uses?
- Follow up 3 : What is the difference between block-level and inline elements in HTML?
- Follow up 4 : What is the role of attributes in HTML?
- Question 2: What is the DOCTYPE declaration in HTML?
- Follow up 1 : Why is it important to include a DOCTYPE in an HTML document?
- Follow up 2 : What happens if a DOCTYPE is not specified?
- Follow up 3 : Can you name some different types of DOCTYPEs?
- Question 3: How can you create a hyperlink in HTML?
- Follow up 1 : What is the difference between absolute and relative URLs?
- Follow up 2 : What does the 'target' attribute do in a hyperlink?
- Follow up 3 : How can you link to a specific part of a webpage?
- Question 4: What is the purpose of the head element in an HTML document?
- Follow up 1 : What kind of information can be included in the head of an HTML document?
- Follow up 2 : What is the role of the title element?
- Follow up 3 : What is the difference between the 'link' and 'script' elements?
- Question 5: How can you include an image in an HTML document?
- Follow up 1 : How can you specify the dimensions of an image?
- Follow up 2 : What are the 'src' and 'alt' attributes in an img tag?
- Follow up 3 : What formats of images can be used in HTML?
Question 1: What is HTML and what is it used for?
Answer:
HTML stands for HyperText Markup Language. It is the standard markup language used for creating web pages and applications on the World Wide Web. HTML is used to structure the content of a web page, define its layout and formatting, and specify the interactive elements and functionality.
Follow up 1: Can you explain the structure of an HTML document?
Answer:
An HTML document consists of several elements that define its structure. The basic structure of an HTML document is as follows:
Title of the Document
Content of the Document
- ``: This declaration defines the document type and version of HTML being used.
- ``: The root element of an HTML document.
- ``: Contains meta-information about the document, such as the title, stylesheets, and scripts.
- ``: Defines the title of the document that appears in the browser's title bar or tab.
- ``: Contains the visible content of the document, such as text, images, links, and other elements.
Follow up 2: What are some common HTML tags and their uses?
Answer:
HTML tags are used to define the structure and content of an HTML document. Some common HTML tags and their uses include:
<h1>
to<h6>
: Used to define headings of different levels.</h6> </h1><p>
: Used to define paragraphs of text.<a>
: Used to create hyperlinks to other web pages or resources.<img>
: Used to insert images into the document.<ul>
and</ul> <ol>
: Used to create unordered and ordered lists, respectively.<li>
: Used to define list items within a list.<div>
: Used to group and style elements.<span>
: Used to apply styles to a specific section of text.- ``: Used to create tables.
- ``: Used to create input forms for user interaction.
- ``: Used to create input fields within a form.
Follow up 3: What is the difference between block-level and inline elements in HTML?
Answer:
Block-level elements are those that take up the full width available and start on a new line, while inline elements only take up the necessary width and do not start on a new line. Some examples of block-level elements include <div>
, <p>
, `
` to ``, and ``, while examples of inline elements include ``, ``, ``, and ``.
Block-level elements can contain other block-level and inline elements, while inline elements can only contain other inline elements or text. Block-level elements can have margins, padding, and width and height properties applied to them, while inline elements do not affect the layout in the same way and have limited styling options.
- `, while examples of inline elements include ``, ``, ``, and ``.
Block-level elements can contain other block-level and inline elements, while inline elements can only contain other inline elements or text. Block-level elements can have margins, padding, and width and height properties applied to them, while inline elements do not affect the layout in the same way and have limited styling options.
Follow up 4: What is the role of attributes in HTML?
Answer:
Attributes provide additional information about HTML elements and are used to modify the behavior or appearance of elements. They are specified within the opening tag of an element and consist of a name and a value, separated by an equals sign. Some common attributes include class
, id
, src
, href
, alt
, style
, and target
.
Attributes can be used to add CSS classes or IDs to elements for styling or JavaScript manipulation, specify the source of an image or the destination of a hyperlink, define alternative text for images, set the style of an element inline, and control how links are opened (e.g., in a new window or tab). Attributes can also be used to provide additional semantic meaning to elements, such as the alt
attribute for images to provide alternative text for accessibility purposes.
Question 2: What is the DOCTYPE declaration in HTML?
Answer:
The DOCTYPE declaration is an instruction that specifies the version of HTML or XHTML used in a web document. It is placed at the very beginning of an HTML document, before the tag. The DOCTYPE declaration is not an HTML tag; it is an instruction to the web browser about what version of HTML the page is written in.
Follow up 1: Why is it important to include a DOCTYPE in an HTML document?
Answer:
Including a DOCTYPE declaration in an HTML document is important for several reasons:
Browser compatibility: Different web browsers have different rendering engines, and they may interpret HTML code differently. By including a DOCTYPE, you ensure that the browser knows which version of HTML to expect, which helps in rendering the page correctly across different browsers.
Standards compliance: DOCTYPEs are used to define the rules and syntax of a specific version of HTML. By including a DOCTYPE, you ensure that your HTML code follows the standards set by the W3C (World Wide Web Consortium) or other relevant organizations.
Quirks mode vs Standards mode: Without a DOCTYPE, the browser may enter quirks mode, which emulates the behavior of older browsers. This can lead to inconsistent rendering and layout issues. By including a DOCTYPE, you ensure that the browser renders the page in standards mode, which is more predictable and consistent.
Follow up 2: What happens if a DOCTYPE is not specified?
Answer:
If a DOCTYPE is not specified, the browser may enter quirks mode. Quirks mode is a compatibility mode that emulates the behavior of older browsers, often those from the late 1990s and early 2000s. In quirks mode, the browser may use non-standard rendering and layout algorithms, which can lead to inconsistent rendering across different browsers.
Without a DOCTYPE, the browser may also make assumptions about the version of HTML being used, which can result in incorrect rendering of the page. It is always recommended to include a DOCTYPE declaration to ensure consistent rendering and standards compliance.
Follow up 3: Can you name some different types of DOCTYPEs?
Answer:
There are several types of DOCTYPEs that can be used in HTML documents. Some common ones include:
- HTML5: ``
- HTML 4.01 Strict: ``
- HTML 4.01 Transitional: ``
- XHTML 1.0 Strict: ``
- XHTML 1.0 Transitional: ``
These are just a few examples, and there are other DOCTYPEs available for different versions and flavors of HTML and XHTML.
Question 3: How can you create a hyperlink in HTML?
Answer:
To create a hyperlink in HTML, you can use the <a>
element. The </a><a>
element is an anchor tag that is used to define a hyperlink. It requires the href
attribute, which specifies the URL of the page you want to link to. Here is an example of how to create a hyperlink in HTML:
</a><a href="https://www.example.com">Click here</a>
Follow up 1: What is the difference between absolute and relative URLs?
Answer:
Absolute URLs specify the complete address of a webpage, including the protocol (e.g., http:// or https://), domain name, and path. Relative URLs, on the other hand, specify the address of a webpage relative to the current page. They do not include the protocol or domain name. Instead, they start with a forward slash (/) to indicate the root directory of the current domain. For example, an absolute URL could be https://www.example.com/page.html
, while a relative URL could be /page.html
.
Follow up 2: What does the 'target' attribute do in a hyperlink?
Answer:
The 'target' attribute in a hyperlink specifies where the linked document will open when clicked. It can be used to control whether the linked page opens in the same tab or window, a new tab or window, or a specific frame. The 'target' attribute can have the following values:
_self
: Opens the linked document in the same tab or window (default behavior)._blank
: Opens the linked document in a new tab or window._parent
: Opens the linked document in the parent frame._top
: Opens the linked document in the full body of the window, canceling any frames.
Here is an example of how to use the 'target' attribute:
<a href="https://www.example.com">Click here</a>
Follow up 3: How can you link to a specific part of a webpage?
Answer:
To link to a specific part of a webpage, you can use the id
attribute and the fragment identifier in the URL. First, you need to assign an id
to the element you want to link to. Then, in the href
attribute of the hyperlink, you can specify the id
preceded by a hash symbol (#). When the hyperlink is clicked, the browser will scroll to the element with the matching id
. Here is an example:
<a href="#section2">Go to Section 2</a>
<h2>Section 2</h2>
Question 4: What is the purpose of the head element in an HTML document?
Answer:
The head element in an HTML document is used to provide metadata and other non-visible information about the document. It contains elements that define the title of the document, link to external stylesheets, specify character encoding, and include scripts.
Follow up 1: What kind of information can be included in the head of an HTML document?
Answer:
The head of an HTML document can include various types of information such as:
- Title: The title element specifies the title of the document, which is displayed in the browser's title bar or tab.
- Metadata: Metadata elements provide additional information about the document, such as character encoding, viewport settings, and author information.
- Link to external stylesheets: The link element is used to link an external CSS file to the HTML document.
- Scripts: The script element is used to include JavaScript code in the document.
- Base URL: The base element specifies the base URL for all relative URLs in the document.
Follow up 2: What is the role of the title element?
Answer:
The title element is used to define the title of an HTML document. It is displayed in the browser's title bar or tab, and is also used by search engines when displaying search results. The title should be concise and descriptive, accurately representing the content of the page.
Follow up 3: What is the difference between the 'link' and 'script' elements?
Answer:
The 'link' and 'script' elements are both used in the head of an HTML document, but they serve different purposes:
Link element: The link element is used to link an external resource to the HTML document, such as a stylesheet or a favicon. It is commonly used to include CSS files using the 'rel' attribute with a value of 'stylesheet'.
Script element: The script element is used to include JavaScript code in the HTML document. It can be used to embed the code directly or to reference an external JavaScript file using the 'src' attribute. The script element can also have the 'async' or 'defer' attribute to control how the script is executed.
Question 5: How can you include an image in an HTML document?
Answer:
To include an image in an HTML document, you can use the <img>
tag. The <img>
tag is a self-closing tag, which means it doesn't require a closing tag. The src
attribute is used to specify the URL or file path of the image. For example, <img src="image.jpg">
will include an image called 'image.jpg' in the document.
Follow up 1: How can you specify the dimensions of an image?
Answer:
You can specify the dimensions of an image using the 'width' and 'height' attributes in the <img>
tag. The 'width' attribute specifies the width of the image in pixels, and the 'height' attribute specifies the height of the image in pixels. For example, <img src="image.jpg" width="300" height="200">
will display the image 'image.jpg' with a width of 300 pixels and a height of 200 pixels.
Follow up 2: What are the 'src' and 'alt' attributes in an img tag?
Answer:
The 'src' attribute in the <img>
tag is used to specify the URL or file path of the image. It tells the browser where to find the image file. The 'alt' attribute is used to provide alternative text for the image. This text is displayed if the image cannot be loaded or if the user is using a screen reader. It is also used by search engines to understand the content of the image. For example, <img src="image.jpg" alt="A beautiful sunset">
will display the image 'image.jpg' with the alternative text 'A beautiful sunset'.
Follow up 3: What formats of images can be used in HTML?
Answer:
HTML supports various image formats, including JPEG, PNG, GIF, and SVG. JPEG (Joint Photographic Experts Group) is a commonly used format for photographs and complex images. PNG (Portable Network Graphics) is a lossless format that supports transparency and is often used for graphics and logos. GIF (Graphics Interchange Format) is a format that supports animation and is commonly used for simple animations and icons. SVG (Scalable Vector Graphics) is a vector-based format that supports interactivity and can be scaled without losing quality. To include an image in HTML, you can use the <img>
tag and specify the image file with the 'src' attribute.