Hello everyone, tell me please, why the append result is different from static html?

I used an example to illustrate: https://www.w3schools.com/code/tryit.asp?filename=FLSELJ1WJACU

$("#test_jq").append("<div class=\"test\"></div>"); $("#test_jq").append("<div class=\"test\"></div>"); $("#test_jq").append("<div class=\"test\"></div>"); $("#test_jq2").append("<div class=\"test\"></div>"); $("#test_jq2").append("<div class=\"test\"></div>"); $("#test_jq2").append("<div class=\"test\"></div>"); 
 .test { padding: 0; margin: 1px; position: relative; width: 100px; height: 120px; display: inline-block; background-color: pink; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1>This is a Heading</h1> <h2>Test preview</h2> <div> <div class="test"></div> <div class="test"></div> <div class="test"></div> </div> <div id="test_jq"></div> <h2>Test preview 2</h2> <div id="test_jq2"></div> <div> <div class="test"></div> <div class="test"></div> <div class="test"></div> </div> 

    2 answers 2

    Quite an interesting example. When you put a clean inline-block into a static layout - with tabs (or spaces, it doesn't matter) and line breaks - then you will see these spaces and line breaks. Try adding this css rule:

     div { font-size: 0; } 

    Just when you do elem.append(childElem) - you do not add hyphens and spaces, and everything works out as you would expect.

    • Thank! Immediately did not realize ... - mat.twg Nov.
    • @ mat.twg If my answer helped you, accept it. - Klimenkomud

    Try this:

     <h2>Test preview</h2> <div><div class="test"></div><div class="test"></div><div class="test"></div></div> <div id="test_jq"></div> 

    In my opinion, everything is clear.

    • Thank! I do not understand why you were minus, really everything is clear! - mat.twg
    • @ mat.twg I don't understand either. Everyone has their own opinion. All together it is called the community ruSO :). - Igor