Google

Apr 3, 2013

CSS Interview Questions and answers

Modern applications are web driven using HTML, CSS, and JavaScript. HTML is the building block of Web pages. CSS is the language used to make those Web pages pretty. JavaScript is used for dynamically manipulating the DOM elements and their look and feel. Understanding the basics of HTML and CSS will help you build better Web pages. CSS (Cascading Style Sheets) are vary useful and it it really worth understanding its key concepts. This blog explains the key concepts via questions and answers style.

Q. For the HTML snippet below, can you answers the questions listed below.

<div id="myAppDetails">
  <form  class="submit">
   <fieldset>
    <dl>
     <dt>
      <label for="portfolioCd">
       <h4>Portfolio Code</h4>
      </label>
     </dt>
     <dd>
      <input id="portfolioCd" type="text">
     </dd>
    </dl>
    <dl>
     <dt>
      <label for="valuationDate">
       <h4>
        Valuation Date <small>(dd/mm/yyyy)</small>
       </h4>
      </label>
     </dt>
     <dd>
      <input id="valuationDate" type="text">
     </dd>
    </dl>

    <div class="buttons">
     <button class="btn" type="submit">
     <span class="submit">Submit</span> <span class="refresh">Refresh</span>
     </button>
    </div>
    
    </fieldset>
  </form>
 
</div>



Q1. How will you write a CSS selector to hide the submit and refresh buttons?
A1. Here is the CSS

 
<style>
  #myAppDetails form button .submit,
  #myAppDetails form button .refresh {
        display: none;
  }
</style>


In the code snippet above

#myAppDetails :  refres to an element with id  myAppDetails, which is the top "DIV" element.
form :  refers to a child form element under the element with id "myAppDetails".
button : refers to a child button element under the  form element in the hierarchy.
.submit or .refresh : refers to an element with a style class, which has  the value of either "submit" or "refresh" in the hierarchy under the button element.

Finally the the "display:none" will hide the element selected.



Q2. In the above example, if you want to select more specifically the form element that has the style class submit, and a child element button with class  submit or refresh, how will you write the CSS selector to display the selected element?


A2: Here is the CSS snippet.

<style>
    #myAppDetails  form.submit button .submit,
    #myAppDetails  form.refresh button .refresh {
        display: inline;
    }
 
</style>




The above CSS is more specific than the previous one. The previous one selects any form, but this one selects only the form element with a style class of submit.

#myAppDetails : select any element with id "myAppDetails".
form.submit : select an element that has a style class "submit".
button :  a button child element under a form with class "submit"
.submit or .refresh : a child element with a class name either "submit" or "refresh" under a button element.

You can define multiple selectors with a comma.

Q3. What if the button element must immediately follow the form element?
A3.  You can use the symbol ">" to denote immediate. This is not used frequently. Here is the sample.

<style>
    #myAppDetails  form.submit>button .submit,
    #myAppDetails  form.refresh>button .refresh {
        display: inline;
    }
 
</style>


The two core concepts that are difficult to master in CSS are:

  1. CSS Specificity.
  2. CSS Floats


Q4. Why does your CSS rules don't apply to some elements?
A4. You can minimize the time it takes for hunting bugs by understanding how the browsers interpret your code and using developer tools like firebug for Firefox and Google chrome developer tool.

  • Every CSS selector has its place in the specificity hierarchy. If two selectors apply to the same element as demonstrated in the above example, the one with the higher specificity wins. For example,
"#myAppDetails  form.submit button .submit" is more specific than "#myAppDetails form button .submit" because the first one looks for specific form with class value of submit, whereas the second one applies to any form.
  • There are four distinct categories which determine the specificity level of a given selector: inline styles, IDs, classes+attributes and elements. ID selectors have a higher specificity than attribute selectors. You should always try to use IDs to increase the specificity. A class selector beats any number of element selectors. Here is an example:
 <div id="myDivId" style="font-color:blue"  class="aCSSClass" >

In CSS you will have

.aCSSClass {
     font-weight:12;
}

/** CSS comment: attribute css selector **/
[title] {
    color:blue
}

