javascript - How can I make my CSS update consistently? -
when click on button layout falls apart. when click on button css breaks apart layout. how update css layout divs properly?
$(document).ready(function(e) { totalcount = 100; (var = 0; < totalcount; i++) { var newdiv = document.createelement("div"); newdiv.innerhtml = "number " + i; var applyclass = "bgcolor"; //identify class document.getelementbyid("maincontainer").appendchild(newdiv); newdiv.id = "div" + i; document.getelementbyid(newdiv.id).classname = applyclass; $('#div' + i).click(callbackfunction()); } function callbackfunction(e) { return function() { //remove instances of css (var = 0; < totalcount; i++) { $("#div" + i).removeclass('newcolor'); } document.getelementbyid(this.id).classlist.add('newcolor'); } } });
div { background-color: #ff0; padding: 2px; font-size: 12px; float: left; margin-left: 2px; margin-right: 2px; margin-top: 30px; padding: 5px; cursor: pointer; -webkit-transition: .5s ease-in-out; transition: .5s ease-in-out } .newcolor { background-color: #00ffff; font-size: 16px; margin-left: 6px; margin-right: 6px; margin-top: 28px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body id="maincontainer"> </body>
you can achive removing
float
and making div this.
div { background-color: #ff0; padding:2px; font-size:12px; word-wrap: break-word;//this 1 too. incase word becomes long. display: inline-block;//this 1 has been added. margin-left: 2px; margin-right: 2px; margin-top: 30px; width:auto; padding: 5px; cursor:pointer; -webkit-transition: .5s ease-in-out; transition: .5s ease-in-out }
Comments
Post a Comment