views:

289

answers:

3

I am trying to allign a table with an input so that the table creates a google-like drop-down. I am using jquery to retrieve the position and dimensions of the input element and then css to position the table. Things work pretty smooth when I don't use DOCTYPE.

Now, when ANY DOCTYPE is included on the page things go wrong (weirdly not in IE). Table is suddenly 1px wider and moves up and left by 1px.

Please someone help! Perhaps some of you have a piece of code that doesn the correct allignment.

<!--<!DOCTYPE html>-->
<html>

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

    <body>

        <input type="text" id="input" />        
        <table id="dropdown" style="border: solid 1px black; border-collapse: collapse; position: absolute;">
            <tr>
                <td>Text</td>
            </tr>
        </table>

        <script>

        var input = $("#input");
        var dropdown = $("#dropdown");

        dropdown.css("top", input.position().top + input.outerHeight() - 1);
        dropdown.css("left", input.position());
        dropdown.css("width", input.outerWidth());

        </script>

    </body>

</html>
A: 

It's likely due to absolute positioning:

position: absolute;

Try removing that or changing it to relative and setting margins.

Absolute positioning is a tricky thing to do, and often it has to be modified for IE vs other browsers.

Jeremy Morgan
A: 

Using the following HTML and jQuery I was able to make the table appear and position the same in both IE8 and Chrome 2.

<!DOCTYPE html>
<html>
 <head>
     <title>Test Page</title>
  </head>
<body>
     <input type="text" id="input" />
     <table id="dropdown" style="border: solid 1px black; 
           border-collapse: collapse; position: absolute;">
        <tr>
           <td>
              Text
            </td>
         </tr>
      </table>

    <script type="text/javascript" 
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;
     </script>

     <script type="text/javascript">
        $(document).ready(function({
            var input = $("#input");
            var dropdown = $("#dropdown");
            dropdown.css("top", input.position().top + input.outerHeight() - 1);
            dropdown.css("left", input.position());
            dropdown.css("width", input.outerWidth());   
         });
      </script>

 </body>
</html>

I noticed that you were missing the head section of the HTML, the language type of the script tags and that your javascript did not have the document.ready section.

Hope this helps some.

Chris
A: 

Thanks for both your answers.

I actually found out the main problem was the border-collapse: collapse property. When set to collapse with <!DOCTYPE...> tag included then absolute table positioning and width setting gave results I wasn't expecting (but in fact desired behaviour as per the W3C standard). For the purpose of accurate alignment it proves better to set the border-collapse: separate.

David