You can calculate the specificity yourself by allocating some weights, but the better approach is to use developer tools like Firebug or Chrome developer tool to calculate it for you and display the effective CSS.

Q5: When will you use a float?
A5: A float is used for laying out elements with CSS in a Web page.

Q6: Where can you apply a float?
A6: You can't float every element on a Web page. To get technical, you can only float block level elements. These are the elements that take up a block of space on the page, like ,
,
    , etc. The other elements known as inline elements like , , , etc can't be floated.


Q7: What are some of the basics you need to keep in mind regarding floats?
A7:

  • When you float an element it becomes a block box. This box can then be shifted to the left or right on the current line. The markup options are float: left, float: right or float: none.
  • You should always set a width on floated items, except if applied directly to an image, which has an implicit width. If no width is set, the results can become unpredictable.
  • The elements can only be floated to the left or right, it is not possible to make an element float in the centre. When we float an element it is shifted to the right or to the left until it reaches the edge of the containing block.
  • Elements following a floated element will wrap around the floated element. If you do not want this to occur, you can apply the β€œclear” property to these following elements. The four options are clear: left, clear: right, clear: both or clear: none.
  • If the container element is the HTML , the floated div will sit on the left margin of the page. If the container element is itself contained by something else, the floated div will sit on the left margin of the container.
Q8: What tips would you give to someone who is using CSS in their projects?
A8:
  • Use browser based development tools like firebug.
  • For a more professional CSS, use a library like Google bootstrap.
  • Use JavaScript frameworks like jQuery to dynamically manipulate CSS.
  • Understand the core concepts like selectors, specificity, and floats as explained above.

Here is CSS and HTML in action demonstrating float and some of the other  CSS concepts with an example.




Labels: ,

Mar 19, 2013

Power of HTML, CSS and jQuery -- collapsible CSS Table

It’s generally recommended you ensure the vast majority of your web page content is immediately visible when someone accesses it – but there are exceptions where you would want to display only the summarized view to not clutter up the page and expand to the details as and when required by clicking on a button or an icon. Here is a very simple tutorial on HTML, CSS, and jQuery that shows how to create a collapsible table. The following code snippet has embedded CSS with the style tag and jQuery with the script tag. The HTML defines the DOM (Document Object Model) structure. The CSS is used to hide and unhide the relevant rows. The jQuery is used to add and remove CSS styles.


Try using this example on Firefox with Firbug turned on or on Goggle chrome with the dEV tools turned on with the "F12" function key. This will enable you to inspect the HTML elements and the styles.

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>

</head>
<body>

<h1>demo for hiding and unhiding table rows </h1>

<button id="myappSummaryCollapse">
    <span class="collapse">Collapse</span>
    <span class="expand">Expand</span>
</button>

<table class="collapsible">
    <tr class="collapsible"><td>collapsible row</td></tr>
    <tr><td>not collapsible row</td></tr>
</table>


<script>

    jQuery('#myappSummaryCollapse').click(function () {
        var button = jQuery(this);
        var tables = jQuery('table.collapsible');

        if (button.hasClass('collapsed')) {
            button.removeClass('collapsed');
            for (var i = 0; i < tables.length; i++) {
                jQuery(tables[i]).removeClass('collapsed');
            }
        } else {
            button.addClass('collapsed');
            for (var i = 0; i < tables.length; i++) {
                jQuery(tables[i]).addClass('collapsed');
            }
        }
    });

</script>



<style>

    #myappSummaryCollapse .expand,
    #myappSummaryCollapse.collapsed .collapse,
    table.collapsed tr.collapsible {
        display: none;
    }

    #myappSummaryCollapse.collapsed .expand {
        display: inline;
    }

</style>


</body>
</html>


Very often, simple examples like the one above can clarify things to go on and do more complex scenarios. In the CSS, #myappSummaryCollapse is the element id and ".expand" is the style class name. "display:none" is used to hide and "display:inline" is used to display. The jQuery is used to add and remove the CSS classes.


Similar results can be achieved using JavaScript directly as shown below.

document.getElementById("p2").style.color="blue";


or

<button onclick="document.getElementById('id1').style.color='red'" type="button">
</button>



But, jQuery selectors are very powerful and let you achieve similar results with less code.

