jQuery inconsistency: width of a <div> element
The following code is based on the example from the course jQuery>Effect>animate(1of3). There's a <div> element (grey background) and below it a <p> element. By clicking on the <p> element you'll get an alert with the <div> element's width in pixels. By clicking on the <div> you'll get the same alert. When hovering over the <div> element its width increases to 450px, when the mouse leaves the div element its width is supposed to shrink back to the initial value, which was stored in the divWidth variable. Unfortunately this is not the case: if you test this code in the code playground you can see that when the page is loaded the <div>'s width is 230.58999633789062 px, after hovering with the mouse and leaving the <div> will shrink to 230.57998657226562 px. This 0.01px difference is enough to force the text to break in two lines. Is there any workaround against this, or do I have to change the code to divWidth+0.01 so that the <div> returns to its exact original width? HTML: <!DOCTYPE html> <html> <head> <title>Page Title</title> <script src="https://code.jquery.com/jquery-3.1.1.js"></script> </head> <body> <div class="test">We are writing a long text here now</div> <p>Check box width</p> </body> </html> CSS: div { display:inline-block; padding:25px; background-color:grey; color:white; text-align:center; cursor:pointer; text-align:left; } JS: $(function() { var divWidth = $("div.test").width(); $("div").hover(function() { $("div").animate({width: '450px'}, 1000); }); $("div").mouseleave(function() { $("div").animate({width: divWidth}, 1000); }); $("div").click(function() { alert($("div").width()); }); $("p").click(function() { alert($("div").width()); }); });