I’m trying to right justify the numbers centered under section with the check boxes and input fields neatly lined up in a column. This all works until I get to number 10 where 10 and the check box are a little off to the right.
input[type=number], p{
width:30px;
margin-left:75px;
margin-right:75px;
}
#captions h2{
color:blue;
display:inline-block;
width:180px;
border:solid 1px green;
text-align:center;
}
#main{
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
border:solid 1px black;
}
.inputs{
display:flex;
align-items:center;
width:360px;
margin-top:8px;
margin-bottom:8px;
}
.inputs span{
display:flex;
width:180px;
justify-content:center;
}
<div id="main">
<span id="captions">
<h2>Sections</h2><h2># of Questions</h2>
</span>
<p class="inputs">
<span>1 <input type='checkbox'></span> <input type="number">
</p>
<p class="inputs">
<span>2 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>3 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>4 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>5 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>6 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>7 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>8 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>9 <input type='checkbox'></span><input type="number">
</p>
<p class="inputs">
<span>10 <input type='checkbox'></span><input type="number">
</p>
</div>
2
Answers
Flexbox is one-dimensional. You’ll need to either switch to grid or specify element width to align content across flex containers. Also, don’t use whitespace characters for layout. Use flex properties, margin, etc.
One fairly simple approach is to size and center the spans inside the flex elements. You’ll have to pick a size which gets you fairly close to whatever maximum size you expect to need, then right-align its content. I’ve added wrappers to get the columns to be 50% width.
My friend, it is better to not use static numbers, instead you can use
counter-reset
in CSS or use theol
tag to make the numbers dynamic.Also, by using
ol
tag, you don’t need to take care of the style with the numbers.I put
::marker
in the code to show you that, you can even control the style of the markers.You can change the
list-style-type
todecimal
to remove the zero before the single numbers.