html interview questions

Top html frequently asked interview questions

jQuery document.createElement equivalent?

I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going on.

var d = document;
var odv = d.createElement("div"); = "none";
this.OuterDiv = odv;

var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";

I would like to know if there is a better way to do this using jQuery. I've been experimenting with:

var odv = $.create("div");
// And many more

But I'm not sure if this is any better.

Source: (StackOverflow)

Make div 100% height of browser window

I have a layout with two columns - a left div and a right div.

The right div has a grey background-color, and I need it to expand vertically depending on the height of the user's browser window. Right now the background-color ends at the last piece of content in that div.

I've tried height:100%, min-height:100%; etc.

Source: (StackOverflow)

When to use IMG vs. CSS background-image?

In what situations is it more appropriate to use an HTML IMG tag to display an image, as opposed to a CSS background-image, and vice-versa?

Factors may include accessibility, browser support, dynamic content, or any kind of technical limits or usability principles.

Source: (StackOverflow)

Make a div fill the height of the remaining screen space

I am currently working on a web application, where I want the content to fill the height of the entire screen.

The page has a header, which contains a logo, and account information. This could be an arbitrary height. I want the content div to fill the rest of the page to the bottom.

I have a header div and a content div. At the moment I am using a table for the layout like so:


#page {
    height: 100%; width: 100%

#tdcontent {
    height: 100%;

#content {
    overflow: auto; /* or overflow: hidden; */
<table id="page">
        <td id="tdheader">
            <div id="header">...</div>
        <td id="tdcontent">
            <div id="content">...</div>

The entire height of the page is filled, and no scrolling is required.

For anything inside the content div, setting top: 0; will put it right underneath the header. Sometimes the content will be a real table, with it's height set to 100%. Putting header inside content will not allow this to work.

Is there a way to achieve the same effect without using the table?


Elements inside the content div will have heights set to percentages as well. So something at 100% inside the div will fill it to the bottom. As will two elements at 50%.

Update 2:

For instance, if the header takes up 20% of the screen's height, a table specified at 50% inside #content would take up 40% of the screen space. So far, wrapping the entire thing in a table is the only thing that works.

Source: (StackOverflow)

Why does HTML think “chucknorris” is a color?

How come certain random strings produce various colors when entered as background colors in HTML? For example:

<body bgcolor="chucknorris"> test </body>

...produces a document with a red background across all browsers and platforms.

Interestingly, while chucknorri produces a red background as well, chucknorr produces a yellow background.

What's going on here?

Source: (StackOverflow)

How do you disable browser Autocomplete on web form field / input tag?

How do you disable autocomplete in the major browsers for a specific input (or form field)?

Source: (StackOverflow)

Why don't self-closing script tags work?

What is the reason browsers do not correctly recognize:

<script src="foobar.js" /> <!-- self-closing script tag -->

Only this is recognized:

<script src="foobar.js"></script>

Does this break the concept of XHTML support?

Note: This statement is correct at least for all IE (6-8 beta 2).

Source: (StackOverflow)

How to make div not larger than its contents?

I have a layout similar to:


I would like for the div to only expand to as wide as my table becomes.

Source: (StackOverflow)

How do I reformat HTML code using Sublime Text 2?

I've got some poorly-formatted HTML code that I'd like to reformat. Is there a command that will automatically reformat HTML code in Sublime Text 2 so it looks better and is easier to read?

Source: (StackOverflow)

When looking at most sites (including SO), most of them use:

<input type="button" />

instead of:

  • What are the main differences between the two, if any?
  • Are there valid reasons to use one instead of the other?
  • Are there valid reasons to use combine them?
  • Does using <button> come with compatibility issues, seeing it is not very widely used?

Source: (StackOverflow)

Change an element's class with JavaScript

How can I change a class of an HTML element in response to an onClick event using JavaScript?

Source: (StackOverflow)

Set cellpadding and cellspacing in CSS?

In an HTML table, the cellpadding and cellspacing can be set like this:

<table cellspacing="1" cellpadding="1">

How can the same be accomplished using CSS?

Source: (StackOverflow)

Horizontally center a div in a div

How can I horizontally center a <div> within another <div> using CSS (if it's even possible) when the outer <div> has width: 100%?

For example:

<div id="outer" style="width: 100%">  
  <div id="inner">Foo foo</div>

Source: (StackOverflow)

Change an input's HTML5 placeholder color with CSS

Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).

But the following CSS doesn't do diddly squat to the placeholder's value:

input[placeholder], [placeholder], *[placeholder] {
    color: red !important;
<input type="text" placeholder="Value">

Value will still remain grey instead of red.

Is there a way to change the color of the placeholder text?

I'm already using the jQuery placeholder plugin for the browsers that don't support the placeholder attribute natively.

Source: (StackOverflow)

Href attribute for JavaScript links: "#" or "javascript:void(0)"?

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better, in terms of functionality, page load speed, validation purposes, etc.?

function myJsFunc() {
<a rel='nofollow' href="#" onclick="myJsFunc();">Run JavaScript Code</a>


function myJsFunc() {
 <a rel='nofollow' href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>

Source: (StackOverflow)