Labels: , ,

Aug 2, 2012

HTML and CSS Interview Questions and Answers

Q. Can you have a DIV element nested inside a SPAN element?
A. It is a bad practice to have a DIV element nested inside a SPAN element. You can have a SPAN inside a DIV. The reason being the DIV is a block element and SPAN is an inline element. So, why would you want to have a block element inside an inline element? SPAN is a like sub tag, which applies styles for the specified content in-between the SPAN tag.

Q. How will you decide between using nested HTML table tags and a DIV tag with CSS?
A. Use DIV tags with CSS for laying out the web pages and use TABLE for representing data.

Tabular data represented using html table tag.




To layout web page elements

The traditional way of laying out web pages was to place them inside invisible tables as demonstrated below. This way of laying out is more complicated to do, harder to maintain and makes the web pages to load very slowly.



Bad: Laying out with nested TABLE tags and CSS

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Table Layout</title>

<style type="text/css">
table {
 width: 100%;
 height: 1000px;
}

td.header {
 width: 100%;
 text-align: center;
 background-color: yellow;
}

td.contents {
 width: 80%;
 text-align: center;
 background-color: orange;
}

td.rhsmenu {
 width: 20%;
 text-align: center;
 background-color: green;
}

td.footer {
 width: 100%;
 text-align: center;
 background-color: blue;
}
</style>


</head>
<body>
 <table>
  <tbody>
   <tr height="15%">
    <td class="header" colspan="2">Header Information</td>
   </tr>
   <tr height="65%">
    <td class="contents">Contents go
     here</td>
    <td class="rhsmenu">RHS Panel</td>
   </tr>
   <tr height="20%">
    <td class="footer" colspan="2">Footer Information</td>
   </tr>
  </tbody>
 </table>

</body>
</html>

 The newer way to lay out your page elements is using DIVs and CSS. It can do everything tables can do and much more, and as an added bonus it loads a lot faster than tables ever did. It is easier to maintain, if you don't go overboard with nested DIVs. Use DIV for the conatainer and then use other elements SPAN, P, etc with CSS.

 Good: Laying out with DIVs and CSS tag


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Div Layout</title>

<!-- This can be defined outside in its own file and included into an html file -->
<style type="text/css">
body {
 display: block;
 width: 400px;
 height:400px;
 text-align: center;
}

/* main content container floated to the left*/
#content {
 position: relative;
 width: 80%;
 height: 65%;
 float: left;
 background-color: orange;
 width: 320px;
}

/* rhs menu container floated to the left*/
#rhs-menu {
 position: relative;
 width: 20%;
 height: 65%;
 float: left;
 background-color: green;
}

/* header container floated to the left*/
#header {
 position: relative;
 width: 100%;
 height: 15%;
 float: left;
 background-color: yellow;
}

/* footer with "clear"" to ensure that the footer is always below content and rhs-menu */
#footer {
 position: relative;
 width: 100%;
 height: 20%;
 clear: both;
 background-color: blue;
}

/* align text vertically*/
#content p,#rhs-menu p,#header p,#footer p {
 position: absolute;
 top: 50%;
 left: 50%;
 height: 50%;
 width: 50%;
 margin: -15% 0 0 -25%;
}

/* position the logo using absolute (from top left corner) or relative*/
.logo-text {
 position: absolute; 
 top: 5px;
 left: 10px;
}

</style>

</head>
<body>

 <div id="header"><span class="logo-text">logo goes here </span>
  <p>Header Information</p>
 </div>
 <div id="content">
  <p>Contents go here</p>
 </div>
 <div id="rhs-menu">
  <p>RHS Panel</p>
 </div>
 <div id="footer">
  <p>Footer Information</p>
 </div>

</body>
</html>

Note: It is a best practice to define HTML, CSS, and JavaScript in separate files. The CSS is combined with HTML in these examples for illustration purpose only.

The typical usecase for the "clear" css tag is when you want to put a footer at the bottom of your page, beneath the RHS menu and the content sections. So, by using the CSS tags like float, clear, absolute, relative, and display you can create attractive, maintainable, and faster layouts.

Labels: ,