While trying to add <li>
tags to <ul>
list using JavaScript, the tags start to move to the left increasingly (distance between horizontal tags is becoming smaller).
let days_list = document.querySelector('.days');
for (let day = 1; day <= 30; day++) {
let li_tag = document.createElement('li');
li_tag.innerHTML = day.toString();
days_list.append(li_tag)
}
li {
list-style-type: none;
display: inline-block;
width: 13.6%;
text-align: center;
margin-bottom: 5px;
font-size: 15px;
color: black;
vertical-align: top;
}
<div>
<ul class="days">
<li>12</li>
<li>12</li>
<li>12</li>
</ul>
</div>
Although static adding of the same elements does not cause such a problem. CSS selectors seem to work perfectly as the dynamic tags have prescribed colors, fonts etc.
vertical-align: top;
does not work either. What seems to be the problem?
2
Answers
Have you tried to using a grid layout with?:
display: grid
andgrid-template-columns: repeat(7, auto)
You set the
display
of your list elements toinline-block
, and inline elements are sensitive to the white space in your code. Simply get rid of the space around your 12’s and everything will look normal.